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

Last change on this file since 1014 was 1014, checked in by alloc, 10 years ago

AEI2U 1.2

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