/* * Copyright (C) 2014 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.update; import java.awt.Image; import java.awt.Toolkit; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import org.apache.commons.io.FileUtils; import org.domainmath.gui.dialog.Error; import org.domainmath.gui.dialog.Msg; /** * It helps user to complete update process. * @author Vinu K.N */ public class UpdateFrame extends javax.swing.JFrame{ private static final long serialVersionUID = 5346596629938962773L; private File backup; private final File updateZipFile; private final File userDir; private final File updateDir; public UpdateFrame() { updateZipFile = new File(System.getProperty("user.dir")+File.separator+"update"+File.separator+"update.zip"); userDir = new File(System.getProperty("user.dir")); updateDir = new File(System.getProperty("user.dir")+File.separator+"update"); initComponents(); if(updateDir.exists()){ if(updateDir.isDirectory()){ setLocationRelativeTo(null); setIconImage(icon); this.progressBar.setValue((progressBar.getMaximum()/2)); backup = backup(); unzip(updateZipFile.getAbsolutePath(),userDir.getAbsolutePath()); deleteTemp(updateDir); } }else{ dispose(); } } /** * Delete temp folder. * @param f */ private void deleteTemp(File f){ try { moveFile(updateDir.listFiles(),userDir); FileUtils.forceDelete(new File(userDir.getAbsolutePath()+File.separator+"update.zip")); FileUtils.deleteDirectory(f); new Msg(this,"Update has been completed. Please restart DomainMath IDE."); System.exit(0); } catch (IOException ex) { Logger.getLogger(UpdateFrame.class.getName()).log(Level.SEVERE, null, ex); } } /** * 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() { progressBar = new javax.swing.JProgressBar(); fileLabel = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("DomainMath IDE"); setResizable(false); fileLabel.setText("Back up..."); jPanel1.setBackground(new java.awt.Color(255, 102, 51)); jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 18)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setText("Please Wait..."); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(fileLabel) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); 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) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE) .addComponent(fileLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * @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 form */ java.awt.EventQueue.invokeLater(() -> { new UpdateFrame().setVisible(true); }); } public java.net.URL imgURL = getClass().getResource("/org/domainmath/gui/update/resources/DomainMath.png"); public Image icon = Toolkit.getDefaultToolkit().getImage(imgURL); // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel fileLabel; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JProgressBar progressBar; // End of variables declaration//GEN-END:variables /** * Back up contents to a folder. * @return backup folder */ private File backup() { // make an unique folder to store backup files. Long n = new Random().nextLong(); File backupDir = new File(System.getProperty("user.dir")+File.separator+"backup_"+n.toString()); backupDir.mkdir(); // get files list. List<File> fileList = getFileList(new File(System.getProperty("user.dir")+File.separator+"files_backup.ini")); // backup. for(File file:fileList){ // copy folders. if(file.isDirectory()){ try { FileUtils.copyDirectoryToDirectory(file, backupDir); FileUtils.deleteDirectory(file); } catch (IOException ex) { } }else if(file.isFile()) { try { FileUtils.copyFileToDirectory(file, backupDir); FileUtils.forceDelete(file); } catch (IOException ex) { } } } return backupDir; } /** * Move files to src dir. * @param file_list * @param srcDir */ private void moveFile(File[] file_list ,File srcDir){ for(File file:file_list){ // copy folders. if(file.isDirectory()){ try { FileUtils.copyDirectoryToDirectory(file, srcDir); FileUtils.deleteDirectory(file); } catch (IOException ex) { } }else if(file.isFile()) { try { FileUtils.copyFileToDirectory(file, srcDir); FileUtils.forceDelete(file); } catch (IOException ex) { } } } } private List<File> getFileList(File datafile) { List<File> data = Collections.synchronizedList(new ArrayList()); String line; try { FileInputStream fin = new FileInputStream(datafile); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); try { while((line=br.readLine()) != null) { StringTokenizer s2 = new StringTokenizer(line,"\n"); while(s2.hasMoreTokens()) { data.add(new File(System.getProperty("user.dir")+File.separator+s2.nextToken())); } } br.close(); } catch (IOException ex) { dispose(); } } catch (FileNotFoundException ex) { String error= "You can not continue update process,you" + "are using older version of DomainMath IDE.You can download latest" + "version from Internet."; new Error(this,"Unable to update.",error); dispose(); } return data; } private void unzip(String src, String des) { try { System.out.println(src+"-"+des); ZipFile zipFile = new ZipFile(src); zipFile.extractAll(des); this.fileLabel.setText("Completed"); this.progressBar.setValue(progressBar.getMaximum()); } catch (ZipException e) { new Error(this,"Unable to update.","Unable to extract downloaded files."); e.printStackTrace(); dispose(); } } }