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

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

AEI2.02:

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