1 |
package net.oni2.aeinstaller.gui; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.ResourceBundle; |
6 |
import java.util.TreeMap; |
7 |
import java.util.TreeSet; |
8 |
|
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComboBox; |
11 |
import javax.swing.JComponent; |
12 |
import javax.swing.JFrame; |
13 |
import javax.swing.JLabel; |
14 |
import javax.swing.JMenu; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.JSplitPane; |
17 |
import javax.swing.JTable; |
18 |
import javax.swing.ListSelectionModel; |
19 |
import javax.swing.RowSorter; |
20 |
import javax.swing.SortOrder; |
21 |
import javax.swing.SwingUtilities; |
22 |
import javax.swing.event.ListSelectionEvent; |
23 |
import javax.swing.event.ListSelectionListener; |
24 |
import javax.swing.table.TableRowSorter; |
25 |
|
26 |
import net.oni2.aeinstaller.backend.Settings; |
27 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
28 |
import net.oni2.aeinstaller.backend.SizeFormatter; |
29 |
import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener; |
30 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
31 |
import net.oni2.aeinstaller.backend.mods.Mod; |
32 |
import net.oni2.aeinstaller.backend.mods.ModManager; |
33 |
import net.oni2.aeinstaller.backend.mods.Type; |
34 |
import net.oni2.aeinstaller.backend.oni.InstallProgressListener; |
35 |
import net.oni2.aeinstaller.backend.oni.Installer; |
36 |
import net.oni2.aeinstaller.gui.about.AboutDialog; |
37 |
import net.oni2.aeinstaller.gui.modtable.DownloadSizeListener; |
38 |
import net.oni2.aeinstaller.gui.modtable.ModTableFilter; |
39 |
import net.oni2.aeinstaller.gui.modtable.ModTableModel; |
40 |
import net.oni2.aeinstaller.gui.settings.SettingsDialog; |
41 |
|
42 |
import org.javabuilders.BuildResult; |
43 |
import org.javabuilders.annotations.DoInBackground; |
44 |
import org.javabuilders.event.BackgroundEvent; |
45 |
import org.javabuilders.swing.SwingJavaBuilder; |
46 |
import org.simplericity.macify.eawt.ApplicationEvent; |
47 |
import org.simplericity.macify.eawt.ApplicationListener; |
48 |
|
49 |
/** |
50 |
* @author Christian Illy |
51 |
*/ |
52 |
public class MainWin extends JFrame implements ApplicationListener, |
53 |
DownloadSizeListener { |
54 |
private static final long serialVersionUID = -4027395051382659650L; |
55 |
|
56 |
private ResourceBundle bundle = ResourceBundle.getBundle(getClass() |
57 |
.getName()); |
58 |
@SuppressWarnings("unused") |
59 |
private BuildResult result = SwingJavaBuilder.build(this, bundle); |
60 |
|
61 |
private JMenu mainMenu; |
62 |
|
63 |
private JSplitPane contents; |
64 |
|
65 |
private JComboBox cmbModTypes; |
66 |
private JTable tblMods; |
67 |
private ModTableModel model; |
68 |
private TableRowSorter<ModTableModel> sorter; |
69 |
private JLabel lblDownloadSizeVal; |
70 |
|
71 |
private JLabel lblSubmitterVal; |
72 |
private JLabel lblCreatorVal; |
73 |
private JLabel lblFilesVal; |
74 |
private HTMLLinkLabel lblDescriptionVal; |
75 |
|
76 |
private JButton btnInstall; |
77 |
|
78 |
/** |
79 |
* Constructor of main window. |
80 |
*/ |
81 |
public MainWin() { |
82 |
this.setTitle(SwingJavaBuilder.getConfig().getResource("appname") |
83 |
+ " - v" |
84 |
+ SwingJavaBuilder.getConfig().getResource("appversion")); |
85 |
|
86 |
contents.setDividerLocation(400); |
87 |
|
88 |
if (Settings.getPlatform() == Platform.MACOS) { |
89 |
mainMenu.setVisible(false); |
90 |
} |
91 |
|
92 |
getRootPane().setDefaultButton(btnInstall); |
93 |
} |
94 |
|
95 |
private void initModTypeBox() { |
96 |
cmbModTypes.removeAllItems(); |
97 |
|
98 |
TreeMap<String, Type> types = new TreeMap<String, Type>(); |
99 |
for (Type t : ModManager.getInstance().getTypesWithContent()) { |
100 |
types.put(t.getName(), t); |
101 |
} |
102 |
for (Type t : types.values()) { |
103 |
cmbModTypes.addItem(t); |
104 |
} |
105 |
cmbModTypes.setSelectedIndex(0); |
106 |
} |
107 |
|
108 |
private void initTable() { |
109 |
tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
110 |
tblMods.getSelectionModel().addListSelectionListener( |
111 |
new ListSelectionListener() { |
112 |
|
113 |
@Override |
114 |
public void valueChanged(ListSelectionEvent e) { |
115 |
int viewRow = tblMods.getSelectedRow(); |
116 |
if (viewRow < 0) { |
117 |
modSelection(null); |
118 |
} else { |
119 |
int modelRow = tblMods |
120 |
.convertRowIndexToModel(viewRow); |
121 |
Mod mod = (Mod) model.getValueAt(modelRow, -1); |
122 |
modSelection(mod); |
123 |
} |
124 |
} |
125 |
}); |
126 |
|
127 |
// To get checkbox-cells with background of row |
128 |
((JComponent) tblMods.getDefaultRenderer(Boolean.class)) |
129 |
.setOpaque(true); |
130 |
|
131 |
model = new ModTableModel(); |
132 |
model.addDownloadSizeListener(this); |
133 |
|
134 |
tblMods.setModel(model); |
135 |
|
136 |
sorter = new TableRowSorter<ModTableModel>(model); |
137 |
tblMods.setRowSorter(sorter); |
138 |
|
139 |
sorter.setRowFilter(new ModTableFilter(null)); |
140 |
|
141 |
sorter.setSortable(2, false); |
142 |
|
143 |
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(); |
144 |
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); |
145 |
sorter.setSortKeys(sortKeys); |
146 |
|
147 |
for (int i = 0; i < model.getColumnCount(); i++) { |
148 |
model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i)); |
149 |
} |
150 |
|
151 |
// for (int i = 3; i > 0; i--) { |
152 |
// tblMods.getColumnModel().removeColumn(tblMods.getColumnModel().getColumn(i)); |
153 |
// } |
154 |
} |
155 |
|
156 |
private boolean askClose() { |
157 |
int res = JOptionPane.showConfirmDialog(this, |
158 |
bundle.getString("askClose.text"), |
159 |
bundle.getString("askClose.title"), JOptionPane.YES_NO_OPTION, |
160 |
JOptionPane.QUESTION_MESSAGE); |
161 |
return res == JOptionPane.YES_OPTION; |
162 |
} |
163 |
|
164 |
private void exit() { |
165 |
setVisible(false); |
166 |
dispose(); |
167 |
} |
168 |
|
169 |
private void saveLocalData() { |
170 |
Settings.getInstance().serializeToFile(); |
171 |
DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename()); |
172 |
} |
173 |
|
174 |
@DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false) |
175 |
private void execDepotUpdate(final BackgroundEvent evt) { |
176 |
try { |
177 |
DepotManager.getInstance().updateInformation(false, |
178 |
new DepotCacheUpdateProgressListener() { |
179 |
|
180 |
@Override |
181 |
public void cacheUpdateProgress(String stepName, |
182 |
int current, int total) { |
183 |
evt.setProgressEnd(total); |
184 |
evt.setProgressValue(current); |
185 |
evt.setProgressMessage(stepName); |
186 |
} |
187 |
}); |
188 |
ModManager.getInstance().init(); |
189 |
initTable(); |
190 |
initModTypeBox(); |
191 |
|
192 |
tblMods.setVisible(true); |
193 |
DepotManager.getInstance().printStats(); |
194 |
} catch (Exception e) { |
195 |
e.printStackTrace(); |
196 |
} |
197 |
} |
198 |
|
199 |
@SuppressWarnings("unused") |
200 |
private void checkUpdates() { |
201 |
if (Settings.getInstance().get("notifyupdates", true)) { |
202 |
} |
203 |
} |
204 |
|
205 |
@SuppressWarnings("unused") |
206 |
private void focus() { |
207 |
SwingUtilities.invokeLater(new Runnable() { |
208 |
|
209 |
@Override |
210 |
public void run() { |
211 |
toFront(); |
212 |
repaint(); |
213 |
} |
214 |
}); |
215 |
|
216 |
} |
217 |
|
218 |
private void showSettings() { |
219 |
new SettingsDialog().setVisible(true); |
220 |
} |
221 |
|
222 |
private void showAbout() { |
223 |
new AboutDialog().setVisible(true); |
224 |
} |
225 |
|
226 |
@SuppressWarnings("unused") |
227 |
private void loadConfig() { |
228 |
// TODO method stub |
229 |
JOptionPane.showMessageDialog(this, "loadConfig", "todo", |
230 |
JOptionPane.INFORMATION_MESSAGE); |
231 |
} |
232 |
|
233 |
@SuppressWarnings("unused") |
234 |
private void saveConfig() { |
235 |
// TODO method stub |
236 |
JOptionPane.showMessageDialog(this, "saveConfig", "todo", |
237 |
JOptionPane.INFORMATION_MESSAGE); |
238 |
} |
239 |
|
240 |
@DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) |
241 |
private void reglobalize(final BackgroundEvent evt) { |
242 |
Installer.initializeEdition(new InstallProgressListener() { |
243 |
@Override |
244 |
public void installProgressUpdate(int done, int total, String step) { |
245 |
evt.setProgressEnd(total); |
246 |
evt.setProgressValue(done); |
247 |
evt.setProgressMessage(step); |
248 |
} |
249 |
}); |
250 |
} |
251 |
|
252 |
@SuppressWarnings("unused") |
253 |
private void tools() { |
254 |
// TODO method stub |
255 |
JOptionPane.showMessageDialog(this, "tools", "todo", |
256 |
JOptionPane.INFORMATION_MESSAGE); |
257 |
} |
258 |
|
259 |
@SuppressWarnings("unused") |
260 |
private void revertSelection() { |
261 |
// TODO method stub |
262 |
JOptionPane.showMessageDialog(this, "revertSelection", "todo", |
263 |
JOptionPane.INFORMATION_MESSAGE); |
264 |
} |
265 |
|
266 |
@DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false) |
267 |
private void install(final BackgroundEvent evt) { |
268 |
TreeSet<Mod> mods = new TreeSet<Mod>(); |
269 |
mods.addAll(ModManager.getInstance().getDefaultMods()); |
270 |
mods.addAll(model.getSelectedMods()); |
271 |
|
272 |
System.out.println("Install mods:"); |
273 |
for (Mod m : mods) { |
274 |
System.out |
275 |
.println(" " + m.getPackageNumber() + ": " + m.getName()); |
276 |
} |
277 |
|
278 |
Installer.install(mods, new InstallProgressListener() { |
279 |
@Override |
280 |
public void installProgressUpdate(int done, int total, String step) { |
281 |
evt.setProgressEnd(total); |
282 |
evt.setProgressValue(done); |
283 |
evt.setProgressMessage(step); |
284 |
} |
285 |
}); |
286 |
|
287 |
// TODO method stub |
288 |
JOptionPane.showMessageDialog(this, "install", "todo", |
289 |
JOptionPane.INFORMATION_MESSAGE); |
290 |
} |
291 |
|
292 |
private void modSelection(Mod m) { |
293 |
lblSubmitterVal.setText(""); |
294 |
lblCreatorVal.setText(""); |
295 |
lblFilesVal.setText(""); |
296 |
lblDescriptionVal.setText(""); |
297 |
if (m != null) { |
298 |
lblSubmitterVal.setText(m.getName()); |
299 |
lblCreatorVal.setText(m.getCreator()); |
300 |
if (m.getNode() != null) { |
301 |
lblFilesVal.setText(Integer.toString(m.getNode().getUploads() |
302 |
.size())); |
303 |
} |
304 |
lblDescriptionVal.setText(m.getDescription()); |
305 |
} |
306 |
// TODO |
307 |
} |
308 |
|
309 |
@SuppressWarnings("unused") |
310 |
private void modTypeSelection() { |
311 |
Type t = (Type) cmbModTypes.getSelectedItem(); |
312 |
if (t != null) |
313 |
sorter.setRowFilter(new ModTableFilter(t)); |
314 |
else |
315 |
sorter.setRowFilter(new ModTableFilter(null)); |
316 |
} |
317 |
|
318 |
@Override |
319 |
public void downloadSizeChanged(int newSize) { |
320 |
lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2)); |
321 |
} |
322 |
|
323 |
@DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) |
324 |
private void initialize(final BackgroundEvent evt) { |
325 |
if (!Installer.isEditionInitialized()) { |
326 |
int res = JOptionPane.showConfirmDialog(this, |
327 |
bundle.getString("askInitialize.text"), |
328 |
bundle.getString("askInitialize.title"), |
329 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); |
330 |
if (res == JOptionPane.NO_OPTION) { |
331 |
exit(); |
332 |
} else { |
333 |
Installer.initializeEdition(new InstallProgressListener() { |
334 |
@Override |
335 |
public void installProgressUpdate(int done, int total, |
336 |
String step) { |
337 |
evt.setProgressEnd(total); |
338 |
evt.setProgressValue(done); |
339 |
evt.setProgressMessage(step); |
340 |
} |
341 |
}); |
342 |
} |
343 |
} |
344 |
} |
345 |
|
346 |
@Override |
347 |
public void handleAbout(ApplicationEvent event) { |
348 |
event.setHandled(true); |
349 |
showAbout(); |
350 |
} |
351 |
|
352 |
@Override |
353 |
public void handleOpenApplication(ApplicationEvent event) { |
354 |
} |
355 |
|
356 |
@Override |
357 |
public void handleOpenFile(ApplicationEvent event) { |
358 |
} |
359 |
|
360 |
@Override |
361 |
public void handlePreferences(ApplicationEvent event) { |
362 |
showSettings(); |
363 |
} |
364 |
|
365 |
@Override |
366 |
public void handlePrintFile(ApplicationEvent event) { |
367 |
} |
368 |
|
369 |
@Override |
370 |
public void handleQuit(ApplicationEvent event) { |
371 |
if (askClose()) { |
372 |
event.setHandled(true); |
373 |
saveLocalData(); |
374 |
exit(); |
375 |
} else { |
376 |
event.setHandled(false); |
377 |
} |
378 |
} |
379 |
|
380 |
@Override |
381 |
public void handleReOpenApplication(ApplicationEvent event) { |
382 |
} |
383 |
|
384 |
} |