Update through the app

This commit is contained in:
mmk2410 2015-06-02 17:25:06 +02:00
parent e2a90823b7
commit ec2ff6afd1
5 changed files with 299 additions and 18 deletions

View file

@ -99,14 +99,33 @@
</scrollpane> </scrollpane>
</children> </children>
</grid> </grid>
<grid id="4a85e" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="4a85e" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <constraints>
<tabbedpane title="About"/> <tabbedpane title="About"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children/> <children>
<component id="fadb2" class="javax.swing.JButton" binding="updateDialogButton" default-binding="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Update Dialog"/>
</properties>
</component>
<hspacer id="f1f0d">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="44230">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid> </grid>
</children> </children>
</tabbedpane> </tabbedpane>

View file

@ -27,19 +27,20 @@ import de.marcelkapfer.c.morseconverter.engine.EncodeNormalMorseManager;
import de.marcelkapfer.c.morseconverter.engine.EncodeWrittenMorseManager; import de.marcelkapfer.c.morseconverter.engine.EncodeWrittenMorseManager;
import de.marcelkapfer.c.morseconverter.intelligentCodeRecognization.NormalMorseCodeRecognization; import de.marcelkapfer.c.morseconverter.intelligentCodeRecognization.NormalMorseCodeRecognization;
import de.marcelkapfer.c.morseconverter.intelligentCodeRecognization.WrittenMorseCodeRecognization; 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 javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Main extends JFrame { public class Main extends JFrame {
// Version numbers for update check // Version numbers for update check
public static final int actualVersion = 200; public static final int currentVersion = 200;
public int latestVersion = actualVersion; public int latestVersion = currentVersion;
public JTabbedPane morseConverterPane; public JTabbedPane morseConverterPane;
private JPanel panel1; private JPanel panel1;
@ -47,6 +48,7 @@ public class Main extends JFrame {
private JTextArea outputAreaWrittenMorse; private JTextArea outputAreaWrittenMorse;
private JTextArea inputAreaNormalMorse; private JTextArea inputAreaNormalMorse;
private JTextArea outputAreaNormalMorse; private JTextArea outputAreaNormalMorse;
private JButton updateDialogButton;
// Contains mainly listeners // Contains mainly listeners
public Main() { public Main() {
@ -66,24 +68,22 @@ public class Main extends JFrame {
}); });
// Trying to read a file from a server // Trying to read a file from a server
try { latestVersion = Update.getLatestVersion("http://c2/LAB/java/morseconverter/version.txt", currentVersion);
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();
}
//TODO To be also shown in the about view //TODO To be also shown in the about view
if (actualVersion < latestVersion){ if (currentVersion < latestVersion){
JOptionPane.showMessageDialog(null, "An Update is available!"); JOptionPane.showMessageDialog(null, "An Update is available!");
} }
updateDialogButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateDialog();
} }
});
}
// The holy main method // The holy main method
public static void main(String[] args){ public static void main(String[] args){
// Tries to present the app in an native look // Tries to present the app in an native look
@ -133,4 +133,8 @@ public class Main extends JFrame {
} }
} }
private void updateDialog() {
UpdateDialog.main(null);
}
} }

View file

@ -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 <http://www.gnu.org/licenses/>.
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;
}
}

View file

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.marcelkapfer.c.morseconverter.update.UpdateDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="551" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Yes"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="No"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="4147a" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="A new version of the Morse Converter is available."/>
</properties>
</component>
<component id="89f1c" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Do you want to download and install it?"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View file

@ -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 <http://www.gnu.org/licenses/>.
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);
}
}