source: AE/installer2/src/net/oni2/aeinstaller/gui/MainWin.java@ 604

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

AEI2: Added load/save config

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