[734] | 1 | package net.oni2.aeinstaller.updater.gui;
|
---|
| 2 |
|
---|
| 3 | import java.awt.BorderLayout;
|
---|
| 4 | import java.awt.Dimension;
|
---|
| 5 | import java.awt.event.ActionEvent;
|
---|
| 6 | import java.awt.event.ActionListener;
|
---|
| 7 | import java.awt.event.WindowAdapter;
|
---|
| 8 | import java.awt.event.WindowEvent;
|
---|
| 9 | import java.io.File;
|
---|
| 10 | import java.io.FileNotFoundException;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | import javax.swing.JButton;
|
---|
| 14 | import javax.swing.JFrame;
|
---|
| 15 | import javax.swing.JLabel;
|
---|
[756] | 16 | import javax.swing.JOptionPane;
|
---|
[734] | 17 | import javax.swing.JProgressBar;
|
---|
| 18 | import javax.swing.SwingWorker;
|
---|
| 19 |
|
---|
| 20 | import net.oni2.aeinstaller.updater.backend.Paths;
|
---|
| 21 | import net.oni2.platformtools.applicationinvoker.ApplicationInvoker;
|
---|
| 22 | import net.oni2.platformtools.applicationinvoker.EExeType;
|
---|
| 23 | import net.oni2.platformtools.applicationinvoker.ERuntimeNotInstalledException;
|
---|
| 24 | import net.oni2.svnaccess.SVN;
|
---|
| 25 | import net.oni2.svnaccess.SVNUpdateListener;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * @author Christian Illy
|
---|
| 29 | */
|
---|
| 30 | public class MainWin extends JFrame {
|
---|
| 31 | private static final long serialVersionUID = -3653187495409881426L;
|
---|
| 32 |
|
---|
| 33 | JLabel step = new JLabel("Preparing");
|
---|
| 34 | JProgressBar bar = new JProgressBar(0, 1);
|
---|
| 35 | JButton closeBtn = new JButton("Close and launch AEI");
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Constructor of main window.
|
---|
| 39 | */
|
---|
| 40 | public MainWin() {
|
---|
| 41 | super("AEInstaller2 self updater");
|
---|
| 42 | setLayout(new BorderLayout(2, 4));
|
---|
| 43 |
|
---|
| 44 | bar.setPreferredSize(new Dimension(250, 16));
|
---|
| 45 | closeBtn.setEnabled(false);
|
---|
| 46 |
|
---|
| 47 | add(bar, BorderLayout.CENTER);
|
---|
| 48 | add(step, BorderLayout.NORTH);
|
---|
| 49 | add(closeBtn, BorderLayout.SOUTH);
|
---|
| 50 |
|
---|
| 51 | setResizable(false);
|
---|
| 52 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
| 53 | pack();
|
---|
| 54 | setLocationRelativeTo(null);
|
---|
| 55 |
|
---|
| 56 | closeBtn.addActionListener(new ActionListener() {
|
---|
| 57 | @Override
|
---|
| 58 | public void actionPerformed(ActionEvent e) {
|
---|
| 59 | exit();
|
---|
| 60 | }
|
---|
| 61 | });
|
---|
| 62 |
|
---|
| 63 | addWindowListener(new WindowAdapter() {
|
---|
| 64 | @Override
|
---|
| 65 | public void windowClosing(WindowEvent e) {
|
---|
| 66 | super.windowClosing(e);
|
---|
| 67 | if (closeBtn.isEnabled()) {
|
---|
| 68 | exit();
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 | Updater upd = new Updater();
|
---|
| 74 | upd.execute();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void exit() {
|
---|
| 78 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 79 | "AEInstaller2.jar");
|
---|
| 80 | if (aei.exists()) {
|
---|
| 81 | try {
|
---|
[767] | 82 | ApplicationInvoker
|
---|
| 83 | .execute(EExeType.JAR, null, aei, null, false);
|
---|
[734] | 84 | } catch (FileNotFoundException e) {
|
---|
| 85 | // TODO Auto-generated catch block
|
---|
| 86 | e.printStackTrace();
|
---|
| 87 | } catch (ERuntimeNotInstalledException e) {
|
---|
| 88 | // TODO Auto-generated catch block
|
---|
| 89 | e.printStackTrace();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | dispose();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | class Status {
|
---|
| 96 | public Status(int done, int total) {
|
---|
| 97 | this.done = done;
|
---|
| 98 | this.total = total;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Steps done
|
---|
| 103 | */
|
---|
| 104 | public int done;
|
---|
| 105 | /**
|
---|
| 106 | * Steps in total to do
|
---|
| 107 | */
|
---|
| 108 | public int total;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | class Updater extends SwingWorker<Status, Status> {
|
---|
| 112 |
|
---|
| 113 | @Override
|
---|
| 114 | protected Status doInBackground() throws Exception {
|
---|
[756] | 115 | step.setText("Waiting for AEI to close");
|
---|
| 116 | int i = 0;
|
---|
| 117 | while (!checkWritable() && i < 20) {
|
---|
| 118 | i++;
|
---|
| 119 | Thread.sleep(500);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | if (i >= 20) {
|
---|
| 123 | JOptionPane
|
---|
| 124 | .showMessageDialog(
|
---|
| 125 | null,
|
---|
| 126 | "Could not update because the main file of AEI was locked.\nPerhaps you are still running an instance of AEI?",
|
---|
| 127 | "Could not update!", JOptionPane.ERROR_MESSAGE);
|
---|
| 128 | System.exit(1);
|
---|
| 129 | return null;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[734] | 132 | step.setText("Updating");
|
---|
[756] | 133 |
|
---|
[734] | 134 | SVN svn = new SVN();
|
---|
| 135 | try {
|
---|
| 136 | int x = svn.checkSVN("http://svn.aei.oni2.net",
|
---|
| 137 | new File(Paths.getPrefsPath(), "bin"));
|
---|
| 138 | if (x != 0) {
|
---|
[765] | 139 | // Update available, missing files or no WC yet
|
---|
[734] | 140 | svn.updateWC("http://svn.aei.oni2.net",
|
---|
| 141 | new File(Paths.getPrefsPath(), "bin"),
|
---|
| 142 | new SVNUpdateListener() {
|
---|
| 143 | public void statusUpdate(int done, int total) {
|
---|
| 144 | publish(new Status(done, total));
|
---|
| 145 | }
|
---|
| 146 | });
|
---|
| 147 | }
|
---|
| 148 | } catch (Exception e) {
|
---|
| 149 | e.printStackTrace();
|
---|
| 150 | }
|
---|
| 151 | return null;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[756] | 154 | private boolean checkWritable() {
|
---|
| 155 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 156 | "AEInstaller2.jar");
|
---|
| 157 | File temp = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 158 | "temp.jar");
|
---|
| 159 | if (!aei.exists())
|
---|
| 160 | return true;
|
---|
| 161 | if (aei.renameTo(temp)) {
|
---|
| 162 | temp.renameTo(aei);
|
---|
| 163 | return true;
|
---|
| 164 | }
|
---|
| 165 | return false;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[734] | 168 | @Override
|
---|
| 169 | protected void process(List<Status> chunks) {
|
---|
| 170 | super.process(chunks);
|
---|
| 171 | if (chunks.size() > 0) {
|
---|
| 172 | Status s = chunks.get(chunks.size() - 1);
|
---|
| 173 | bar.setValue(s.done);
|
---|
| 174 | bar.setMaximum(s.total);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | @Override
|
---|
| 179 | protected void done() {
|
---|
| 180 | super.done();
|
---|
| 181 | step.setText("AEI is up to date");
|
---|
| 182 | bar.setValue(1);
|
---|
| 183 | bar.setMaximum(1);
|
---|
| 184 | closeBtn.setEnabled(true);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | }
|
---|