source: java/installer2/src/net/oni2/aeinstaller/gui/modtable/ModTable.java@ 782

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

AEI2.00:

  • Core package download in download window
File size: 14.3 KB
Line 
1package net.oni2.aeinstaller.gui.modtable;
2
3import java.awt.Desktop;
4import java.awt.Rectangle;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.KeyAdapter;
8import java.awt.event.KeyEvent;
9import java.awt.event.MouseAdapter;
10import java.awt.event.MouseEvent;
11import java.io.File;
12import java.io.IOException;
13import java.util.ArrayList;
14import java.util.HashSet;
15import java.util.List;
16import java.util.ResourceBundle;
17import java.util.TreeSet;
18import java.util.Vector;
19
20import javax.swing.JCheckBoxMenuItem;
21import javax.swing.JComponent;
22import javax.swing.JMenuItem;
23import javax.swing.JOptionPane;
24import javax.swing.JPopupMenu;
25import javax.swing.JTable;
26import javax.swing.JViewport;
27import javax.swing.ListSelectionModel;
28import javax.swing.RowSorter;
29import javax.swing.SortOrder;
30import javax.swing.event.ListSelectionEvent;
31import javax.swing.event.RowSorterEvent;
32import javax.swing.table.JTableHeader;
33import javax.swing.table.TableColumn;
34import javax.swing.table.TableColumnModel;
35import javax.swing.table.TableRowSorter;
36
37import net.oni2.SettingsManager;
38import net.oni2.aeinstaller.backend.packages.Package;
39import net.oni2.aeinstaller.backend.packages.Type;
40import net.oni2.aeinstaller.gui.downloadwindow.Downloader;
41
42/**
43 * @author Christian Illy
44 */
45public class ModTable extends JTable {
46 private static final long serialVersionUID = 1L;
47
48 private ResourceBundle bundle = ResourceBundle
49 .getBundle("net.oni2.aeinstaller.localization.ModTable");
50
51 /**
52 * @author Christian Illy
53 */
54 public enum ETableContentType {
55 /**
56 * Table showing mods
57 */
58 MODS,
59 /**
60 * Table showing tools
61 */
62 TOOLS,
63 /**
64 * Table showing core packages
65 */
66 CORE
67 };
68
69 private HashSet<ModSelectionListener> modSelListeners = new HashSet<ModSelectionListener>();
70
71 private ModTableModel model;
72 private TableRowSorter<ModTableModel> sorter;
73
74 private ETableContentType contentType = ETableContentType.MODS;
75
76 /**
77 * Create a new ModTable
78 *
79 * @param contentType
80 * Content to show
81 */
82 public ModTable(ETableContentType contentType) {
83 super();
84
85 this.contentType = contentType;
86
87 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
88 getSelectionModel().addListSelectionListener(this);
89 addMouseListener(new MouseEventHandler());
90 addKeyListener(new KeyEventHandler());
91 // To get checkbox-cells with background of row
92 ((JComponent) getDefaultRenderer(Boolean.class)).setOpaque(true);
93
94 model = new ModTableModel(contentType);
95
96 setModel(model);
97
98 sorter = new TableRowSorter<ModTableModel>(model);
99 setRowSorter(sorter);
100
101 setFilter(null, 0, null, EApplyFilterTo.ALL);
102
103 List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
104
105 int sortCol = SettingsManager.getInstance().get("modSortColumn", 1);
106 SortOrder sortOrder = SortOrder.valueOf(SettingsManager.getInstance().get(
107 "modSortOrder", "ASCENDING"));
108
109 sortKeys.add(new RowSorter.SortKey(sortCol, sortOrder));
110 sorter.setSortKeys(sortKeys);
111
112 for (int i = 0; i < model.getColumnCount(); i++) {
113 model.setColumnConstraints(i, getColumnModel().getColumn(i));
114 }
115
116 getTableHeader().addMouseListener(new HeaderMouseEventHandler());
117
118 if (contentType != ETableContentType.MODS) {
119 getColumnModel().removeColumn(getColumnModel().getColumn(0));
120 }
121 }
122
123 @Override
124 public String getToolTipText(MouseEvent e) {
125 int r = rowAtPoint(e.getPoint());
126 int c = columnAtPoint(e.getPoint());
127 if (r >= 0 && r < getRowCount()) {
128 int modelCol = convertColumnIndexToModel(c);
129 if (modelCol == 4) {
130 final Package mod = (Package) getValueAt(r, -1);
131
132 String tt = "<html>";
133 tt += String.format("%s: %s<br>",
134 bundle.getString("state.installed"),
135 bundle.getString((mod.isInstalled() ? "yes" : "no")));
136 tt += String.format(
137 "%s: %s<br>",
138 bundle.getString("state.updatable"),
139 bundle.getString((mod.isLocalAvailable()
140 && mod.isNewerAvailable() ? "yes" : "no")));
141 tt += String.format("%s: %s</html>", bundle
142 .getString("state.downloaded"), bundle.getString((mod
143 .isLocalAvailable() ? "yes" : "no")));
144 return tt;
145 }
146 }
147 return super.getToolTipText(e);
148 }
149
150 /**
151 * @param listener
152 * Listener to add
153 */
154 public void addModSelectionListener(ModSelectionListener listener) {
155 modSelListeners.add(listener);
156 }
157
158 /**
159 * @param listener
160 * Listener to remove
161 */
162 public void removeModSelectionListener(ModSelectionListener listener) {
163 modSelListeners.remove(listener);
164 }
165
166 private void notifyModSelectionListeners(Package m) {
167 for (ModSelectionListener l : modSelListeners) {
168 l.modSelectionChanged(this, m);
169 }
170 }
171
172 /**
173 * @param listener
174 * Listener to add
175 */
176 public void addDownloadSizeListener(ModInstallSelectionListener listener) {
177 model.addDownloadSizeListener(listener);
178 }
179
180 /**
181 * @param listener
182 * Listener to remove
183 */
184 public void removeDownloadSizeListener(ModInstallSelectionListener listener) {
185 model.removeDownloadSizeListener(listener);
186 }
187
188 /**
189 * Reload the nodes data after an update to the cache
190 */
191 public void reloadData() {
192 model.reloadData();
193 }
194
195 /**
196 * Revert the selection to the mods that are currently installed
197 */
198 public void revertSelection() {
199 model.revertSelection();
200 }
201
202 /**
203 * Reload the selection after a config was loaded
204 *
205 * @param config
206 * Config to load
207 */
208 public void reloadSelection(File config) {
209 model.reloadSelection(config);
210 }
211
212 /**
213 * @return Mods selected for installation
214 */
215 public TreeSet<Package> getSelectedMods() {
216 return model.getSelectedMods();
217 }
218
219 /**
220 * @param type
221 * Type of mods to show (null for all)
222 * @param downloadState
223 * Show only: 0 = all, 1 = online, 2 = downloaded
224 * @param filterString
225 * String to filter on
226 * @param filterTo
227 * Fields to use string filter on
228 */
229 public void setFilter(Type type, int downloadState, String filterString,
230 EApplyFilterTo filterTo) {
231 sorter.setRowFilter(new ModTableFilter(type, downloadState,
232 filterString, filterTo, contentType == ETableContentType.CORE,
233 false));
234 }
235
236 @Override
237 public void sorterChanged(RowSorterEvent evt) {
238 super.sorterChanged(evt);
239 if (evt.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
240 @SuppressWarnings("unchecked")
241 RowSorter<ModTableModel> rs = (RowSorter<ModTableModel>) getRowSorter();
242 List<? extends RowSorter.SortKey> keys = rs.getSortKeys();
243 if (keys.size() > 0) {
244 int col = keys.get(0).getColumn();
245 SortOrder so = keys.get(0).getSortOrder();
246 SettingsManager.getInstance().put("modSortColumn", col);
247 SettingsManager.getInstance().put("modSortOrder", so.toString());
248 }
249 }
250 }
251
252 /**
253 * Select/Unselect all currently visible items depending on the current
254 * state of selection
255 */
256 public void unSelectAll() {
257 boolean isAll = true;
258 for (int i = 0; i < getRowCount(); i++) {
259 int modRow = convertRowIndexToModel(i);
260 boolean inst = (Boolean) model.getValueAt(modRow, 0);
261 if (!inst) {
262 isAll = false;
263 break;
264 }
265 }
266
267 for (int i = 0; i < getRowCount(); i++) {
268 int modRow = convertRowIndexToModel(i);
269 model.selectPackage(modRow, !isAll);
270 }
271 model.updateDownloadSize();
272 invalidate();
273 repaint();
274 }
275
276 @Override
277 public void valueChanged(ListSelectionEvent e) {
278 super.valueChanged(e);
279 int viewRow = getSelectedRow();
280 if (viewRow < 0) {
281 notifyModSelectionListeners(null);
282 } else {
283 Package mod = (Package) getValueAt(viewRow, -1);
284 notifyModSelectionListeners(mod);
285 }
286 }
287
288 private class MouseEventHandler extends MouseAdapter {
289 private void mouseEventProcessing(MouseEvent e) {
290 int r = rowAtPoint(e.getPoint());
291 if (r >= 0 && r < getRowCount())
292 setRowSelectionInterval(r, r);
293 else
294 clearSelection();
295
296 int rowindex = getSelectedRow();
297 if (rowindex >= 0) {
298 if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
299 final Package mod = (Package) getValueAt(rowindex, -1);
300
301 JPopupMenu popup = new JPopupMenu();
302
303 if (mod.isLocalAvailable()) {
304 // Open package folder item
305 JMenuItem openModFolder = new JMenuItem(
306 bundle.getString("openModFolder.text"));
307 openModFolder.addActionListener(new ActionListener() {
308 @Override
309 public void actionPerformed(ActionEvent arg0) {
310 try {
311 Desktop.getDesktop().open(
312 mod.getLocalPath());
313 } catch (IOException e) {
314 e.printStackTrace();
315 }
316 }
317 });
318 popup.add(openModFolder);
319 }
320
321 if (mod.getUrl() != null) {
322 // Open Depot page item
323 JMenuItem openDepotPage = new JMenuItem(
324 bundle.getString("openDepotPage.text"));
325 openDepotPage.addActionListener(new ActionListener() {
326 @Override
327 public void actionPerformed(ActionEvent arg0) {
328 try {
329 Desktop.getDesktop().browse(mod.getUrl());
330 } catch (IOException e) {
331 e.printStackTrace();
332 }
333 }
334 });
335 popup.add(openDepotPage);
336 }
337
338 if (mod.getFile() != null) {
339 // Download package
340 JMenuItem downloadPackage = new JMenuItem(
341 bundle.getString("downloadPackage.text"));
342 downloadPackage.addActionListener(new ActionListener() {
343 @Override
344 public void actionPerformed(ActionEvent arg0) {
345 TreeSet<Package> toDo = new TreeSet<Package>();
346 TreeSet<Package> deps = new TreeSet<Package>();
347 toDo.add(mod);
348 Downloader dl = new Downloader(toDo, deps, false);
349 try {
350 dl.setVisible(true);
351 } finally {
352 dl.dispose();
353 }
354 invalidate();
355 repaint();
356 }
357 });
358 popup.add(downloadPackage);
359 }
360
361 if (mod.isLocalAvailable()
362 && contentType != ETableContentType.CORE) {
363 // Delete package folder item
364 JMenuItem deleteModFolder = new JMenuItem(
365 bundle.getString("deletePackage.text"));
366 deleteModFolder.addActionListener(new ActionListener() {
367 @Override
368 public void actionPerformed(ActionEvent arg0) {
369 if (mod.getNode() == null) {
370 JOptionPane.showMessageDialog(
371 null,
372 bundle.getString("deletePackageLocalOnly.text"),
373 bundle.getString("deletePackageLocalOnly.title"),
374 JOptionPane.INFORMATION_MESSAGE);
375 } else {
376 int res = JOptionPane.showConfirmDialog(
377 null,
378 bundle.getString("deletePackageConfirm.text"),
379 bundle.getString("deletePackageConfirm.title"),
380 JOptionPane.YES_NO_OPTION,
381 JOptionPane.WARNING_MESSAGE);
382 if (res == JOptionPane.YES_OPTION) {
383 mod.deleteLocalPackage();
384 invalidate();
385 repaint();
386 }
387 }
388 }
389 });
390 popup.add(deleteModFolder);
391 }
392
393 if (popup.getSubElements().length > 0)
394 popup.show(e.getComponent(), e.getX(), e.getY());
395 }
396 }
397 }
398
399 @Override
400 public void mousePressed(MouseEvent e) {
401 mouseEventProcessing(e);
402 }
403
404 @Override
405 public void mouseReleased(MouseEvent e) {
406 mouseEventProcessing(e);
407 }
408 }
409
410 private class KeyEventHandler extends KeyAdapter {
411 private void goToRow(int row) {
412 setRowSelectionInterval(row, row);
413 JViewport viewport = (JViewport) getParent();
414 Rectangle rect = getCellRect(row, 0, true);
415 Rectangle r2 = viewport.getVisibleRect();
416 scrollRectToVisible(new Rectangle(rect.x, rect.y,
417 (int) r2.getWidth(), (int) r2.getHeight()));
418 }
419
420 @Override
421 public void keyTyped(KeyEvent e) {
422 super.keyTyped(e);
423
424 if (e.getModifiers() == 0) {
425 String key = String.valueOf(e.getKeyChar()).toLowerCase();
426 int row = getSelectedRow();
427 if (row == (getRowCount() - 1))
428 row = -1;
429 for (int i = row + 1; i < getRowCount(); i++) {
430 Package m = (Package) getValueAt(i, -1);
431 if (m.getName().toLowerCase().startsWith(key)) {
432 goToRow(i);
433 return;
434 }
435 }
436 if (row > 0) {
437 for (int i = 0; i < row; i++) {
438 Package m = (Package) getValueAt(i, -1);
439 if (m.getName().toLowerCase().startsWith(key)) {
440 goToRow(i);
441 return;
442 }
443 }
444 }
445 }
446 }
447 }
448
449 private class HeaderMouseEventHandler extends MouseAdapter {
450 private Vector<TableColumn> columns = new Vector<TableColumn>();
451 private TableColumnModel tcm = getColumnModel();
452
453 public HeaderMouseEventHandler() {
454 super();
455
456 for (int i = 1; i < tcm.getColumnCount(); i++) {
457 columns.add(tcm.getColumn(i));
458 }
459
460 for (int i = 1; i < columns.size(); i++) {
461 TableColumn tc = columns.get(i);
462 if (!SettingsManager.getInstance().get(
463 String.format("modShowColumn%02d", tc.getModelIndex()),
464 true))
465 tcm.removeColumn(tc);
466 }
467 }
468
469 private TableColumn getColumn(String name) {
470 for (TableColumn tc : columns) {
471 if (tc.getHeaderValue().equals(name)) {
472 return tc;
473 }
474 }
475 return null;
476 }
477
478 private int headerContains(TableColumn tc) {
479 for (int col = 0; col < tcm.getColumnCount(); col++) {
480 if (tcm.getColumn(col).equals(tc))
481 return col;
482 }
483 return -1;
484 }
485
486 private void mouseEventProcessing(MouseEvent e) {
487 if (e.isPopupTrigger() && e.getComponent() instanceof JTableHeader) {
488 JPopupMenu popup = new JPopupMenu();
489
490 ActionListener al = new ActionListener() {
491 @Override
492 public void actionPerformed(ActionEvent e) {
493 JCheckBoxMenuItem itm = (JCheckBoxMenuItem) e
494 .getSource();
495 TableColumn col = getColumn(itm.getText());
496 if (itm.isSelected()) {
497 tcm.addColumn(col);
498 for (int i = columns.indexOf(col) - 1; i >= 0; i--) {
499 int pos = headerContains(columns.get(i));
500 if (pos >= 0) {
501 tcm.moveColumn(tcm.getColumnCount() - 1,
502 pos + 1);
503 break;
504 }
505 }
506 } else {
507 tcm.removeColumn(col);
508 }
509 SettingsManager.getInstance().put(
510 String.format("modShowColumn%02d",
511 col.getModelIndex()), itm.isSelected());
512 }
513 };
514
515 for (int i = 1; i < columns.size(); i++) {
516 JCheckBoxMenuItem itm = new JCheckBoxMenuItem(
517 (String) columns.get(i).getHeaderValue());
518 itm.setSelected(headerContains(columns.get(i)) >= 0);
519
520 itm.addActionListener(al);
521 popup.add(itm);
522 }
523
524 if (popup.getSubElements().length > 0)
525 popup.show(e.getComponent(), e.getX(), e.getY());
526 }
527 }
528
529 @Override
530 public void mousePressed(MouseEvent e) {
531 mouseEventProcessing(e);
532 }
533
534 @Override
535 public void mouseReleased(MouseEvent e) {
536 mouseEventProcessing(e);
537 }
538 }
539
540}
Note: See TracBrowser for help on using the repository browser.