[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");
|
---|
[1134] | 42 | System.out.println("AEI2 updater version 1.4");
|
---|
[734] | 43 | setLayout(new BorderLayout(2, 4));
|
---|
| 44 |
|
---|
| 45 | bar.setPreferredSize(new Dimension(250, 16));
|
---|
| 46 | closeBtn.setEnabled(false);
|
---|
| 47 |
|
---|
| 48 | add(bar, BorderLayout.CENTER);
|
---|
| 49 | add(step, BorderLayout.NORTH);
|
---|
| 50 | add(closeBtn, BorderLayout.SOUTH);
|
---|
| 51 |
|
---|
| 52 | setResizable(false);
|
---|
| 53 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
| 54 | pack();
|
---|
| 55 | setLocationRelativeTo(null);
|
---|
| 56 |
|
---|
| 57 | closeBtn.addActionListener(new ActionListener() {
|
---|
| 58 | @Override
|
---|
| 59 | public void actionPerformed(ActionEvent e) {
|
---|
| 60 | exit();
|
---|
| 61 | }
|
---|
| 62 | });
|
---|
| 63 |
|
---|
| 64 | addWindowListener(new WindowAdapter() {
|
---|
| 65 | @Override
|
---|
| 66 | public void windowClosing(WindowEvent e) {
|
---|
| 67 | super.windowClosing(e);
|
---|
| 68 | if (closeBtn.isEnabled()) {
|
---|
| 69 | exit();
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | Updater upd = new Updater();
|
---|
| 75 | upd.execute();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | private void exit() {
|
---|
| 79 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 80 | "AEInstaller2.jar");
|
---|
| 81 | if (aei.exists()) {
|
---|
| 82 | try {
|
---|
[767] | 83 | ApplicationInvoker
|
---|
| 84 | .execute(EExeType.JAR, null, aei, null, false);
|
---|
[734] | 85 | } catch (FileNotFoundException e) {
|
---|
| 86 | // TODO Auto-generated catch block
|
---|
| 87 | e.printStackTrace();
|
---|
| 88 | } catch (ERuntimeNotInstalledException e) {
|
---|
| 89 | // TODO Auto-generated catch block
|
---|
| 90 | e.printStackTrace();
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[1078] | 93 |
|
---|
| 94 | System.out.println("Closing AEI updater");
|
---|
[734] | 95 | dispose();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | class Status {
|
---|
| 99 | public Status(int done, int total) {
|
---|
| 100 | this.done = done;
|
---|
| 101 | this.total = total;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Steps done
|
---|
| 106 | */
|
---|
| 107 | public int done;
|
---|
| 108 | /**
|
---|
| 109 | * Steps in total to do
|
---|
| 110 | */
|
---|
| 111 | public int total;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | class Updater extends SwingWorker<Status, Status> {
|
---|
[1078] | 115 | protected void setStep(String text) {
|
---|
| 116 | step.setText(text);
|
---|
| 117 | System.out.println(text);
|
---|
| 118 | }
|
---|
[734] | 119 |
|
---|
| 120 | @Override
|
---|
| 121 | protected Status doInBackground() throws Exception {
|
---|
[1078] | 122 | setStep("Waiting for AEI to close");
|
---|
[756] | 123 | int i = 0;
|
---|
| 124 | while (!checkWritable() && i < 20) {
|
---|
| 125 | i++;
|
---|
| 126 | Thread.sleep(500);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (i >= 20) {
|
---|
| 130 | JOptionPane
|
---|
| 131 | .showMessageDialog(
|
---|
| 132 | null,
|
---|
| 133 | "Could not update because the main file of AEI was locked.\nPerhaps you are still running an instance of AEI?",
|
---|
| 134 | "Could not update!", JOptionPane.ERROR_MESSAGE);
|
---|
[1078] | 135 | System.out.println("Could not update because the main file of AEI was locked.");
|
---|
[756] | 136 | System.exit(1);
|
---|
| 137 | return null;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1078] | 140 | setStep("Updating");
|
---|
[756] | 141 |
|
---|
[734] | 142 | SVN svn = new SVN();
|
---|
| 143 | try {
|
---|
[1078] | 144 | boolean showError = false;
|
---|
| 145 |
|
---|
[1134] | 146 | File wcDir = new File(Paths.getPrefsPath(), "bin");
|
---|
| 147 | System.out.println("AEI WC path: " + wcDir.getAbsolutePath());
|
---|
| 148 | int x = svn.checkSVN("http://svn.aei.oni2.net", wcDir);
|
---|
[853] | 149 | switch (x) {
|
---|
| 150 | case -2: // No repos connection
|
---|
[1078] | 151 | System.out.println("Error: No repository connection");
|
---|
| 152 | showError = true;
|
---|
[853] | 153 | break;
|
---|
| 154 | case 0: // Repos up to date
|
---|
[1078] | 155 | System.out.println("AEI up to date");
|
---|
[853] | 156 | break;
|
---|
| 157 | case -1:// no WC yet
|
---|
| 158 | case 1:// Update available
|
---|
| 159 | case 2:// missing files
|
---|
[1078] | 160 | switch (x) {
|
---|
| 161 | case -1:
|
---|
| 162 | System.out.println("No working copy created so far");
|
---|
| 163 | break;
|
---|
| 164 | case 1:
|
---|
| 165 | System.out.println("Updating to HEAD");
|
---|
| 166 | break;
|
---|
| 167 | case 2:
|
---|
| 168 | System.out.println("Updating for missing files");
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | showError = !svn.updateWC("http://svn.aei.oni2.net",
|
---|
[1134] | 172 | wcDir,
|
---|
[853] | 173 | new SVNUpdateListener() {
|
---|
| 174 | public void statusUpdate(int done, int total) {
|
---|
| 175 | publish(new Status(done, total));
|
---|
| 176 | }
|
---|
| 177 | });
|
---|
[1078] | 178 | if (showError) {
|
---|
| 179 | System.out.println("Error: Updating failed!");
|
---|
| 180 | }
|
---|
[853] | 181 | break;
|
---|
[734] | 182 | }
|
---|
[1078] | 183 | if (showError) {
|
---|
[853] | 184 | JOptionPane
|
---|
| 185 | .showMessageDialog(
|
---|
| 186 | null,
|
---|
| 187 | "Perhaps you don't have a internet connection right now or\nyou have (not) entered (wrong) proxy data?",
|
---|
| 188 | "Updating AEI failed!",
|
---|
| 189 | JOptionPane.ERROR_MESSAGE);
|
---|
| 190 | System.exit(1);
|
---|
| 191 | }
|
---|
[734] | 192 | } catch (Exception e) {
|
---|
| 193 | e.printStackTrace();
|
---|
| 194 | }
|
---|
| 195 | return null;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[756] | 198 | private boolean checkWritable() {
|
---|
| 199 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 200 | "AEInstaller2.jar");
|
---|
| 201 | File temp = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 202 | "temp.jar");
|
---|
| 203 | if (!aei.exists())
|
---|
| 204 | return true;
|
---|
| 205 | if (aei.renameTo(temp)) {
|
---|
| 206 | temp.renameTo(aei);
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|
| 209 | return false;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[734] | 212 | @Override
|
---|
| 213 | protected void process(List<Status> chunks) {
|
---|
| 214 | super.process(chunks);
|
---|
| 215 | if (chunks.size() > 0) {
|
---|
| 216 | Status s = chunks.get(chunks.size() - 1);
|
---|
| 217 | bar.setValue(s.done);
|
---|
| 218 | bar.setMaximum(s.total);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | @Override
|
---|
| 223 | protected void done() {
|
---|
| 224 | super.done();
|
---|
[1078] | 225 | step.setText("AEI is up to date");
|
---|
| 226 | bar.setValue(1);
|
---|
| 227 | bar.setMaximum(1);
|
---|
| 228 | closeBtn.setEnabled(true);
|
---|
[734] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | }
|
---|