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

Last change on this file since 798 was 767, checked in by alloc, 12 years ago

AEI2 0.99y:

  • Perhaps fixes launching Oni on MacOS
File size: 4.6 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 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
113 @Override
114 protected Status doInBackground() throws Exception {
115 step.setText("Waiting for AEI to close");
116 int i = 0;
117 while (!checkWritable() && i < 20) {
118 i++;
119 Thread.sleep(500);
120 }
121
122 if (i >= 20) {
123 JOptionPane
124 .showMessageDialog(
125 null,
126 "Could not update because the main file of AEI was locked.\nPerhaps you are still running an instance of AEI?",
127 "Could not update!", JOptionPane.ERROR_MESSAGE);
128 System.exit(1);
129 return null;
130 }
131
132 step.setText("Updating");
133
134 SVN svn = new SVN();
135 try {
136 int x = svn.checkSVN("http://svn.aei.oni2.net",
137 new File(Paths.getPrefsPath(), "bin"));
138 if (x != 0) {
139 // Update available, missing files or no WC yet
140 svn.updateWC("http://svn.aei.oni2.net",
141 new File(Paths.getPrefsPath(), "bin"),
142 new SVNUpdateListener() {
143 public void statusUpdate(int done, int total) {
144 publish(new Status(done, total));
145 }
146 });
147 }
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 return null;
152 }
153
154 private boolean checkWritable() {
155 File aei = new File(new File(Paths.getInstallerPath(), "bin"),
156 "AEInstaller2.jar");
157 File temp = new File(new File(Paths.getInstallerPath(), "bin"),
158 "temp.jar");
159 if (!aei.exists())
160 return true;
161 if (aei.renameTo(temp)) {
162 temp.renameTo(aei);
163 return true;
164 }
165 return false;
166 }
167
168 @Override
169 protected void process(List<Status> chunks) {
170 super.process(chunks);
171 if (chunks.size() > 0) {
172 Status s = chunks.get(chunks.size() - 1);
173 bar.setValue(s.done);
174 bar.setMaximum(s.total);
175 }
176 }
177
178 @Override
179 protected void done() {
180 super.done();
181 step.setText("AEI is up to date");
182 bar.setValue(1);
183 bar.setMaximum(1);
184 closeBtn.setEnabled(true);
185 }
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.