[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");
|
---|
[1014] | 42 | System.out.println("AEI2 updater version 1.2");
|
---|
[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 |
|
---|
[734] | 146 | int x = svn.checkSVN("http://svn.aei.oni2.net",
|
---|
| 147 | new File(Paths.getPrefsPath(), "bin"));
|
---|
[853] | 148 | switch (x) {
|
---|
| 149 | case -2: // No repos connection
|
---|
[1078] | 150 | System.out.println("Error: No repository connection");
|
---|
| 151 | showError = true;
|
---|
[853] | 152 | break;
|
---|
| 153 | case 0: // Repos up to date
|
---|
[1078] | 154 | System.out.println("AEI up to date");
|
---|
[853] | 155 | break;
|
---|
| 156 | case -1:// no WC yet
|
---|
| 157 | case 1:// Update available
|
---|
| 158 | case 2:// missing files
|
---|
[1078] | 159 | switch (x) {
|
---|
| 160 | case -1:
|
---|
| 161 | System.out.println("No working copy created so far");
|
---|
| 162 | break;
|
---|
| 163 | case 1:
|
---|
| 164 | System.out.println("Updating to HEAD");
|
---|
| 165 | break;
|
---|
| 166 | case 2:
|
---|
| 167 | System.out.println("Updating for missing files");
|
---|
| 168 | break;
|
---|
| 169 | }
|
---|
| 170 | showError = !svn.updateWC("http://svn.aei.oni2.net",
|
---|
[853] | 171 | new File(Paths.getPrefsPath(), "bin"),
|
---|
| 172 | new SVNUpdateListener() {
|
---|
| 173 | public void statusUpdate(int done, int total) {
|
---|
| 174 | publish(new Status(done, total));
|
---|
| 175 | }
|
---|
| 176 | });
|
---|
[1078] | 177 | if (showError) {
|
---|
| 178 | System.out.println("Error: Updating failed!");
|
---|
| 179 | }
|
---|
[853] | 180 | break;
|
---|
[734] | 181 | }
|
---|
[1078] | 182 | if (showError) {
|
---|
[853] | 183 | JOptionPane
|
---|
| 184 | .showMessageDialog(
|
---|
| 185 | null,
|
---|
| 186 | "Perhaps you don't have a internet connection right now or\nyou have (not) entered (wrong) proxy data?",
|
---|
| 187 | "Updating AEI failed!",
|
---|
| 188 | JOptionPane.ERROR_MESSAGE);
|
---|
| 189 | System.exit(1);
|
---|
| 190 | }
|
---|
[734] | 191 | } catch (Exception e) {
|
---|
| 192 | e.printStackTrace();
|
---|
| 193 | }
|
---|
| 194 | return null;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[756] | 197 | private boolean checkWritable() {
|
---|
| 198 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 199 | "AEInstaller2.jar");
|
---|
| 200 | File temp = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 201 | "temp.jar");
|
---|
| 202 | if (!aei.exists())
|
---|
| 203 | return true;
|
---|
| 204 | if (aei.renameTo(temp)) {
|
---|
| 205 | temp.renameTo(aei);
|
---|
| 206 | return true;
|
---|
| 207 | }
|
---|
| 208 | return false;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[734] | 211 | @Override
|
---|
| 212 | protected void process(List<Status> chunks) {
|
---|
| 213 | super.process(chunks);
|
---|
| 214 | if (chunks.size() > 0) {
|
---|
| 215 | Status s = chunks.get(chunks.size() - 1);
|
---|
| 216 | bar.setValue(s.done);
|
---|
| 217 | bar.setMaximum(s.total);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | @Override
|
---|
| 222 | protected void done() {
|
---|
| 223 | super.done();
|
---|
[1078] | 224 | step.setText("AEI is up to date");
|
---|
| 225 | bar.setValue(1);
|
---|
| 226 | bar.setMaximum(1);
|
---|
| 227 | closeBtn.setEnabled(true);
|
---|
[734] | 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | }
|
---|