diff --git a/pom.xml b/pom.xml index 9b4e1bc..14c921f 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ info.istlab.IoTP IoTP jar - 0.1 + 0.11 IoTP http://maven.apache.org diff --git a/src/main/java/info/istlab/IoTP/App.java b/src/main/java/info/istlab/IoTP/App.java index 5856993..9e97b73 100644 --- a/src/main/java/info/istlab/IoTP/App.java +++ b/src/main/java/info/istlab/IoTP/App.java @@ -1,7 +1,21 @@ package info.istlab.IoTP; +import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.CodeSource; +import java.security.ProtectionDomain; import java.util.ArrayList; /** @@ -13,12 +27,22 @@ static ArrayList iotDirs; static String workingDir; + + public static String latestVersion; + public static Path execPath; + public static String downloadurl = "https://cit.istlab.info/IoTP/target/"; + static { userhome = System.getProperty("user.home"); } public static void main(String[] args) { + try { + App.execPath = getApplicationPath(new App().getClass()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } // IoTPフォルダを探す iotDirs = new ArrayList(); @@ -28,11 +52,13 @@ workingDir = iotDirs.get(0).getAbsolutePath(); System.out.println(workingDir); new Launcher(iotDirs.get(0)).setVisible(true); - } else { + } else if (iotDirs.size() > 1){ System.out.println("フォルダが複数みつかりました"); for(File f: iotDirs){ System.out.println(f.getAbsolutePath()); } + } else { + } } @@ -57,4 +83,76 @@ } } } + + + public static Path getApplicationPath(Class cls) throws URISyntaxException { + ProtectionDomain pd = cls.getProtectionDomain(); + CodeSource cs = pd.getCodeSource(); + URL location = cs.getLocation(); + URI uri = location.toURI(); + Path path = Paths.get(uri); + return path; + } + + public static boolean isLatest() { + // 最初に、ダウンロードサイトから最新版の情報を得る + // 更新があれば、ダウンロードし、ファイルを表示する。 + StringBuilder sb = new StringBuilder(); + latestVersion = null; + try { + URL url = new URL(downloadurl); + InputStream is = url.openConnection().getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + String line = null; + while ((line = reader.readLine()) != null) { + if (latestVersion == null) { + latestVersion = line.trim(); + System.out.println(line); + } + sb.append(line + "\n"); + } + reader.close(); + } catch (Exception ex) { + } + // System.out.println(sb.toString()); + // 同じかどうかチェック + // System.out.println("[" + latestVersion + "]"); + // System.out.println("=" + execPath.getFileName() + "="); + if (execPath.getFileName().toString().equals("classes")) + return true; + return latestVersion.equals(execPath.getFileName().toString()); + } + + // ダウンロードして実行パーミッションをつける + public static String download() { + try { + URL url = new URL(downloadurl + "/" + latestVersion); + ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(latestVersion); + FileChannel fileChannel = fileOutputStream.getChannel(); + fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); + fileChannel.close(); + fileOutputStream.close(); + + File execjar = new File(execPath.getParent().toString() + File.separator + latestVersion); + execjar.setExecutable(true); + return execjar.getAbsolutePath(); + } catch (Exception ex) { + } finally { + } + return null; + } + + public static void reboot(String pathfile) { + String com = "sleep 2 ; java -jar " + pathfile; + System.out.println(com); + ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", com); + try { + processBuilder.start(); + System.exit(0); + }catch(Exception ex){ + + } + } + } diff --git a/src/main/java/info/istlab/IoTP/Launcher.java b/src/main/java/info/istlab/IoTP/Launcher.java index 4e43754..f2b2be2 100644 --- a/src/main/java/info/istlab/IoTP/Launcher.java +++ b/src/main/java/info/istlab/IoTP/Launcher.java @@ -27,7 +27,7 @@ public class Launcher extends JFrame implements MouseInputListener, KeyListener { public static Launcher theapp; - public static String version = "0.1"; + public static String version = "0.11"; // JPanel mainP; File root; JTree tree; @@ -70,15 +70,8 @@ JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("App"); /**************************************************** */ menuBar.add(menu); - JMenuItem mi = new JMenuItem("show version"); - mi.addActionListener(ae -> JOptionPane.showMessageDialog(this, "IoTP Launcher v" + version)); - menu.add(mi); - // mi = new JMenuItem("最新版をチェック"); - // // mi.addActionListener(ae -> downloadLatest()); - // menu.add(mi); - - mi = new JMenuItem("シリアルモニタを開く"); + JMenuItem mi = new JMenuItem("シリアルモニタを開く"); mi.addActionListener(ae -> SerialWindow.invoke() ); menu.add(mi); @@ -86,16 +79,30 @@ mi.addActionListener(ae -> openExecPath()); menu.add(mi); - mi = new JMenuItem("exit"); + + mi = new JMenuItem("IoTPのバージョン確認"); + mi.addActionListener(ae -> JOptionPane.showMessageDialog(this, "IoTP Launcher v" + version)); + menu.add(mi); + + mi = new JMenuItem("最新版があるかチェック"); + mi.addActionListener(ae -> downloadLatest()); + menu.add(mi); + + mi = new JMenuItem("ダウンロードサイトをブラウザで開く"); + mi.addActionListener(ae -> openURL(App.downloadurl)); + menu.add(mi); + + + mi = new JMenuItem("終了"); mi.addActionListener(ae -> System.exit(0)); menu.add(mi); menu = new JMenu("File"); /**************************************************** */ menuBar.add(menu); - jcbmi = new JCheckBoxMenuItem("exclude class files from filetree"); - jcbmi.addItemListener(ae -> reloadFiles()); - menu.add(jcbmi); + // jcbmi = new JCheckBoxMenuItem("exclude class files from filetree"); + // jcbmi.addItemListener(ae -> reloadFiles()); + // menu.add(jcbmi); mi = new JMenuItem("reload filetree"); mi.addActionListener(ae -> reloadFiles()); @@ -103,9 +110,9 @@ menu.addSeparator(); - mi = new JMenuItem("rename/delete NWP folder"); - mi.addActionListener(ae -> renameOrDeleteNWPFolder()); - menu.add(mi); + // mi = new JMenuItem("rename/delete NWP folder"); + // mi.addActionListener(ae -> renameOrDeleteNWPFolder()); + // menu.add(mi); // mi = new JMenuItem("get NWP folder"); // mi.addActionListener(ae -> App.getNWPifnotexist(true)); @@ -188,22 +195,22 @@ tree.setModel(model); } - // public void downloadLatest() { - // boolean isLatest = App.isLatest(); + public void downloadLatest() { + boolean isLatest = App.isLatest(); - // // if () { - // if (isLatest) { - // JOptionPane.showMessageDialog(this, - // "すでに最新版です : v" + version + "\n(download folder: " + App.execPath.getParent().toString() + ")"); - // } else { - // int res = JOptionPane.showConfirmDialog(this, "最新版 \n(" + App.latestVersion + ") をダウンロードする?\n(download folder: " - // + App.execPath.getParent().toString() + ")"); - // if (res == JOptionPane.YES_OPTION) { - // App.download(); - // openExecPath(); - // } - // } - // } + // if () { + if (isLatest) { + JOptionPane.showMessageDialog(this, + "すでに最新版です : v" + version + "\n(download folder: " + App.execPath.getParent().toString() + ")"); + } else { + int res = JOptionPane.showConfirmDialog(this, "最新版 \n(" + App.latestVersion + ") をダウンロードする?\n(download folder: " + + App.execPath.getParent().toString() + ")"); + if (res == JOptionPane.YES_OPTION) { + App.download(); + openExecPath(); + } + } + } public void openExecPath() { File execFolder = new File(App.workingDir);