source: java/AEInstaller2-Updater/src/net/oni2/aeinstaller/updater/gui/MainWin.java

Last change on this file was 1134, checked in by alloc, 4 years ago

Updater with a few more log lines

File size: 5.9 KB
Line 
1package net.oni2.aeinstaller.updater.gui;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.WindowAdapter;
8import java.awt.event.WindowEvent;
9import java.io.File;
10import java.io.FileNotFoundException;
11import java.util.List;
12
13import javax.swing.JButton;
14import javax.swing.JFrame;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JProgressBar;
18import javax.swing.SwingWorker;
19
20import net.oni2.aeinstaller.updater.backend.Paths;
21import net.oni2.platformtools.applicationinvoker.ApplicationInvoker;
22import net.oni2.platformtools.applicationinvoker.EExeType;
23import net.oni2.platformtools.applicationinvoker.ERuntimeNotInstalledException;
24import net.oni2.svnaccess.SVN;
25import net.oni2.svnaccess.SVNUpdateListener;
26
27/**
28 * @author Christian Illy
29 */
30public 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 System.out.println("AEI2 updater version 1.4");
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 {
83 ApplicationInvoker
84 .execute(EExeType.JAR, null, aei, null, false);
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 }
93
94 System.out.println("Closing AEI updater");
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> {
115 protected void setStep(String text) {
116 step.setText(text);
117 System.out.println(text);
118 }
119
120 @Override
121 protected Status doInBackground() throws Exception {
122 setStep("Waiting for AEI to close");
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);
135 System.out.println("Could not update because the main file of AEI was locked.");
136 System.exit(1);
137 return null;
138 }
139
140 setStep("Updating");
141
142 SVN svn = new SVN();
143 try {
144 boolean showError = false;
145
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);
149 switch (x) {
150 case -2: // No repos connection
151 System.out.println("Error: No repository connection");
152 showError = true;
153 break;
154 case 0: // Repos up to date
155 System.out.println("AEI up to date");
156 break;
157 case -1:// no WC yet
158 case 1:// Update available
159 case 2:// missing files
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",
172 wcDir,
173 new SVNUpdateListener() {
174 public void statusUpdate(int done, int total) {
175 publish(new Status(done, total));
176 }
177 });
178 if (showError) {
179 System.out.println("Error: Updating failed!");
180 }
181 break;
182 }
183 if (showError) {
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 }
192 } catch (Exception e) {
193 e.printStackTrace();
194 }
195 return null;
196 }
197
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
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();
225 step.setText("AEI is up to date");
226 bar.setValue(1);
227 bar.setMaximum(1);
228 closeBtn.setEnabled(true);
229 }
230 }
231
232}
Note: See TracBrowser for help on using the repository browser.