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 = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false) |
267 |
private void checkMandatoryFiles(final BackgroundEvent evt) { |
268 |
System.out.println("Tools:"); |
269 |
for (Mod m : ModManager.getInstance().getTools()) { |
270 |
System.out.format(" %05d %s", m.getPackageNumber(), m.getName()); |
271 |
} |
272 |
System.out.println(); |
273 |
|
274 |
System.out.println("Mandatory tools:"); |
275 |
for (Mod m : ModManager.getInstance().getMandatoryTools()) { |
276 |
System.out.format(" %05d %s", m.getPackageNumber(), m.getName()); |
277 |
} |
278 |
} |
279 |
|
280 |
@DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false) |
281 |
private void install(final BackgroundEvent evt) { |
282 |
// TODO: Conflicts/Dependencies |
283 |
|
284 |
TreeSet<Mod> mods = new TreeSet<Mod>(); |
285 |
mods.addAll(ModManager.getInstance().getMandatoryMods()); |
286 |
mods.addAll(model.getSelectedMods()); |
287 |
|
288 |
System.out.println("Install mods:"); |
289 |
for (Mod m : mods) { |
290 |
System.out |
291 |
.println(" " + m.getPackageNumber() + ": " + m.getName()); |
292 |
} |
293 |
|
294 |
Installer.install(mods, new InstallProgressListener() { |
295 |
@Override |
296 |
public void installProgressUpdate(int done, int total, String step) { |
297 |
evt.setProgressEnd(total); |
298 |
evt.setProgressValue(done); |
299 |
evt.setProgressMessage(step); |
300 |
} |
301 |
}); |
302 |
} |
303 |
|
304 |
private void modSelection(Mod m) { |
305 |
lblSubmitterVal.setText(""); |
306 |
lblCreatorVal.setText(""); |
307 |
lblFilesVal.setText(""); |
308 |
lblDescriptionVal.setText(""); |
309 |
if (m != null) { |
310 |
lblSubmitterVal.setText(m.getName()); |
311 |
lblCreatorVal.setText(m.getCreator()); |
312 |
if (m.getNode() != null) { |
313 |
lblFilesVal.setText(Integer.toString(m.getNode().getUploads() |
314 |
.size())); |
315 |
} |
316 |
lblDescriptionVal.setText(m.getDescription()); |
317 |
} |
318 |
// TODO |
319 |
} |
320 |
|
321 |
@SuppressWarnings("unused") |
322 |
private void modTypeSelection() { |
323 |
Type t = (Type) cmbModTypes.getSelectedItem(); |
324 |
if (t != null) |
325 |
sorter.setRowFilter(new ModTableFilter(t)); |
326 |
else |
327 |
sorter.setRowFilter(new ModTableFilter(null)); |
328 |
} |
329 |
|
330 |
@Override |
331 |
public void downloadSizeChanged(int newSize) { |
332 |
lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2)); |
333 |
} |
334 |
|
335 |
@DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) |
336 |
private void initialize(final BackgroundEvent evt) { |
337 |
if (!Installer.isEditionInitialized()) { |
338 |
int res = JOptionPane.showConfirmDialog(this, |
339 |
bundle.getString("askInitialize.text"), |
340 |
bundle.getString("askInitialize.title"), |
341 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); |
342 |
if (res == JOptionPane.NO_OPTION) { |
343 |
exit(); |
344 |
} else { |
345 |
Installer.initializeEdition(new InstallProgressListener() { |
346 |
@Override |
347 |
public void installProgressUpdate(int done, int total, |
348 |
String step) { |
349 |
evt.setProgressEnd(total); |
350 |
evt.setProgressValue(done); |
351 |
evt.setProgressMessage(step); |
352 |
} |
353 |
}); |
354 |
} |
355 |
} |
356 |
} |
357 |
|
358 |
@Override |
359 |
public void handleAbout(ApplicationEvent event) { |
360 |
event.setHandled(true); |
361 |
showAbout(); |
362 |
} |
363 |
|
364 |
@Override |
365 |
public void handleOpenApplication(ApplicationEvent event) { |
366 |
} |
367 |
|
368 |
@Override |
369 |
public void handleOpenFile(ApplicationEvent event) { |
370 |
} |
371 |
|
372 |
@Override |
373 |
public void handlePreferences(ApplicationEvent event) { |
374 |
showSettings(); |
375 |
} |
376 |
|
377 |
@Override |
378 |
public void handlePrintFile(ApplicationEvent event) { |
379 |
} |
380 |
|
381 |
@Override |
382 |
public void handleQuit(ApplicationEvent event) { |
383 |
if (askClose()) { |
384 |
event.setHandled(true); |
385 |
saveLocalData(); |
386 |
exit(); |
387 |
} else { |
388 |
event.setHandled(false); |
389 |
} |
390 |
} |
391 |
|
392 |
@Override |
393 |
public void handleReOpenApplication(ApplicationEvent event) { |
394 |
} |
395 |
|
396 |
} |