OSDN Git Service

fix: organize imports
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / CheckVersion.java
1 package com.ranfa.lib;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import javax.swing.JOptionPane;
8
9 import com.fasterxml.jackson.core.JsonProcessingException;
10 import com.fasterxml.jackson.databind.JsonMappingException;
11 import com.fasterxml.jackson.databind.JsonNode;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.ranfa.main.DelesteRandomSelector;
14
15 public class CheckVersion {
16
17         private final static String URI = "https://raw.githubusercontent.com/hizumiaoba/DelesteRandomSelector/master/version.json";
18         private final static String RELEASE_STRING = "https://github.com/hizumiaoba/DelesteRandomSelector/releases";
19
20         public static void needToBeUpdated() {
21                 int latestMajor = 0;
22                 int latestMinor = 0;
23                 int latestPatch = 0;
24                 try {
25                         JsonNode node = new ObjectMapper().readTree(new URL(URI));
26                         latestMajor = node.get("major").asInt();
27                         latestMinor = node.get("minor").asInt();
28                         latestPatch = node.get("patch").asInt();
29                 } catch (JsonMappingException e) {
30                         e.printStackTrace();
31                 } catch (JsonProcessingException e) {
32                         e.printStackTrace();
33                 } catch (MalformedURLException e) {
34                         e.printStackTrace();
35                 } catch (IOException e) {
36                         e.printStackTrace();
37                 }
38                 String newVersion = String.format("v%d.%d.%d", latestMajor, latestMinor, latestPatch);
39                 if(latestMajor > getMajorVersion()) {
40                         JOptionPane.showInputDialog(null, String.format("大規模なソフトウェアの更新が公開されています。速やかにアップデートをお願いします。\n最新バージョン:%s", newVersion), RELEASE_STRING);
41                 } else if(latestMinor > getMinorVersion()) {
42                         JOptionPane.showInputDialog(null, String.format("ソフトウェアの軽微な機能改修が公開されています。こちらから最新バージョンをダウンロードしてください。\n最新バージョン:%s", newVersion), RELEASE_STRING);
43                 } else if(latestPatch > getPatchVersion()) {
44                         JOptionPane.showInputDialog(null, String.format("ソフトウェアのバグ修正が公開されています。こちらから最新バージョンをダウンロードしてください。\n最新バージョン:%s", newVersion), RELEASE_STRING);
45                 }
46         }
47
48
49         /**
50          * アノテーションで記載されているバージョンを取得します
51          * @since v1.0.0
52          * @return アノテーションで定義されているバージョン
53          */
54         public static String getVersion() {
55                 String value = "v"
56                                 + getMajorVersion() + "."
57                                 + getMinorVersion() + "."
58                                 + getPatchVersion();
59                 return value;
60         }
61
62         public static int getMajorVersion() {
63                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
64                 return version.major();
65         }
66
67         public static int getMinorVersion() {
68                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
69                 return version.minor();
70         }
71
72         public static int getPatchVersion() {
73                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
74                 return version.patch();
75         }
76
77 }