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.JOptionPane;
|
---|
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 {
|
---|
82 | ApplicationInvoker.execute(EExeType.JAR, null, aei, null);
|
---|
83 | } catch (FileNotFoundException e) {
|
---|
84 | // TODO Auto-generated catch block
|
---|
85 | e.printStackTrace();
|
---|
86 | } catch (ERuntimeNotInstalledException e) {
|
---|
87 | // TODO Auto-generated catch block
|
---|
88 | e.printStackTrace();
|
---|
89 | }
|
---|
90 | }
|
---|
91 | dispose();
|
---|
92 | }
|
---|
93 |
|
---|
94 | class Status {
|
---|
95 | public Status(int done, int total) {
|
---|
96 | this.done = done;
|
---|
97 | this.total = total;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Steps done
|
---|
102 | */
|
---|
103 | public int done;
|
---|
104 | /**
|
---|
105 | * Steps in total to do
|
---|
106 | */
|
---|
107 | public int total;
|
---|
108 | }
|
---|
109 |
|
---|
110 | class Updater extends SwingWorker<Status, Status> {
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | protected Status doInBackground() throws Exception {
|
---|
114 | step.setText("Waiting for AEI to close");
|
---|
115 | int i = 0;
|
---|
116 | while (!checkWritable() && i < 20) {
|
---|
117 | i++;
|
---|
118 | Thread.sleep(500);
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (i >= 20) {
|
---|
122 | JOptionPane
|
---|
123 | .showMessageDialog(
|
---|
124 | null,
|
---|
125 | "Could not update because the main file of AEI was locked.\nPerhaps you are still running an instance of AEI?",
|
---|
126 | "Could not update!", JOptionPane.ERROR_MESSAGE);
|
---|
127 | System.exit(1);
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | step.setText("Updating");
|
---|
132 |
|
---|
133 | SVN svn = new SVN();
|
---|
134 | try {
|
---|
135 | int x = svn.checkSVN("http://svn.aei.oni2.net",
|
---|
136 | new File(Paths.getPrefsPath(), "bin"));
|
---|
137 | if (x != 0) {
|
---|
138 | // Update available, missing files or no WC yet
|
---|
139 | svn.updateWC("http://svn.aei.oni2.net",
|
---|
140 | new File(Paths.getPrefsPath(), "bin"),
|
---|
141 | new SVNUpdateListener() {
|
---|
142 | public void statusUpdate(int done, int total) {
|
---|
143 | publish(new Status(done, total));
|
---|
144 | }
|
---|
145 | });
|
---|
146 | }
|
---|
147 | } catch (Exception e) {
|
---|
148 | e.printStackTrace();
|
---|
149 | }
|
---|
150 | return null;
|
---|
151 | }
|
---|
152 |
|
---|
153 | private boolean checkWritable() {
|
---|
154 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
155 | "AEInstaller2.jar");
|
---|
156 | File temp = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
157 | "temp.jar");
|
---|
158 | if (!aei.exists())
|
---|
159 | return true;
|
---|
160 | if (aei.renameTo(temp)) {
|
---|
161 | temp.renameTo(aei);
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | @Override
|
---|
168 | protected void process(List<Status> chunks) {
|
---|
169 | super.process(chunks);
|
---|
170 | if (chunks.size() > 0) {
|
---|
171 | Status s = chunks.get(chunks.size() - 1);
|
---|
172 | bar.setValue(s.done);
|
---|
173 | bar.setMaximum(s.total);
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | protected void done() {
|
---|
179 | super.done();
|
---|
180 | step.setText("AEI is up to date");
|
---|
181 | bar.setValue(1);
|
---|
182 | bar.setMaximum(1);
|
---|
183 | closeBtn.setEnabled(true);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|