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

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

AEI2

File size: 11.2 KB
RevLine 
[591]1package net.oni2.aeinstaller.gui;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.ResourceBundle;
6import java.util.TreeMap;
[600]7import java.util.TreeSet;
[591]8
[600]9import javax.swing.JButton;
[591]10import javax.swing.JComboBox;
[593]11import javax.swing.JComponent;
[591]12import javax.swing.JFrame;
[592]13import javax.swing.JLabel;
[593]14import javax.swing.JMenu;
15import javax.swing.JOptionPane;
[592]16import javax.swing.JSplitPane;
[591]17import javax.swing.JTable;
18import javax.swing.ListSelectionModel;
19import javax.swing.RowSorter;
20import javax.swing.SortOrder;
21import javax.swing.SwingUtilities;
22import javax.swing.event.ListSelectionEvent;
23import javax.swing.event.ListSelectionListener;
24import javax.swing.table.TableRowSorter;
25
26import net.oni2.aeinstaller.backend.Settings;
[593]27import net.oni2.aeinstaller.backend.Settings.Platform;
[600]28import net.oni2.aeinstaller.backend.SizeFormatter;
[591]29import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener;
30import net.oni2.aeinstaller.backend.depot.DepotManager;
[600]31import net.oni2.aeinstaller.backend.mods.Mod;
32import net.oni2.aeinstaller.backend.mods.ModManager;
33import net.oni2.aeinstaller.backend.mods.Type;
34import net.oni2.aeinstaller.backend.oni.InstallProgressListener;
35import net.oni2.aeinstaller.backend.oni.Installer;
[593]36import net.oni2.aeinstaller.gui.about.AboutDialog;
[600]37import net.oni2.aeinstaller.gui.modtable.DownloadSizeListener;
[591]38import net.oni2.aeinstaller.gui.modtable.ModTableFilter;
39import net.oni2.aeinstaller.gui.modtable.ModTableModel;
40import net.oni2.aeinstaller.gui.settings.SettingsDialog;
41
42import org.javabuilders.BuildResult;
43import org.javabuilders.annotations.DoInBackground;
44import org.javabuilders.event.BackgroundEvent;
45import org.javabuilders.swing.SwingJavaBuilder;
[593]46import org.simplericity.macify.eawt.ApplicationEvent;
47import org.simplericity.macify.eawt.ApplicationListener;
[591]48
49/**
50 * @author Christian Illy
51 */
[600]52public class MainWin extends JFrame implements ApplicationListener,
53 DownloadSizeListener {
[591]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
[593]61 private JMenu mainMenu;
62
[592]63 private JSplitPane contents;
64
[591]65 private JComboBox cmbModTypes;
66 private JTable tblMods;
67 private ModTableModel model;
68 private TableRowSorter<ModTableModel> sorter;
[600]69 private JLabel lblDownloadSizeVal;
[591]70
[592]71 private JLabel lblSubmitterVal;
72 private JLabel lblCreatorVal;
73 private JLabel lblFilesVal;
74 private HTMLLinkLabel lblDescriptionVal;
75
[600]76 private JButton btnInstall;
77
[591]78 /**
79 * Constructor of main window.
80 */
81 public MainWin() {
[593]82 this.setTitle(SwingJavaBuilder.getConfig().getResource("appname")
83 + " - v"
84 + SwingJavaBuilder.getConfig().getResource("appversion"));
[591]85
[592]86 contents.setDividerLocation(400);
[593]87
88 if (Settings.getPlatform() == Platform.MACOS) {
89 mainMenu.setVisible(false);
90 }
[600]91
92 getRootPane().setDefaultButton(btnInstall);
[591]93 }
94
95 private void initModTypeBox() {
[592]96 cmbModTypes.removeAllItems();
97
[600]98 TreeMap<String, Type> types = new TreeMap<String, Type>();
99 for (Type t : ModManager.getInstance().getTypesWithContent()) {
100 types.put(t.getName(), t);
[591]101 }
[600]102 for (Type t : types.values()) {
[591]103 cmbModTypes.addItem(t);
104 }
105 cmbModTypes.setSelectedIndex(0);
106 }
107
108 private void initTable() {
109 tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
[592]110 tblMods.getSelectionModel().addListSelectionListener(
111 new ListSelectionListener() {
[591]112
[592]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);
[600]121 Mod mod = (Mod) model.getValueAt(modelRow, -1);
[592]122 modSelection(mod);
123 }
124 }
125 });
126
[593]127 // To get checkbox-cells with background of row
128 ((JComponent) tblMods.getDefaultRenderer(Boolean.class))
129 .setOpaque(true);
130
[591]131 model = new ModTableModel();
[600]132 model.addDownloadSizeListener(this);
[591]133
134 tblMods.setModel(model);
135
136 sorter = new TableRowSorter<ModTableModel>(model);
137 tblMods.setRowSorter(sorter);
138
[600]139 sorter.setRowFilter(new ModTableFilter(null));
[591]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
[593]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
[591]164 private void exit() {
165 setVisible(false);
166 dispose();
167 }
168
169 private void saveLocalData() {
170 Settings.getInstance().serializeToFile();
[596]171 DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename());
[591]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 });
[600]188 ModManager.getInstance().init();
189 initTable();
[592]190 initModTypeBox();
[600]191
[592]192 tblMods.setVisible(true);
[591]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() {
[593]219 new SettingsDialog().setVisible(true);
[591]220 }
221
[593]222 private void showAbout() {
223 new AboutDialog().setVisible(true);
224 }
225
226 @SuppressWarnings("unused")
227 private void loadConfig() {
[600]228 // TODO method stub
[593]229 JOptionPane.showMessageDialog(this, "loadConfig", "todo",
230 JOptionPane.INFORMATION_MESSAGE);
231 }
232
233 @SuppressWarnings("unused")
234 private void saveConfig() {
[600]235 // TODO method stub
[593]236 JOptionPane.showMessageDialog(this, "saveConfig", "todo",
237 JOptionPane.INFORMATION_MESSAGE);
238 }
239
[600]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
[593]252 @SuppressWarnings("unused")
[600]253 private void tools() {
254 // TODO method stub
255 JOptionPane.showMessageDialog(this, "tools", "todo",
[593]256 JOptionPane.INFORMATION_MESSAGE);
257 }
[596]258
[593]259 @SuppressWarnings("unused")
260 private void revertSelection() {
[600]261 // TODO method stub
[593]262 JOptionPane.showMessageDialog(this, "revertSelection", "todo",
263 JOptionPane.INFORMATION_MESSAGE);
264 }
265
[602]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
[600]280 @DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false)
281 private void install(final BackgroundEvent evt) {
[602]282 // TODO: Conflicts/Dependencies
283
[600]284 TreeSet<Mod> mods = new TreeSet<Mod>();
[602]285 mods.addAll(ModManager.getInstance().getMandatoryMods());
[600]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) {
[592]305 lblSubmitterVal.setText("");
306 lblCreatorVal.setText("");
307 lblFilesVal.setText("");
308 lblDescriptionVal.setText("");
[600]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());
[591]317 }
[592]318 // TODO
[591]319 }
320
321 @SuppressWarnings("unused")
[592]322 private void modTypeSelection() {
[600]323 Type t = (Type) cmbModTypes.getSelectedItem();
[592]324 if (t != null)
[600]325 sorter.setRowFilter(new ModTableFilter(t));
[592]326 else
[600]327 sorter.setRowFilter(new ModTableFilter(null));
[591]328 }
[593]329
330 @Override
[600]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
[593]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 }
[600]395
[592]396}
Note: See TracBrowser for help on using the repository browser.