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

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

AEI2.12:

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