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

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

AEI2:

  • Added offline detection (should run without internet connection)
  • Update check (manual/on startup), not actually updating yet
File size: 20.4 KB
Line 
1package net.oni2.aeinstaller.gui;
2
3import java.awt.Desktop;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.awt.event.MouseAdapter;
7import java.awt.event.MouseEvent;
8import java.io.File;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.List;
14import java.util.ResourceBundle;
15import java.util.TreeMap;
16import java.util.TreeSet;
17import java.util.Vector;
18
19import javax.swing.JButton;
20import javax.swing.JComboBox;
21import javax.swing.JComponent;
22import javax.swing.JFileChooser;
23import javax.swing.JFrame;
24import javax.swing.JLabel;
25import javax.swing.JMenu;
26import javax.swing.JMenuItem;
27import javax.swing.JOptionPane;
28import javax.swing.JPopupMenu;
29import javax.swing.JSplitPane;
30import javax.swing.JTable;
31import javax.swing.ListSelectionModel;
32import javax.swing.RowSorter;
33import javax.swing.SortOrder;
34import javax.swing.SwingUtilities;
35import javax.swing.event.ListSelectionEvent;
36import javax.swing.event.ListSelectionListener;
37import javax.swing.filechooser.FileFilter;
38import javax.swing.table.TableRowSorter;
39
40import net.oni2.aeinstaller.backend.Paths;
41import net.oni2.aeinstaller.backend.Settings;
42import net.oni2.aeinstaller.backend.Settings.Platform;
43import net.oni2.aeinstaller.backend.SizeFormatter;
44import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener;
45import net.oni2.aeinstaller.backend.depot.DepotManager;
46import net.oni2.aeinstaller.backend.mods.Mod;
47import net.oni2.aeinstaller.backend.mods.ModManager;
48import net.oni2.aeinstaller.backend.mods.Type;
49import net.oni2.aeinstaller.backend.mods.download.ModDownloader;
50import net.oni2.aeinstaller.backend.mods.download.ModDownloader.State;
51import net.oni2.aeinstaller.backend.mods.download.ModDownloaderListener;
52import net.oni2.aeinstaller.backend.oni.InstallProgressListener;
53import net.oni2.aeinstaller.backend.oni.Installer;
54import net.oni2.aeinstaller.backend.oni.OniSplit;
55import net.oni2.aeinstaller.gui.about.AboutDialog;
56import net.oni2.aeinstaller.gui.downloadwindow.Downloader;
57import net.oni2.aeinstaller.gui.modtable.DownloadSizeListener;
58import net.oni2.aeinstaller.gui.modtable.ModTableFilter;
59import net.oni2.aeinstaller.gui.modtable.ModTableModel;
60import net.oni2.aeinstaller.gui.settings.SettingsDialog;
61
62import org.javabuilders.BuildResult;
63import org.javabuilders.annotations.DoInBackground;
64import org.javabuilders.event.BackgroundEvent;
65import org.javabuilders.swing.SwingJavaBuilder;
66import org.simplericity.macify.eawt.ApplicationEvent;
67import org.simplericity.macify.eawt.ApplicationListener;
68
69/**
70 * @author Christian Illy
71 */
72public class MainWin extends JFrame implements ApplicationListener,
73 DownloadSizeListener {
74 private static final long serialVersionUID = -4027395051382659650L;
75
76 private ResourceBundle bundle = ResourceBundle.getBundle(getClass()
77 .getName());
78 @SuppressWarnings("unused")
79 private BuildResult result = SwingJavaBuilder.build(this, bundle);
80
81 private JMenu mainMenu;
82
83 private JSplitPane contents;
84
85 private JComboBox cmbModTypes;
86 private JTable tblMods;
87 private ModTableModel model;
88 private TableRowSorter<ModTableModel> sorter;
89 private JLabel lblDownloadSizeVal;
90
91 private JLabel lblSubmitterVal;
92 private JLabel lblCreatorVal;
93 private JLabel lblTypesVal;
94 private JLabel lblPlatformVal;
95 private JLabel lblPackageNumberVal;
96 private HTMLLinkLabel lblDescriptionVal;
97
98 private JButton btnInstall;
99
100 private TreeSet<Mod> execUpdates = null;
101
102 private enum EInstallResult {
103 DONE,
104 OFFLINE,
105 INCOMPATIBLE
106 };
107
108 private EInstallResult installDone = EInstallResult.DONE;
109
110 /**
111 * Constructor of main window.
112 */
113 public MainWin() {
114 this.setTitle(SwingJavaBuilder.getConfig().getResource("appname")
115 + " - v"
116 + SwingJavaBuilder.getConfig().getResource("appversion"));
117
118 contents.setDividerLocation(400);
119
120 if (Settings.getPlatform() == Platform.MACOS) {
121 mainMenu.setVisible(false);
122 }
123
124 getRootPane().setDefaultButton(btnInstall);
125 lblDownloadSizeVal.setText(SizeFormatter.format(0, 2));
126 }
127
128 private void initModTypeBox() {
129 cmbModTypes.removeAllItems();
130
131 TreeMap<String, Type> types = new TreeMap<String, Type>();
132 for (Type t : ModManager.getInstance().getTypesWithContent()) {
133 types.put(t.getName(), t);
134 }
135 for (Type t : types.values()) {
136 cmbModTypes.addItem(t);
137 }
138 cmbModTypes.setSelectedIndex(0);
139 }
140
141 private void initTable() {
142 tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
143 tblMods.getSelectionModel().addListSelectionListener(
144 new ListSelectionListener() {
145 @Override
146 public void valueChanged(ListSelectionEvent e) {
147 int viewRow = tblMods.getSelectedRow();
148 if (viewRow < 0) {
149 modSelection(null);
150 } else {
151 int modelRow = tblMods
152 .convertRowIndexToModel(viewRow);
153 Mod mod = (Mod) model.getValueAt(modelRow, -1);
154 modSelection(mod);
155 }
156 }
157 });
158 tblMods.addMouseListener(new MouseAdapter() {
159 private void common(MouseEvent e) {
160 int r = tblMods.rowAtPoint(e.getPoint());
161 if (r >= 0 && r < tblMods.getRowCount())
162 tblMods.setRowSelectionInterval(r, r);
163 else
164 tblMods.clearSelection();
165
166 int rowindex = tblMods.getSelectedRow();
167 if (rowindex >= 0) {
168 if (e.isPopupTrigger()
169 && e.getComponent() instanceof JTable) {
170 int modelRow = tblMods.convertRowIndexToModel(rowindex);
171 final Mod mod = (Mod) model.getValueAt(modelRow, -1);
172
173 if (mod.isLocalAvailable()) {
174 JPopupMenu popup = new JPopupMenu();
175 JMenuItem openModFolder = new JMenuItem(bundle
176 .getString("openModFolder.text"));
177 openModFolder
178 .addActionListener(new ActionListener() {
179 @Override
180 public void actionPerformed(
181 ActionEvent arg0) {
182 try {
183 Desktop.getDesktop().open(
184 mod.getLocalPath());
185 } catch (IOException e) {
186 e.printStackTrace();
187 }
188 }
189 });
190 popup.add(openModFolder);
191 popup.show(e.getComponent(), e.getX(), e.getY());
192 }
193 }
194 }
195 }
196
197 @Override
198 public void mousePressed(MouseEvent e) {
199 common(e);
200 }
201
202 @Override
203 public void mouseReleased(MouseEvent e) {
204 common(e);
205 }
206 });
207 // To get checkbox-cells with background of row
208 ((JComponent) tblMods.getDefaultRenderer(Boolean.class))
209 .setOpaque(true);
210
211 model = new ModTableModel();
212 model.addDownloadSizeListener(this);
213
214 tblMods.setModel(model);
215
216 sorter = new TableRowSorter<ModTableModel>(model);
217 tblMods.setRowSorter(sorter);
218
219 sorter.setRowFilter(new ModTableFilter(null));
220
221 List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
222 sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
223 sorter.setSortKeys(sortKeys);
224
225 for (int i = 0; i < model.getColumnCount(); i++) {
226 model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i));
227 }
228 }
229
230 private void exit() {
231 dispose();
232 System.exit(0);
233 }
234
235 private void saveLocalData() {
236 Settings.getInstance().serializeToFile();
237 DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename());
238 }
239
240 @DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false)
241 private void execDepotUpdate(final BackgroundEvent evt) {
242 if (!Settings.getInstance().isOfflineMode()) {
243 try {
244 DepotManager.getInstance().updateInformation(false,
245 new DepotCacheUpdateProgressListener() {
246
247 @Override
248 public void cacheUpdateProgress(String stepName,
249 int current, int total) {
250 evt.setProgressEnd(total);
251 evt.setProgressValue(current);
252 evt.setProgressMessage(stepName);
253 }
254 });
255 } catch (Exception e) {
256 e.printStackTrace();
257 }
258 }
259 ModManager.getInstance().init();
260 initTable();
261 initModTypeBox();
262
263 tblMods.setVisible(true);
264 }
265
266 @SuppressWarnings("unused")
267 private void checkUpdates(Object evtSource) {
268 if ((evtSource != this)
269 || Settings.getInstance().get("notifyupdates", true)) {
270 if (Settings.getInstance().isOfflineMode()) {
271 if (evtSource != this) {
272 JOptionPane.showMessageDialog(this,
273 bundle.getString("offlineMode.text"),
274 bundle.getString("offlineMode.title"),
275 JOptionPane.WARNING_MESSAGE);
276 }
277 } else {
278 TreeSet<Mod> mods = ModManager.getInstance().getUpdatableMods();
279 TreeSet<Mod> tools = ModManager.getInstance()
280 .getUpdatableTools();
281 int size = 0;
282 String strMods = "";
283 for (Mod m : mods) {
284 size += m.getZipSize();
285 if (strMods.length() > 0)
286 strMods += "<br>";
287 strMods += " - " + m.getName();
288 }
289 String strTools = "";
290 for (Mod m : tools) {
291 size += m.getZipSize();
292 if (strTools.length() > 0)
293 strTools += "<br>";
294 strTools += " - " + m.getName();
295 }
296 String message = "<html>";
297 message += String.format(
298 bundle.getString("updatesAvailable.text"), strMods,
299 strTools, SizeFormatter.format(size, 3));
300 message += "</html>";
301 int res = JOptionPane
302 .showConfirmDialog(this, message,
303 bundle.getString("updatesAvailable.title"),
304 JOptionPane.YES_NO_OPTION,
305 JOptionPane.QUESTION_MESSAGE);
306 if (res == JOptionPane.YES_OPTION) {
307 execUpdates = new TreeSet<Mod>();
308 execUpdates.addAll(mods);
309 execUpdates.addAll(tools);
310 }
311 }
312 }
313 }
314
315 @DoInBackground(progressMessage = "doUpdate.title", cancelable = false, indeterminateProgress = false)
316 private void doUpdate(final BackgroundEvent evt) {
317 if (execUpdates != null) {
318 // TODO
319 System.out.println("Update: " + execUpdates.toString());
320 // TODO: install new tools if previously installed
321 }
322 execUpdates = null;
323 }
324
325 @SuppressWarnings("unused")
326 private void focus() {
327 SwingUtilities.invokeLater(new Runnable() {
328
329 @Override
330 public void run() {
331 toFront();
332 repaint();
333 }
334 });
335
336 }
337
338 private void showSettings() {
339 new SettingsDialog().setVisible(true);
340 }
341
342 private void showAbout() {
343 new AboutDialog().setVisible(true);
344 }
345
346 private JFileChooser getConfigOpenSaveDialog(boolean save) {
347 JFileChooser fc = new JFileChooser();
348 fc.setCurrentDirectory(Paths.getEditionBasePath());
349 if (save)
350 fc.setDialogType(JFileChooser.SAVE_DIALOG);
351 else
352 fc.setDialogType(JFileChooser.OPEN_DIALOG);
353 fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
354 fc.setFileFilter(new FileFilter() {
355 @Override
356 public String getDescription() {
357 return "XML files";
358 }
359
360 @Override
361 public boolean accept(File arg0) {
362 return (arg0.isDirectory())
363 || (arg0.getName().toLowerCase().endsWith(".xml"));
364 }
365 });
366 fc.setMultiSelectionEnabled(false);
367 return fc;
368 }
369
370 @SuppressWarnings("unused")
371 private void loadConfig() {
372 JFileChooser fc = getConfigOpenSaveDialog(false);
373 int res = fc.showOpenDialog(this);
374 if (res == JFileChooser.APPROVE_OPTION) {
375 if (fc.getSelectedFile().exists())
376 model.reloadSelection(fc.getSelectedFile());
377 }
378 }
379
380 @SuppressWarnings("unused")
381 private void saveConfig() {
382 JFileChooser fc = getConfigOpenSaveDialog(true);
383 int res = fc.showSaveDialog(this);
384 if (res == JFileChooser.APPROVE_OPTION) {
385 File f = fc.getSelectedFile();
386 if (!f.getName().endsWith(".xml"))
387 f = new File(f.getParentFile(), f.getName() + ".xml");
388 ModManager.getInstance().saveModSelection(f,
389 model.getSelectedMods());
390 }
391 }
392
393 @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false)
394 private void reglobalize(final BackgroundEvent evt) {
395 Installer.initializeEdition(new InstallProgressListener() {
396 @Override
397 public void installProgressUpdate(int done, int total, String step) {
398 evt.setProgressEnd(total);
399 evt.setProgressValue(done);
400 evt.setProgressMessage(step);
401 }
402 });
403 }
404
405 @SuppressWarnings("unused")
406 private void tools() {
407 // TODO method stub
408 JOptionPane.showMessageDialog(this, "tools", "todo",
409 JOptionPane.INFORMATION_MESSAGE);
410 }
411
412 @SuppressWarnings("unused")
413 private void revertSelection() {
414 model.revertSelection();
415 }
416
417 @DoInBackground(progressMessage = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false)
418 private void checkMandatoryFiles(final BackgroundEvent evt) {
419 if (!Settings.getInstance().isOfflineMode()) {
420 TreeSet<Mod> mand = new TreeSet<Mod>();
421 for (Mod m : ModManager.getInstance().getMandatoryTools()) {
422 if (m.isNewerAvailable()) {
423 mand.add(m);
424 }
425 }
426 for (Mod m : ModManager.getInstance().getMandatoryMods()) {
427 if (m.isNewerAvailable()) {
428 mand.add(m);
429 }
430 }
431 if (mand.size() > 0) {
432 ModDownloader m = new ModDownloader(mand,
433 new ModDownloaderListener() {
434 @Override
435 public void updateStatus(ModDownloader source,
436 State state, int filesDown, int filesTotal,
437 int bytesDown, int bytesTotal,
438 int duration, int remaining, int speed) {
439 evt.setProgressEnd(filesTotal);
440 evt.setProgressValue(filesDown);
441 }
442 });
443 while (!m.isFinished()) {
444 try {
445 Thread.sleep(10);
446 } catch (InterruptedException e) {
447 e.printStackTrace();
448 }
449 }
450 }
451 evt.setProgressMessage(bundle
452 .getString("mandatoryToolsInstall.title"));
453 Installer
454 .installTools(ModManager.getInstance().getMandatoryTools());
455 }
456 }
457
458 @DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false)
459 private void install(final BackgroundEvent evt) {
460 TreeSet<Mod> mods = new TreeSet<Mod>();
461 mods.addAll(ModManager.getInstance().getMandatoryMods());
462 mods.addAll(model.getSelectedMods());
463
464 boolean instReady = false;
465 installDone = EInstallResult.DONE;
466
467 while (!instReady) {
468 TreeSet<Mod> toDownload = new TreeSet<Mod>();
469 for (Mod m : mods) {
470 if (!m.isLocalAvailable())
471 toDownload.add(m);
472 }
473 if (Settings.getInstance().isOfflineMode()) {
474 installDone = EInstallResult.OFFLINE;
475 break;
476 }
477 if (toDownload.size() > 0) {
478 Downloader dl = new Downloader(toDownload);
479 try {
480 dl.setVisible(true);
481 if (!dl.isFinished())
482 break;
483 } finally {
484 dl.dispose();
485 }
486 }
487 HashMap<Mod, HashSet<Mod>> dependencies = ModManager.getInstance()
488 .checkDependencies(mods);
489 if (dependencies.size() > 0) {
490 System.out.println("Unmet dependencies: "
491 + dependencies.toString());
492 for (Mod m : dependencies.keySet()) {
493 for (Mod mDep : dependencies.get(m))
494 mods.add(mDep);
495 }
496 } else {
497 HashMap<Mod, HashSet<Mod>> conflicts = ModManager.getInstance()
498 .checkIncompabitilites(mods);
499 if (conflicts.size() > 0) {
500 installDone = EInstallResult.INCOMPATIBLE;
501 System.err.println("Incompatible mods: "
502 + conflicts.toString());
503 break;
504 } else {
505 instReady = true;
506 }
507 }
508 }
509
510 if (instReady) {
511 Installer.install(mods, new InstallProgressListener() {
512 @Override
513 public void installProgressUpdate(int done, int total,
514 String step) {
515 evt.setProgressEnd(total);
516 evt.setProgressValue(done);
517 evt.setProgressMessage(step);
518 }
519 });
520 installDone = EInstallResult.DONE;
521 }
522 }
523
524 @SuppressWarnings("unused")
525 private void installDone() {
526 switch (installDone) {
527 case DONE:
528 JOptionPane.showMessageDialog(this,
529 bundle.getString("installDone.text"),
530 bundle.getString("installDone.title"),
531 JOptionPane.INFORMATION_MESSAGE);
532 break;
533 case OFFLINE:
534 JOptionPane.showMessageDialog(this,
535 bundle.getString("offlineMode.text"),
536 bundle.getString("offlineMode.title"),
537 JOptionPane.WARNING_MESSAGE);
538 break;
539 case INCOMPATIBLE:
540 break;
541 }
542 }
543
544 private void modSelection(Mod m) {
545 lblSubmitterVal.setText("");
546 lblCreatorVal.setText("");
547 lblDescriptionVal.setText("");
548 lblTypesVal.setText("");
549 lblPlatformVal.setText("");
550 lblPackageNumberVal.setText("");
551 if (m != null) {
552 lblSubmitterVal.setText(m.getName());
553 lblCreatorVal.setText(m.getCreator());
554 lblDescriptionVal.setText(m.getDescription());
555
556 String types = "";
557 for (Type t : m.getTypes()) {
558 if (types.length() > 0)
559 types += ", ";
560 types += t.getName();
561 }
562 lblTypesVal.setText(types);
563 lblPlatformVal.setText(m.getPlatform().toString());
564 lblPackageNumberVal.setText(m.getPackageNumberString());
565 }
566 }
567
568 @SuppressWarnings("unused")
569 private void modTypeSelection() {
570 Type t = (Type) cmbModTypes.getSelectedItem();
571 if (t != null)
572 sorter.setRowFilter(new ModTableFilter(t));
573 else
574 sorter.setRowFilter(new ModTableFilter(null));
575 }
576
577 @Override
578 public void downloadSizeChanged(int newSize) {
579 lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2));
580 }
581
582 @SuppressWarnings("unused")
583 private void checkInitialize() {
584 if (!Installer.isEditionInitialized()) {
585 if (!OniSplit.isOniSplitInstalled()) {
586 JOptionPane.showMessageDialog(this,
587 bundle.getString("noOniSplit.text"),
588 bundle.getString("noOniSplit.title"),
589 JOptionPane.ERROR_MESSAGE);
590 exit();
591 } else {
592 int res = JOptionPane
593 .showConfirmDialog(this,
594 bundle.getString("askInitialize.text"),
595 bundle.getString("askInitialize.title"),
596 JOptionPane.YES_NO_OPTION,
597 JOptionPane.QUESTION_MESSAGE);
598 if (res == JOptionPane.NO_OPTION) {
599 saveLocalData();
600 exit();
601 }
602 }
603 }
604 }
605
606 @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false)
607 private void initialize(final BackgroundEvent evt) {
608 if (!Installer.isEditionInitialized()) {
609 Installer.initializeEdition(new InstallProgressListener() {
610 @Override
611 public void installProgressUpdate(int done, int total,
612 String step) {
613 evt.setProgressEnd(total);
614 evt.setProgressValue(done);
615 evt.setProgressMessage(step);
616 }
617 });
618 }
619 }
620
621 private Vector<String> getBasicOniLaunchParams() {
622 Vector<String> params = new Vector<String>();
623 File exe = null;
624 switch (Settings.getPlatform()) {
625 case WIN:
626 exe = new File(Paths.getEditionBasePath(), "Oni.exe");
627 if (exe.exists())
628 params.add(exe.getPath());
629 break;
630 case MACOS:
631 exe = new File(Paths.getEditionBasePath(),
632 "Oni.app/Contents/MacOS/Oni");
633 if (exe.exists())
634 params.add(exe.getPath());
635 break;
636 case LINUX:
637 String wine = Settings.getWinePath();
638 exe = new File(Paths.getEditionBasePath(), "Oni.exe");
639 if (exe.exists()) {
640 if (wine != null) {
641 params.add(wine);
642 params.add(exe.getPath());
643 }
644 }
645 break;
646 default:
647 }
648 if (params.size() > 0) {
649 params.add("-debugfiles");
650 }
651 return params;
652 }
653
654 @SuppressWarnings("unused")
655 private void oniFull() {
656 Vector<String> params = getBasicOniLaunchParams();
657 if (params.size() > 0) {
658 try {
659 ProcessBuilder pb = new ProcessBuilder(params);
660 pb.directory(Paths.getEditionBasePath());
661 pb.start();
662 } catch (IOException e) {
663 e.printStackTrace();
664 }
665 }
666 }
667
668 @SuppressWarnings("unused")
669 private void oniWin() {
670 Vector<String> params = getBasicOniLaunchParams();
671 if (params.size() > 0) {
672 params.add("-noswitch");
673 try {
674 ProcessBuilder pb = new ProcessBuilder(params);
675 pb.directory(Paths.getEditionBasePath());
676 pb.start();
677 } catch (IOException e) {
678 e.printStackTrace();
679 }
680 }
681 }
682
683 @SuppressWarnings("unused")
684 private void openEditionFolder() {
685 try {
686 Desktop.getDesktop().open(Paths.getEditionBasePath());
687 } catch (IOException e) {
688 e.printStackTrace();
689 }
690 }
691
692 @Override
693 public void handleAbout(ApplicationEvent event) {
694 event.setHandled(true);
695 showAbout();
696 }
697
698 @Override
699 public void handleOpenApplication(ApplicationEvent event) {
700 }
701
702 @Override
703 public void handleOpenFile(ApplicationEvent event) {
704 }
705
706 @Override
707 public void handlePreferences(ApplicationEvent event) {
708 showSettings();
709 }
710
711 @Override
712 public void handlePrintFile(ApplicationEvent event) {
713 }
714
715 @Override
716 public void handleQuit(ApplicationEvent event) {
717 event.setHandled(true);
718 saveLocalData();
719 exit();
720 }
721
722 @Override
723 public void handleReOpenApplication(ApplicationEvent event) {
724 }
725
726}
Note: See TracBrowser for help on using the repository browser.