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

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

AEI2: Looooots of refactorings for breaking out independent features into libraries

File size: 14.2 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.setValueAt(!isAll, modRow, 0);
270 }
271 invalidate();
272 repaint();
273 }
274
275 @Override
276 public void valueChanged(ListSelectionEvent e) {
277 super.valueChanged(e);
278 int viewRow = getSelectedRow();
279 if (viewRow < 0) {
280 notifyModSelectionListeners(null);
281 } else {
282 Package mod = (Package) getValueAt(viewRow, -1);
283 notifyModSelectionListeners(mod);
284 }
285 }
286
287 private class MouseEventHandler extends MouseAdapter {
288 private void mouseEventProcessing(MouseEvent e) {
289 int r = rowAtPoint(e.getPoint());
290 if (r >= 0 && r < getRowCount())
291 setRowSelectionInterval(r, r);
292 else
293 clearSelection();
294
295 int rowindex = getSelectedRow();
296 if (rowindex >= 0) {
297 if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
298 final Package mod = (Package) getValueAt(rowindex, -1);
299
300 JPopupMenu popup = new JPopupMenu();
301
302 if (mod.isLocalAvailable()) {
303 // Open package folder item
304 JMenuItem openModFolder = new JMenuItem(
305 bundle.getString("openModFolder.text"));
306 openModFolder.addActionListener(new ActionListener() {
307 @Override
308 public void actionPerformed(ActionEvent arg0) {
309 try {
310 Desktop.getDesktop().open(
311 mod.getLocalPath());
312 } catch (IOException e) {
313 e.printStackTrace();
314 }
315 }
316 });
317 popup.add(openModFolder);
318 }
319
320 if (mod.getUrl() != null) {
321 // Open Depot page item
322 JMenuItem openDepotPage = new JMenuItem(
323 bundle.getString("openDepotPage.text"));
324 openDepotPage.addActionListener(new ActionListener() {
325 @Override
326 public void actionPerformed(ActionEvent arg0) {
327 try {
328 Desktop.getDesktop().browse(mod.getUrl());
329 } catch (IOException e) {
330 e.printStackTrace();
331 }
332 }
333 });
334 popup.add(openDepotPage);
335 }
336
337 if (mod.getFile() != null) {
338 // Download package
339 JMenuItem downloadPackage = new JMenuItem(
340 bundle.getString("downloadPackage.text"));
341 downloadPackage.addActionListener(new ActionListener() {
342 @Override
343 public void actionPerformed(ActionEvent arg0) {
344 TreeSet<Package> toDo = new TreeSet<Package>();
345 TreeSet<Package> deps = new TreeSet<Package>();
346 toDo.add(mod);
347 Downloader dl = new Downloader(toDo, deps);
348 try {
349 dl.setVisible(true);
350 } finally {
351 dl.dispose();
352 }
353 invalidate();
354 repaint();
355 }
356 });
357 popup.add(downloadPackage);
358 }
359
360 if (mod.isLocalAvailable()
361 && contentType != ETableContentType.CORE) {
362 // Delete package folder item
363 JMenuItem deleteModFolder = new JMenuItem(
364 bundle.getString("deletePackage.text"));
365 deleteModFolder.addActionListener(new ActionListener() {
366 @Override
367 public void actionPerformed(ActionEvent arg0) {
368 if (mod.getNode() == null) {
369 JOptionPane.showMessageDialog(
370 null,
371 bundle.getString("deletePackageLocalOnly.text"),
372 bundle.getString("deletePackageLocalOnly.title"),
373 JOptionPane.INFORMATION_MESSAGE);
374 } else {
375 int res = JOptionPane.showConfirmDialog(
376 null,
377 bundle.getString("deletePackageConfirm.text"),
378 bundle.getString("deletePackageConfirm.title"),
379 JOptionPane.YES_NO_OPTION,
380 JOptionPane.WARNING_MESSAGE);
381 if (res == JOptionPane.YES_OPTION) {
382 mod.deleteLocalPackage();
383 invalidate();
384 repaint();
385 }
386 }
387 }
388 });
389 popup.add(deleteModFolder);
390 }
391
392 if (popup.getSubElements().length > 0)
393 popup.show(e.getComponent(), e.getX(), e.getY());
394 }
395 }
396 }
397
398 @Override
399 public void mousePressed(MouseEvent e) {
400 mouseEventProcessing(e);
401 }
402
403 @Override
404 public void mouseReleased(MouseEvent e) {
405 mouseEventProcessing(e);
406 }
407 }
408
409 private class KeyEventHandler extends KeyAdapter {
410 private void goToRow(int row) {
411 setRowSelectionInterval(row, row);
412 JViewport viewport = (JViewport) getParent();
413 Rectangle rect = getCellRect(row, 0, true);
414 Rectangle r2 = viewport.getVisibleRect();
415 scrollRectToVisible(new Rectangle(rect.x, rect.y,
416 (int) r2.getWidth(), (int) r2.getHeight()));
417 }
418
419 @Override
420 public void keyTyped(KeyEvent e) {
421 super.keyTyped(e);
422
423 if (e.getModifiers() == 0) {
424 String key = String.valueOf(e.getKeyChar()).toLowerCase();
425 int row = getSelectedRow();
426 if (row == (getRowCount() - 1))
427 row = -1;
428 for (int i = row + 1; i < getRowCount(); i++) {
429 Package m = (Package) getValueAt(i, -1);
430 if (m.getName().toLowerCase().startsWith(key)) {
431 goToRow(i);
432 return;
433 }
434 }
435 if (row > 0) {
436 for (int i = 0; i < row; i++) {
437 Package m = (Package) getValueAt(i, -1);
438 if (m.getName().toLowerCase().startsWith(key)) {
439 goToRow(i);
440 return;
441 }
442 }
443 }
444 }
445 }
446 }
447
448 private class HeaderMouseEventHandler extends MouseAdapter {
449 private Vector<TableColumn> columns = new Vector<TableColumn>();
450 private TableColumnModel tcm = getColumnModel();
451
452 public HeaderMouseEventHandler() {
453 super();
454
455 for (int i = 1; i < tcm.getColumnCount(); i++) {
456 columns.add(tcm.getColumn(i));
457 }
458
459 for (int i = 1; i < columns.size(); i++) {
460 TableColumn tc = columns.get(i);
461 if (!SettingsManager.getInstance().get(
462 String.format("modShowColumn%02d", tc.getModelIndex()),
463 true))
464 tcm.removeColumn(tc);
465 }
466 }
467
468 private TableColumn getColumn(String name) {
469 for (TableColumn tc : columns) {
470 if (tc.getHeaderValue().equals(name)) {
471 return tc;
472 }
473 }
474 return null;
475 }
476
477 private int headerContains(TableColumn tc) {
478 for (int col = 0; col < tcm.getColumnCount(); col++) {
479 if (tcm.getColumn(col).equals(tc))
480 return col;
481 }
482 return -1;
483 }
484
485 private void mouseEventProcessing(MouseEvent e) {
486 if (e.isPopupTrigger() && e.getComponent() instanceof JTableHeader) {
487 JPopupMenu popup = new JPopupMenu();
488
489 ActionListener al = new ActionListener() {
490 @Override
491 public void actionPerformed(ActionEvent e) {
492 JCheckBoxMenuItem itm = (JCheckBoxMenuItem) e
493 .getSource();
494 TableColumn col = getColumn(itm.getText());
495 if (itm.isSelected()) {
496 tcm.addColumn(col);
497 for (int i = columns.indexOf(col) - 1; i >= 0; i--) {
498 int pos = headerContains(columns.get(i));
499 if (pos >= 0) {
500 tcm.moveColumn(tcm.getColumnCount() - 1,
501 pos + 1);
502 break;
503 }
504 }
505 } else {
506 tcm.removeColumn(col);
507 }
508 SettingsManager.getInstance().put(
509 String.format("modShowColumn%02d",
510 col.getModelIndex()), itm.isSelected());
511 }
512 };
513
514 for (int i = 1; i < columns.size(); i++) {
515 JCheckBoxMenuItem itm = new JCheckBoxMenuItem(
516 (String) columns.get(i).getHeaderValue());
517 itm.setSelected(headerContains(columns.get(i)) >= 0);
518
519 itm.addActionListener(al);
520 popup.add(itm);
521 }
522
523 if (popup.getSubElements().length > 0)
524 popup.show(e.getComponent(), e.getX(), e.getY());
525 }
526 }
527
528 @Override
529 public void mousePressed(MouseEvent e) {
530 mouseEventProcessing(e);
531 }
532
533 @Override
534 public void mouseReleased(MouseEvent e) {
535 mouseEventProcessing(e);
536 }
537 }
538
539}
Note: See TracBrowser for help on using the repository browser.