[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;
|
---|
| 16 | import javax.swing.JProgressBar;
|
---|
| 17 | import javax.swing.SwingWorker;
|
---|
| 18 |
|
---|
| 19 | import net.oni2.aeinstaller.updater.backend.Paths;
|
---|
| 20 | import net.oni2.platformtools.applicationinvoker.ApplicationInvoker;
|
---|
| 21 | import net.oni2.platformtools.applicationinvoker.EExeType;
|
---|
| 22 | import net.oni2.platformtools.applicationinvoker.ERuntimeNotInstalledException;
|
---|
| 23 | import net.oni2.svnaccess.SVN;
|
---|
| 24 | import net.oni2.svnaccess.SVNUpdateListener;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * @author Christian Illy
|
---|
| 28 | */
|
---|
| 29 | public class MainWin extends JFrame {
|
---|
| 30 | private static final long serialVersionUID = -3653187495409881426L;
|
---|
| 31 |
|
---|
| 32 | JLabel step = new JLabel("Preparing");
|
---|
| 33 | JProgressBar bar = new JProgressBar(0, 1);
|
---|
| 34 | JButton closeBtn = new JButton("Close and launch AEI");
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Constructor of main window.
|
---|
| 38 | */
|
---|
| 39 | public MainWin() {
|
---|
| 40 | super("AEInstaller2 self updater");
|
---|
| 41 | setLayout(new BorderLayout(2, 4));
|
---|
| 42 |
|
---|
| 43 | bar.setPreferredSize(new Dimension(250, 16));
|
---|
| 44 | closeBtn.setEnabled(false);
|
---|
| 45 |
|
---|
| 46 | add(bar, BorderLayout.CENTER);
|
---|
| 47 | add(step, BorderLayout.NORTH);
|
---|
| 48 | add(closeBtn, BorderLayout.SOUTH);
|
---|
| 49 |
|
---|
| 50 | setResizable(false);
|
---|
| 51 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
| 52 | pack();
|
---|
| 53 | setLocationRelativeTo(null);
|
---|
| 54 |
|
---|
| 55 | closeBtn.addActionListener(new ActionListener() {
|
---|
| 56 | @Override
|
---|
| 57 | public void actionPerformed(ActionEvent e) {
|
---|
| 58 | exit();
|
---|
| 59 | }
|
---|
| 60 | });
|
---|
| 61 |
|
---|
| 62 | addWindowListener(new WindowAdapter() {
|
---|
| 63 | @Override
|
---|
| 64 | public void windowClosing(WindowEvent e) {
|
---|
| 65 | super.windowClosing(e);
|
---|
| 66 | if (closeBtn.isEnabled()) {
|
---|
| 67 | exit();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | });
|
---|
| 71 |
|
---|
| 72 | Updater upd = new Updater();
|
---|
| 73 | upd.execute();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | private void exit() {
|
---|
| 77 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
| 78 | "AEInstaller2.jar");
|
---|
| 79 | if (aei.exists()) {
|
---|
| 80 | try {
|
---|
| 81 | ApplicationInvoker.execute(EExeType.JAR, null, aei, null);
|
---|
| 82 | } catch (FileNotFoundException e) {
|
---|
| 83 | // TODO Auto-generated catch block
|
---|
| 84 | e.printStackTrace();
|
---|
| 85 | } catch (ERuntimeNotInstalledException e) {
|
---|
| 86 | // TODO Auto-generated catch block
|
---|
| 87 | e.printStackTrace();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | dispose();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | class Status {
|
---|
| 94 | public Status(int done, int total) {
|
---|
| 95 | this.done = done;
|
---|
| 96 | this.total = total;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Steps done
|
---|
| 101 | */
|
---|
| 102 | public int done;
|
---|
| 103 | /**
|
---|
| 104 | * Steps in total to do
|
---|
| 105 | */
|
---|
| 106 | public int total;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | class Updater extends SwingWorker<Status, Status> {
|
---|
| 110 |
|
---|
| 111 | @Override
|
---|
| 112 | protected Status doInBackground() throws Exception {
|
---|
| 113 | Thread.sleep(2000);
|
---|
| 114 | step.setText("Updating");
|
---|
| 115 |
|
---|
| 116 | SVN svn = new SVN();
|
---|
| 117 | try {
|
---|
| 118 | int x = svn.checkSVN("http://svn.aei.oni2.net",
|
---|
| 119 | new File(Paths.getPrefsPath(), "bin"));
|
---|
| 120 | if (x != 0) {
|
---|
| 121 | // Update available or no WC yet
|
---|
| 122 | svn.updateWC("http://svn.aei.oni2.net",
|
---|
| 123 | new File(Paths.getPrefsPath(), "bin"),
|
---|
| 124 | new SVNUpdateListener() {
|
---|
| 125 | public void statusUpdate(int done, int total) {
|
---|
| 126 | publish(new Status(done, total));
|
---|
| 127 | }
|
---|
| 128 | });
|
---|
| 129 | }
|
---|
| 130 | } catch (Exception e) {
|
---|
| 131 | e.printStackTrace();
|
---|
| 132 | }
|
---|
| 133 | return null;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | @Override
|
---|
| 137 | protected void process(List<Status> chunks) {
|
---|
| 138 | super.process(chunks);
|
---|
| 139 | if (chunks.size() > 0) {
|
---|
| 140 | Status s = chunks.get(chunks.size() - 1);
|
---|
| 141 | bar.setValue(s.done);
|
---|
| 142 | bar.setMaximum(s.total);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | @Override
|
---|
| 147 | protected void done() {
|
---|
| 148 | super.done();
|
---|
| 149 | step.setText("AEI is up to date");
|
---|
| 150 | bar.setValue(1);
|
---|
| 151 | bar.setMaximum(1);
|
---|
| 152 | closeBtn.setEnabled(true);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | }
|
---|