/*
* Copyright (C) 2015 Vinu K.N
*
* 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/>.
*/
package org.domainmath.gui.etc;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.io.FileUtils;
import org.domainmath.gui.MainFrame;
import org.domainmath.gui.dialog.Msg;
import org.domainmath.gui.dialog.NetworkError;
/**
*
* @author Vinu K.N
*/
public class ResultDialog extends javax.swing.JDialog {
private final String updateURL = "http://garr.dl.sourceforge.net/project/domainmath/update/update.xml";
private final String downloadURL = "http://garr.dl.sourceforge.net/project/domainmath/update/update.zip";
private Msg msg;
private final MainFrame frame;
private final boolean displayError;
private Thread thread;
public ResultDialog(MainFrame frame,boolean displayError) {
super(frame,true);
initComponents();
setLocationRelativeTo(null);
setIconImage(icon);
this.frame = frame;
this.displayError = displayError;
//check for updates.
checkUpdate();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
label = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
newVerLabel = new javax.swing.JLabel();
newBVerLabel = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
currentVerLabel = new javax.swing.JLabel();
currentBVerLabel = new javax.swing.JLabel();
buttonPanel = new javax.swing.JPanel();
updateButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("DomainMath IDE");
jPanel1.setBackground(new java.awt.Color(255, 155, 0));
label.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N
label.setForeground(new java.awt.Color(255, 255, 255));
label.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/etc/resources/appbar.speakerphone.png"))); // NOI18N
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/domainmath/gui/etc/resources/resultdialog_en"); // NOI18N
label.setText(bundle.getString("label.text")); // NOI18N
label.setToolTipText("");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel2.setText("More Information:");
jLabel1.setFont(new java.awt.Font("Segoe UI Semibold", 1, 12)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 255));
jLabel1.setText("New");
jLabel3.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 51, 51));
jLabel3.setText("Version");
jLabel4.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 51, 51));
jLabel4.setText("Build Version");
newVerLabel.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
newVerLabel.setForeground(new java.awt.Color(0, 102, 0));
newVerLabel.setText("0.1.7");
newBVerLabel.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
newBVerLabel.setText("201410261524");
jLabel7.setFont(new java.awt.Font("Segoe UI Semibold", 1, 12)); // NOI18N
jLabel7.setForeground(new java.awt.Color(0, 0, 255));
jLabel7.setText("Current");
currentVerLabel.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
currentVerLabel.setForeground(new java.awt.Color(0, 102, 0));
currentVerLabel.setText("0.1.6");
currentBVerLabel.setFont(new java.awt.Font("Segoe UI Semibold", 0, 12)); // NOI18N
currentBVerLabel.setText("201409261524");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(jLabel1))))
.addGap(61, 61, 61)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(currentVerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(newVerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(newBVerLabel)
.addComponent(currentBVerLabel))
.addGap(83, 83, 83))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(newVerLabel)
.addComponent(newBVerLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(currentVerLabel)
.addComponent(currentBVerLabel))
.addContainerGap(54, Short.MAX_VALUE))
);
buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 10, 5, 10));
buttonPanel.setPreferredSize(new java.awt.Dimension(513, 44));
buttonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
updateButton.setText("Update");
updateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateButtonActionPerformed(evt);
}
});
buttonPanel.add(updateButton);
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
buttonPanel.add(cancelButton);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
dispose();
}//GEN-LAST:event_cancelButtonActionPerformed
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
thread = new Thread(new UpdateThread());
thread.start();
dispose();
MainFrame.statusPanel.changeStatus("Downloading update...");
}//GEN-LAST:event_updateButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(() -> {
ResultDialog dialog = new ResultDialog(null,true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
});
}
public java.net.URL imgURL = getClass().getResource("/org/domainmath/gui/etc/resources/DomainMath.png");
public Image icon = Toolkit.getDefaultToolkit().getImage(imgURL);
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton cancelButton;
private javax.swing.JLabel currentBVerLabel;
private javax.swing.JLabel currentVerLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel label;
private javax.swing.JLabel newBVerLabel;
private javax.swing.JLabel newVerLabel;
private javax.swing.JButton updateButton;
// End of variables declaration//GEN-END:variables
/**
* Check for update. First we download update.xml from sourcefoge,compare with local
* update.xml file. If build number is different,try to download update.zip
* from sourceforge.
*/
private void checkUpdate() {
File updateDir = new File(System.getProperty("user.dir")+File.separator+"update");
File updateFile = new File(System.getProperty("user.dir")+File.separator+"update"+File.separator+"update.xml");
updateDir.mkdir();
try {
// download file update.xml.
FileUtils.copyURLToFile(new URL(updateURL), updateFile.getAbsoluteFile());
Update update = getUpdate(new File(System.getProperty("user.dir")+File.separator+"update.xml"));
Update update2 = getUpdate(new File(System.getProperty("user.dir")+File.separator+"update"+File.separator+"update.xml"));
// compare build number.
if(update.getBuildNumber().equals(update2.getBuildNumber())){
Msg msg = new Msg(true);
msg.setSecondTitle("DomainMath IDE up to date.");
msg.setVisible(true);
deleteTemp(updateDir);
}else{
this.currentVerLabel.setText(update.getVersionName());
this.currentBVerLabel.setText(update.getBuildNumber().toString());
this.newVerLabel.setText(update2.getVersionName());
this.newBVerLabel.setText(update2.getBuildNumber().toString());
this.setVisible(true);
}
} catch (MalformedURLException ex) {
if(displayError){
nerror("Unable to check updates.");
}
System.err.println("Unable to check updates...");
} catch (IOException ex) {
if(displayError){
nerror("Unable to check updates.");
}
System.err.println("Unable to check updates...");
}
dispose();
}
/**
* Display different types of network errors.
* @param error
*/
private void nerror(String error){
new NetworkError("DomainMath IDE",error,true);
dispose();
}
/**
* Delete downloaded update.xml when no update is available.
* @param f
*/
private void deleteTemp(File f){
try {
FileUtils.deleteDirectory(f);
} catch (IOException ex) {
}
}
/**
* Process update.xml
* @param file
* @return Update object
*/
private Update getUpdate(File file){
Update update=null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Update.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
update = (Update) jaxbUnmarshaller.unmarshal(file);
return update;
} catch (JAXBException ex) {
dispose();
}
return update;
}
/**
* Download update from sourceforge.
*/
private void updateDMI() {
File updateFile = new File(System.getProperty("user.dir")+File.separator+"update"+File.separator+"update.zip");
try {
FileUtils.copyURLToFile(new URL(downloadURL), updateFile.getAbsoluteFile());
msg = new Msg("Update completed.","Please close DomainMath IDE and double click Update.jar file to complete update process.",true);
msg.setVisible(true);
MainFrame.statusPanel.changeStatus("Downloading completed.");
MainFrame.statusPanel.changeStatus("Get Ready.");
thread.interrupt();
String cmd = System.getProperty("java.home")+File.separator+"bin"+File.separator+"java -jar "+
System.getProperty("user.dir")+File.separator+"update.jar";
Runtime.getRuntime().exec(cmd);
} catch (MalformedURLException ex) {
nerror("Unable to download update files.\nPlease check your network connection.");
} catch (IOException ex) {
nerror("Unable to download update files.\nPlease check your network connection.");
}
}
/**
* Updating DMI should run in seperate process.
*/
class UpdateThread implements Runnable{
@Override
public void run() {
updateDMI();
}
}
}