From ec2ff6afd1bd042c20094f527bd14f90c2574416 Mon Sep 17 00:00:00 2001 From: mmk2410 Date: Tue, 2 Jun 2015 17:25:06 +0200 Subject: [PATCH] Update through the app --- .../marcelkapfer/c/morseconverter/Main.form | 23 +++- .../marcelkapfer/c/morseconverter/Main.java | 36 +++--- .../c/morseconverter/update/Update.java | 75 +++++++++++++ .../c/morseconverter/update/UpdateDialog.form | 80 ++++++++++++++ .../c/morseconverter/update/UpdateDialog.java | 103 ++++++++++++++++++ 5 files changed, 299 insertions(+), 18 deletions(-) create mode 100644 src/de/marcelkapfer/c/morseconverter/update/Update.java create mode 100644 src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.form create mode 100644 src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.java diff --git a/src/de/marcelkapfer/c/morseconverter/Main.form b/src/de/marcelkapfer/c/morseconverter/Main.form index 0cea877..b12763b 100644 --- a/src/de/marcelkapfer/c/morseconverter/Main.form +++ b/src/de/marcelkapfer/c/morseconverter/Main.form @@ -99,14 +99,33 @@ - + - + + + + + + + + + + + + + + + + + + + + diff --git a/src/de/marcelkapfer/c/morseconverter/Main.java b/src/de/marcelkapfer/c/morseconverter/Main.java index 6bc307e..5227658 100644 --- a/src/de/marcelkapfer/c/morseconverter/Main.java +++ b/src/de/marcelkapfer/c/morseconverter/Main.java @@ -27,19 +27,20 @@ import de.marcelkapfer.c.morseconverter.engine.EncodeNormalMorseManager; import de.marcelkapfer.c.morseconverter.engine.EncodeWrittenMorseManager; import de.marcelkapfer.c.morseconverter.intelligentCodeRecognization.NormalMorseCodeRecognization; import de.marcelkapfer.c.morseconverter.intelligentCodeRecognization.WrittenMorseCodeRecognization; +import de.marcelkapfer.c.morseconverter.update.Update; +import de.marcelkapfer.c.morseconverter.update.UpdateDialog; import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; -import java.io.*; -import java.net.MalformedURLException; -import java.net.URL; public class Main extends JFrame { // Version numbers for update check - public static final int actualVersion = 200; - public int latestVersion = actualVersion; + public static final int currentVersion = 200; + public int latestVersion = currentVersion; public JTabbedPane morseConverterPane; private JPanel panel1; @@ -47,6 +48,7 @@ public class Main extends JFrame { private JTextArea outputAreaWrittenMorse; private JTextArea inputAreaNormalMorse; private JTextArea outputAreaNormalMorse; + private JButton updateDialogButton; // Contains mainly listeners public Main() { @@ -66,24 +68,22 @@ public class Main extends JFrame { }); // Trying to read a file from a server - try { - URL versionURL = new URL("http://c2/LAB/java/morseconverter/version.txt"); - BufferedReader versionReader = new BufferedReader(new InputStreamReader(versionURL.openStream())); - latestVersion = Integer.valueOf(versionReader.readLine()); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + latestVersion = Update.getLatestVersion("http://c2/LAB/java/morseconverter/version.txt", currentVersion); //TODO To be also shown in the about view - if (actualVersion < latestVersion){ + if (currentVersion < latestVersion){ JOptionPane.showMessageDialog(null, "An Update is available!"); } - } + updateDialogButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + updateDialog(); + } + }); + } // The holy main method public static void main(String[] args){ // Tries to present the app in an native look @@ -133,4 +133,8 @@ public class Main extends JFrame { } } + private void updateDialog() { + UpdateDialog.main(null); + } + } diff --git a/src/de/marcelkapfer/c/morseconverter/update/Update.java b/src/de/marcelkapfer/c/morseconverter/update/Update.java new file mode 100644 index 0000000..fb81cf4 --- /dev/null +++ b/src/de/marcelkapfer/c/morseconverter/update/Update.java @@ -0,0 +1,75 @@ +package de.marcelkapfer.c.morseconverter.update; + +import java.io.BufferedReader; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; +import java.util.logging.FileHandler; + +/* + This is a Java application for converting writtenMorse and normal morse code. + Copyright (C) 2014-2015 Marcel Michael Kapfer + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Marcel Michael Kapfer + marcelmichaelkapfer@yahoo.co.nz + + */ +public class Update { + + private static URL update; + + public static boolean isConnected(String url){ + try { + update = new URL(url); + update.openStream(); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + public static boolean update(String path, String filename){ + try { + ReadableByteChannel updateChannel = Channels.newChannel(update.openStream()); + FileOutputStream updateOutputStream = new FileOutputStream(path + filename); + updateOutputStream.getChannel().transferFrom(updateChannel, 0, Long.MAX_VALUE); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + public static int getLatestVersion(String versionFile, int currentVersion){ + int latestVersion = currentVersion; + try { + URL versionURL = new URL(versionFile); + BufferedReader versionReader = new BufferedReader(new InputStreamReader(versionURL.openStream())); + latestVersion = Integer.valueOf(versionReader.readLine()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return latestVersion; + } + +} diff --git a/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.form b/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.form new file mode 100644 index 0000000..0f715d3 --- /dev/null +++ b/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.form @@ -0,0 +1,80 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.java b/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.java new file mode 100644 index 0000000..d752c24 --- /dev/null +++ b/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.java @@ -0,0 +1,103 @@ +package de.marcelkapfer.c.morseconverter.update; + +/* + This is a Java application for converting writtenMorse and normal morse code. + Copyright (C) 2014-2015 Marcel Michael Kapfer + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Marcel Michael Kapfer + marcelmichaelkapfer@yahoo.co.nz + + */ + + +import de.marcelkapfer.c.morseconverter.Main; + +import javax.swing.*; +import java.awt.event.*; +import java.io.File; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class UpdateDialog extends JDialog { + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + + public UpdateDialog() { + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + + buttonOK.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onOK(); + } + }); + + buttonCancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + } + + private void onOK() { + // add your code here + try { + String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); + path = path.substring(0, path.lastIndexOf(File.separator)); + if(Update.isConnected("http://c2/LAB/java/morseconverter/morseconverter-1.jar")){ + if(Update.update(path + File.separator, "morseconverter-1.jar")){ + JOptionPane.showMessageDialog(null, "Update successful. \n Please restart the software to activate."); + } else { + JOptionPane.showMessageDialog(null, "Update not successful."); + } + } else { + JOptionPane.showMessageDialog(null, "The connection to the server was not possible. \n Try again later."); + } + } catch (URISyntaxException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, "There was an error while getting the current directory."); + } + dispose(); + } + + private void onCancel() { + // add your code here if necessary + dispose(); + } + + public static void main(String[] args) { + UpdateDialog dialog = new UpdateDialog(); + dialog.pack(); + dialog.setVisible(true); + } +}