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

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

AEI2

File size: 11.1 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 HTMLLinkLabel lblDescriptionVal;
74
[600]75 private JButton btnInstall;
76
[591]77 /**
78 * Constructor of main window.
79 */
80 public MainWin() {
[593]81 this.setTitle(SwingJavaBuilder.getConfig().getResource("appname")
82 + " - v"
83 + SwingJavaBuilder.getConfig().getResource("appversion"));
[591]84
[592]85 contents.setDividerLocation(400);
[593]86
87 if (Settings.getPlatform() == Platform.MACOS) {
88 mainMenu.setVisible(false);
89 }
[600]90
91 getRootPane().setDefaultButton(btnInstall);
[591]92 }
93
94 private void initModTypeBox() {
[592]95 cmbModTypes.removeAllItems();
96
[600]97 TreeMap<String, Type> types = new TreeMap<String, Type>();
98 for (Type t : ModManager.getInstance().getTypesWithContent()) {
99 types.put(t.getName(), t);
[591]100 }
[600]101 for (Type t : types.values()) {
[591]102 cmbModTypes.addItem(t);
103 }
104 cmbModTypes.setSelectedIndex(0);
105 }
106
107 private void initTable() {
108 tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
[592]109 tblMods.getSelectionModel().addListSelectionListener(
110 new ListSelectionListener() {
[591]111
[592]112 @Override
113 public void valueChanged(ListSelectionEvent e) {
114 int viewRow = tblMods.getSelectedRow();
115 if (viewRow < 0) {
116 modSelection(null);
117 } else {
118 int modelRow = tblMods
119 .convertRowIndexToModel(viewRow);
[600]120 Mod mod = (Mod) model.getValueAt(modelRow, -1);
[592]121 modSelection(mod);
122 }
123 }
124 });
125
[593]126 // To get checkbox-cells with background of row
127 ((JComponent) tblMods.getDefaultRenderer(Boolean.class))
128 .setOpaque(true);
129
[591]130 model = new ModTableModel();
[600]131 model.addDownloadSizeListener(this);
[591]132
133 tblMods.setModel(model);
134
135 sorter = new TableRowSorter<ModTableModel>(model);
136 tblMods.setRowSorter(sorter);
137
[600]138 sorter.setRowFilter(new ModTableFilter(null));
[591]139
140 sorter.setSortable(2, false);
141
142 List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
143 sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
144 sorter.setSortKeys(sortKeys);
145
146 for (int i = 0; i < model.getColumnCount(); i++) {
147 model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i));
148 }
149
150 // for (int i = 3; i > 0; i--) {
151 // tblMods.getColumnModel().removeColumn(tblMods.getColumnModel().getColumn(i));
152 // }
153 }
154
[593]155 private boolean askClose() {
156 int res = JOptionPane.showConfirmDialog(this,
157 bundle.getString("askClose.text"),
158 bundle.getString("askClose.title"), JOptionPane.YES_NO_OPTION,
159 JOptionPane.QUESTION_MESSAGE);
160 return res == JOptionPane.YES_OPTION;
161 }
162
[591]163 private void exit() {
164 setVisible(false);
165 dispose();
166 }
167
168 private void saveLocalData() {
169 Settings.getInstance().serializeToFile();
[596]170 DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename());
[591]171 }
172
173 @DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false)
174 private void execDepotUpdate(final BackgroundEvent evt) {
175 try {
176 DepotManager.getInstance().updateInformation(false,
177 new DepotCacheUpdateProgressListener() {
178
179 @Override
180 public void cacheUpdateProgress(String stepName,
181 int current, int total) {
182 evt.setProgressEnd(total);
183 evt.setProgressValue(current);
184 evt.setProgressMessage(stepName);
185 }
186 });
[600]187 ModManager.getInstance().init();
188 initTable();
[592]189 initModTypeBox();
[600]190
[592]191 tblMods.setVisible(true);
[591]192 } catch (Exception e) {
193 e.printStackTrace();
194 }
195 }
196
197 @SuppressWarnings("unused")
198 private void checkUpdates() {
199 if (Settings.getInstance().get("notifyupdates", true)) {
200 }
201 }
202
203 @SuppressWarnings("unused")
204 private void focus() {
205 SwingUtilities.invokeLater(new Runnable() {
206
207 @Override
208 public void run() {
209 toFront();
210 repaint();
211 }
212 });
213
214 }
215
216 private void showSettings() {
[593]217 new SettingsDialog().setVisible(true);
[591]218 }
219
[593]220 private void showAbout() {
221 new AboutDialog().setVisible(true);
222 }
223
224 @SuppressWarnings("unused")
225 private void loadConfig() {
[600]226 // TODO method stub
[593]227 JOptionPane.showMessageDialog(this, "loadConfig", "todo",
228 JOptionPane.INFORMATION_MESSAGE);
229 }
230
231 @SuppressWarnings("unused")
232 private void saveConfig() {
[600]233 // TODO method stub
[593]234 JOptionPane.showMessageDialog(this, "saveConfig", "todo",
235 JOptionPane.INFORMATION_MESSAGE);
236 }
237
[600]238 @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false)
239 private void reglobalize(final BackgroundEvent evt) {
240 Installer.initializeEdition(new InstallProgressListener() {
241 @Override
242 public void installProgressUpdate(int done, int total, String step) {
243 evt.setProgressEnd(total);
244 evt.setProgressValue(done);
245 evt.setProgressMessage(step);
246 }
247 });
248 }
249
[593]250 @SuppressWarnings("unused")
[600]251 private void tools() {
252 // TODO method stub
253 JOptionPane.showMessageDialog(this, "tools", "todo",
[593]254 JOptionPane.INFORMATION_MESSAGE);
255 }
[596]256
[593]257 @SuppressWarnings("unused")
258 private void revertSelection() {
[600]259 // TODO method stub
[593]260 JOptionPane.showMessageDialog(this, "revertSelection", "todo",
261 JOptionPane.INFORMATION_MESSAGE);
262 }
263
[602]264 @DoInBackground(progressMessage = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false)
265 private void checkMandatoryFiles(final BackgroundEvent evt) {
[603]266 //TODO
267 System.out.println("Mandatory Tools:");
268 for (Mod m : ModManager.getInstance().getMandatoryTools()) {
269 System.out.format(" %05d %15s - Local: %b - Update: %b", m.getPackageNumber(), m.getName(), m.isLocalAvailable(), m.isNewerAvailable());
[602]270 }
271 System.out.println();
272
[603]273 System.out.println("Mandatory Mods:");
274 for (Mod m : ModManager.getInstance().getMandatoryMods()) {
275 System.out.format(" %05d %15s - Local: %b - Update: %b", m.getPackageNumber(), m.getName(), m.isLocalAvailable(), m.isNewerAvailable());
[602]276 }
277 }
278
[600]279 @DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false)
280 private void install(final BackgroundEvent evt) {
[602]281 // TODO: Conflicts/Dependencies
282
[600]283 TreeSet<Mod> mods = new TreeSet<Mod>();
[602]284 mods.addAll(ModManager.getInstance().getMandatoryMods());
[600]285 mods.addAll(model.getSelectedMods());
286
287 System.out.println("Install mods:");
288 for (Mod m : mods) {
289 System.out
290 .println(" " + m.getPackageNumber() + ": " + m.getName());
291 }
292
293 Installer.install(mods, new InstallProgressListener() {
294 @Override
295 public void installProgressUpdate(int done, int total, String step) {
296 evt.setProgressEnd(total);
297 evt.setProgressValue(done);
298 evt.setProgressMessage(step);
299 }
300 });
301 }
302
303 private void modSelection(Mod m) {
[592]304 lblSubmitterVal.setText("");
305 lblCreatorVal.setText("");
306 lblDescriptionVal.setText("");
[600]307 if (m != null) {
308 lblSubmitterVal.setText(m.getName());
309 lblCreatorVal.setText(m.getCreator());
310 lblDescriptionVal.setText(m.getDescription());
[591]311 }
[592]312 // TODO
[591]313 }
314
315 @SuppressWarnings("unused")
[592]316 private void modTypeSelection() {
[600]317 Type t = (Type) cmbModTypes.getSelectedItem();
[592]318 if (t != null)
[600]319 sorter.setRowFilter(new ModTableFilter(t));
[592]320 else
[600]321 sorter.setRowFilter(new ModTableFilter(null));
[591]322 }
[593]323
324 @Override
[600]325 public void downloadSizeChanged(int newSize) {
326 lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2));
327 }
328
329 @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false)
330 private void initialize(final BackgroundEvent evt) {
331 if (!Installer.isEditionInitialized()) {
332 int res = JOptionPane.showConfirmDialog(this,
333 bundle.getString("askInitialize.text"),
334 bundle.getString("askInitialize.title"),
335 JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
336 if (res == JOptionPane.NO_OPTION) {
337 exit();
338 } else {
339 Installer.initializeEdition(new InstallProgressListener() {
340 @Override
341 public void installProgressUpdate(int done, int total,
342 String step) {
343 evt.setProgressEnd(total);
344 evt.setProgressValue(done);
345 evt.setProgressMessage(step);
346 }
347 });
348 }
349 }
350 }
351
352 @Override
[593]353 public void handleAbout(ApplicationEvent event) {
354 event.setHandled(true);
355 showAbout();
356 }
357
358 @Override
359 public void handleOpenApplication(ApplicationEvent event) {
360 }
361
362 @Override
363 public void handleOpenFile(ApplicationEvent event) {
364 }
365
366 @Override
367 public void handlePreferences(ApplicationEvent event) {
368 showSettings();
369 }
370
371 @Override
372 public void handlePrintFile(ApplicationEvent event) {
373 }
374
375 @Override
376 public void handleQuit(ApplicationEvent event) {
377 if (askClose()) {
378 event.setHandled(true);
379 saveLocalData();
380 exit();
381 } else {
382 event.setHandled(false);
383 }
384 }
385
386 @Override
387 public void handleReOpenApplication(ApplicationEvent event) {
388 }
[600]389
[592]390}
Note: See TracBrowser for help on using the repository browser.