This repository has been archived on 2022-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
morse-converter/src/de/marcelkapfer/c/morseconverter/update/UpdateDialog.java

104 lines
3.5 KiB
Java

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);
}
}