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
|
---|
83 | .execute(EExeType.JAR, null, aei, null, false);
|
---|
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 | boolean uptodate = false;
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | protected Status doInBackground() throws Exception {
|
---|
116 | step.setText("Waiting for AEI to close");
|
---|
117 | int i = 0;
|
---|
118 | while (!checkWritable() && i < 20) {
|
---|
119 | i++;
|
---|
120 | Thread.sleep(500);
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (i >= 20) {
|
---|
124 | JOptionPane
|
---|
125 | .showMessageDialog(
|
---|
126 | null,
|
---|
127 | "Could not update because the main file of AEI was locked.\nPerhaps you are still running an instance of AEI?",
|
---|
128 | "Could not update!", JOptionPane.ERROR_MESSAGE);
|
---|
129 | System.exit(1);
|
---|
130 | return null;
|
---|
131 | }
|
---|
132 |
|
---|
133 | step.setText("Updating");
|
---|
134 |
|
---|
135 | SVN svn = new SVN();
|
---|
136 | try {
|
---|
137 | int x = svn.checkSVN("http://svn.aei.oni2.net",
|
---|
138 | new File(Paths.getPrefsPath(), "bin"));
|
---|
139 | switch (x) {
|
---|
140 | case -2: // No repos connection
|
---|
141 | break;
|
---|
142 | case 0: // Repos up to date
|
---|
143 | uptodate = true;
|
---|
144 | break;
|
---|
145 | case -1:// no WC yet
|
---|
146 | case 1:// Update available
|
---|
147 | case 2:// missing files
|
---|
148 | uptodate = svn.updateWC("http://svn.aei.oni2.net",
|
---|
149 | new File(Paths.getPrefsPath(), "bin"),
|
---|
150 | new SVNUpdateListener() {
|
---|
151 | public void statusUpdate(int done, int total) {
|
---|
152 | publish(new Status(done, total));
|
---|
153 | }
|
---|
154 | });
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | if (!uptodate) {
|
---|
158 | JOptionPane
|
---|
159 | .showMessageDialog(
|
---|
160 | null,
|
---|
161 | "Perhaps you don't have a internet connection right now or\nyou have (not) entered (wrong) proxy data?",
|
---|
162 | "Updating AEI failed!",
|
---|
163 | JOptionPane.ERROR_MESSAGE);
|
---|
164 | System.exit(1);
|
---|
165 | }
|
---|
166 | } catch (Exception e) {
|
---|
167 | e.printStackTrace();
|
---|
168 | }
|
---|
169 | return null;
|
---|
170 | }
|
---|
171 |
|
---|
172 | private boolean checkWritable() {
|
---|
173 | File aei = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
174 | "AEInstaller2.jar");
|
---|
175 | File temp = new File(new File(Paths.getInstallerPath(), "bin"),
|
---|
176 | "temp.jar");
|
---|
177 | if (!aei.exists())
|
---|
178 | return true;
|
---|
179 | if (aei.renameTo(temp)) {
|
---|
180 | temp.renameTo(aei);
|
---|
181 | return true;
|
---|
182 | }
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | protected void process(List<Status> chunks) {
|
---|
188 | super.process(chunks);
|
---|
189 | if (chunks.size() > 0) {
|
---|
190 | Status s = chunks.get(chunks.size() - 1);
|
---|
191 | bar.setValue(s.done);
|
---|
192 | bar.setMaximum(s.total);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | protected void done() {
|
---|
198 | super.done();
|
---|
199 | if (uptodate) {
|
---|
200 | step.setText("AEI is up to date");
|
---|
201 | bar.setValue(1);
|
---|
202 | bar.setMaximum(1);
|
---|
203 | closeBtn.setEnabled(true);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|