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

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

AEI2.07:

  • Allow for non-ISO-8859-1 characters in properties files (more exactly require them to be UTF8)
File size: 14.4 KB
RevLine 
[631]1package net.oni2.aeinstaller.gui.modtable;
2
3import java.awt.Desktop;
[639]4import java.awt.Rectangle;
[631]5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
[639]7import java.awt.event.KeyAdapter;
8import java.awt.event.KeyEvent;
[631]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;
[659]18import java.util.Vector;
[631]19
[659]20import javax.swing.JCheckBoxMenuItem;
[631]21import javax.swing.JComponent;
22import javax.swing.JMenuItem;
[657]23import javax.swing.JOptionPane;
[631]24import javax.swing.JPopupMenu;
25import javax.swing.JTable;
[639]26import javax.swing.JViewport;
[631]27import javax.swing.ListSelectionModel;
28import javax.swing.RowSorter;
29import javax.swing.SortOrder;
30import javax.swing.event.ListSelectionEvent;
[645]31import javax.swing.event.RowSorterEvent;
[659]32import javax.swing.table.JTableHeader;
33import javax.swing.table.TableColumn;
34import javax.swing.table.TableColumnModel;
[631]35import javax.swing.table.TableRowSorter;
36
[720]37import net.oni2.SettingsManager;
[648]38import net.oni2.aeinstaller.backend.packages.Package;
39import net.oni2.aeinstaller.backend.packages.Type;
[673]40import net.oni2.aeinstaller.gui.downloadwindow.Downloader;
[840]41import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
[631]42
43/**
44 * @author Christian Illy
45 */
46public class ModTable extends JTable {
47 private static final long serialVersionUID = 1L;
48
[840]49 private ResourceBundle bundle = UTF8ResourceBundleLoader
[631]50 .getBundle("net.oni2.aeinstaller.localization.ModTable");
51
[657]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
[631]70 private HashSet<ModSelectionListener> modSelListeners = new HashSet<ModSelectionListener>();
71
72 private ModTableModel model;
73 private TableRowSorter<ModTableModel> sorter;
74
[657]75 private ETableContentType contentType = ETableContentType.MODS;
76
[631]77 /**
78 * Create a new ModTable
[658]79 *
80 * @param contentType
81 * Content to show
[631]82 */
[658]83 public ModTable(ETableContentType contentType) {
[631]84 super();
85
[658]86 this.contentType = contentType;
87
[631]88 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
[639]89 getSelectionModel().addListSelectionListener(this);
90 addMouseListener(new MouseEventHandler());
91 addKeyListener(new KeyEventHandler());
[631]92 // To get checkbox-cells with background of row
93 ((JComponent) getDefaultRenderer(Boolean.class)).setOpaque(true);
94
[658]95 model = new ModTableModel(contentType);
[631]96
97 setModel(model);
98
99 sorter = new TableRowSorter<ModTableModel>(model);
100 setRowSorter(sorter);
101
[660]102 setFilter(null, 0, null, EApplyFilterTo.ALL);
[631]103
104 List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
[645]105
[720]106 int sortCol = SettingsManager.getInstance().get("modSortColumn", 1);
[805]107 SortOrder sortOrder = SortOrder.valueOf(SettingsManager.getInstance()
108 .get("modSortOrder", "ASCENDING"));
[645]109
110 sortKeys.add(new RowSorter.SortKey(sortCol, sortOrder));
[631]111 sorter.setSortKeys(sortKeys);
112
113 for (int i = 0; i < model.getColumnCount(); i++) {
114 model.setColumnConstraints(i, getColumnModel().getColumn(i));
115 }
[658]116
[659]117 getTableHeader().addMouseListener(new HeaderMouseEventHandler());
118
[658]119 if (contentType != ETableContentType.MODS) {
120 getColumnModel().removeColumn(getColumnModel().getColumn(0));
121 }
[631]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) {
[648]131 final Package mod = (Package) getValueAt(r, -1);
[631]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
[648]167 private void notifyModSelectionListeners(Package m) {
[631]168 for (ModSelectionListener l : modSelListeners) {
169 l.modSelectionChanged(this, m);
170 }
171 }
172
173 /**
174 * @param listener
175 * Listener to add
176 */
[673]177 public void addDownloadSizeListener(ModInstallSelectionListener listener) {
[631]178 model.addDownloadSizeListener(listener);
179 }
180
181 /**
182 * @param listener
183 * Listener to remove
184 */
[673]185 public void removeDownloadSizeListener(ModInstallSelectionListener listener) {
[631]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 */
[648]216 public TreeSet<Package> getSelectedMods() {
[631]217 return model.getSelectedMods();
218 }
219
220 /**
221 * @param type
222 * Type of mods to show (null for all)
223 * @param downloadState
[805]224 * Show only: 0 = all, 1 = online, 2 = downloaded, 3 = installed
[660]225 * @param filterString
226 * String to filter on
227 * @param filterTo
228 * Fields to use string filter on
[631]229 */
[660]230 public void setFilter(Type type, int downloadState, String filterString,
231 EApplyFilterTo filterTo) {
[657]232 sorter.setRowFilter(new ModTableFilter(type, downloadState,
[660]233 filterString, filterTo, contentType == ETableContentType.CORE,
234 false));
[631]235 }
[639]236
237 @Override
[645]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();
[720]247 SettingsManager.getInstance().put("modSortColumn", col);
[805]248 SettingsManager.getInstance()
249 .put("modSortOrder", so.toString());
[645]250 }
251 }
252 }
253
[657]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);
[752]271 model.selectPackage(modRow, !isAll);
[657]272 }
[752]273 model.updateDownloadSize();
[657]274 invalidate();
275 repaint();
276 }
277
[645]278 @Override
[639]279 public void valueChanged(ListSelectionEvent e) {
280 super.valueChanged(e);
281 int viewRow = getSelectedRow();
282 if (viewRow < 0) {
283 notifyModSelectionListeners(null);
284 } else {
[648]285 Package mod = (Package) getValueAt(viewRow, -1);
[639]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) {
[648]301 final Package mod = (Package) getValueAt(rowindex, -1);
[639]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
[673]340 if (mod.getFile() != null) {
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);
[805]350 Downloader dl = new Downloader(toDo, deps,
351 false);
[673]352 try {
353 dl.setVisible(true);
354 } finally {
355 dl.dispose();
356 }
357 invalidate();
358 repaint();
359 }
360 });
361 popup.add(downloadPackage);
362 }
363
[657]364 if (mod.isLocalAvailable()
365 && contentType != ETableContentType.CORE) {
366 // Delete package folder item
367 JMenuItem deleteModFolder = new JMenuItem(
368 bundle.getString("deletePackage.text"));
369 deleteModFolder.addActionListener(new ActionListener() {
370 @Override
371 public void actionPerformed(ActionEvent arg0) {
372 if (mod.getNode() == null) {
373 JOptionPane.showMessageDialog(
374 null,
375 bundle.getString("deletePackageLocalOnly.text"),
376 bundle.getString("deletePackageLocalOnly.title"),
377 JOptionPane.INFORMATION_MESSAGE);
378 } else {
379 int res = JOptionPane.showConfirmDialog(
380 null,
381 bundle.getString("deletePackageConfirm.text"),
382 bundle.getString("deletePackageConfirm.title"),
383 JOptionPane.YES_NO_OPTION,
384 JOptionPane.WARNING_MESSAGE);
385 if (res == JOptionPane.YES_OPTION) {
386 mod.deleteLocalPackage();
387 invalidate();
388 repaint();
389 }
390 }
391 }
392 });
393 popup.add(deleteModFolder);
394 }
395
[639]396 if (popup.getSubElements().length > 0)
397 popup.show(e.getComponent(), e.getX(), e.getY());
398 }
399 }
400 }
401
402 @Override
403 public void mousePressed(MouseEvent e) {
404 mouseEventProcessing(e);
405 }
406
407 @Override
408 public void mouseReleased(MouseEvent e) {
409 mouseEventProcessing(e);
410 }
411 }
412
413 private class KeyEventHandler extends KeyAdapter {
[660]414 private void goToRow(int row) {
415 setRowSelectionInterval(row, row);
416 JViewport viewport = (JViewport) getParent();
417 Rectangle rect = getCellRect(row, 0, true);
418 Rectangle r2 = viewport.getVisibleRect();
419 scrollRectToVisible(new Rectangle(rect.x, rect.y,
420 (int) r2.getWidth(), (int) r2.getHeight()));
421 }
422
[639]423 @Override
424 public void keyTyped(KeyEvent e) {
425 super.keyTyped(e);
426
427 if (e.getModifiers() == 0) {
428 String key = String.valueOf(e.getKeyChar()).toLowerCase();
[660]429 int row = getSelectedRow();
430 if (row == (getRowCount() - 1))
431 row = -1;
432 for (int i = row + 1; i < getRowCount(); i++) {
[648]433 Package m = (Package) getValueAt(i, -1);
[639]434 if (m.getName().toLowerCase().startsWith(key)) {
[660]435 goToRow(i);
436 return;
[639]437 }
438 }
[660]439 if (row > 0) {
440 for (int i = 0; i < row; i++) {
441 Package m = (Package) getValueAt(i, -1);
442 if (m.getName().toLowerCase().startsWith(key)) {
443 goToRow(i);
444 return;
445 }
446 }
447 }
[639]448 }
449 }
450 }
[659]451
452 private class HeaderMouseEventHandler extends MouseAdapter {
453 private Vector<TableColumn> columns = new Vector<TableColumn>();
454 private TableColumnModel tcm = getColumnModel();
455
456 public HeaderMouseEventHandler() {
457 super();
458
459 for (int i = 1; i < tcm.getColumnCount(); i++) {
460 columns.add(tcm.getColumn(i));
461 }
462
463 for (int i = 1; i < columns.size(); i++) {
464 TableColumn tc = columns.get(i);
[720]465 if (!SettingsManager.getInstance().get(
[659]466 String.format("modShowColumn%02d", tc.getModelIndex()),
467 true))
468 tcm.removeColumn(tc);
469 }
470 }
471
472 private TableColumn getColumn(String name) {
473 for (TableColumn tc : columns) {
474 if (tc.getHeaderValue().equals(name)) {
475 return tc;
476 }
477 }
478 return null;
479 }
480
481 private int headerContains(TableColumn tc) {
482 for (int col = 0; col < tcm.getColumnCount(); col++) {
483 if (tcm.getColumn(col).equals(tc))
484 return col;
485 }
486 return -1;
487 }
488
489 private void mouseEventProcessing(MouseEvent e) {
490 if (e.isPopupTrigger() && e.getComponent() instanceof JTableHeader) {
491 JPopupMenu popup = new JPopupMenu();
492
493 ActionListener al = new ActionListener() {
494 @Override
495 public void actionPerformed(ActionEvent e) {
496 JCheckBoxMenuItem itm = (JCheckBoxMenuItem) e
497 .getSource();
498 TableColumn col = getColumn(itm.getText());
499 if (itm.isSelected()) {
500 tcm.addColumn(col);
501 for (int i = columns.indexOf(col) - 1; i >= 0; i--) {
502 int pos = headerContains(columns.get(i));
503 if (pos >= 0) {
504 tcm.moveColumn(tcm.getColumnCount() - 1,
505 pos + 1);
506 break;
507 }
508 }
509 } else {
510 tcm.removeColumn(col);
511 }
[720]512 SettingsManager.getInstance().put(
[659]513 String.format("modShowColumn%02d",
514 col.getModelIndex()), itm.isSelected());
515 }
516 };
517
518 for (int i = 1; i < columns.size(); i++) {
519 JCheckBoxMenuItem itm = new JCheckBoxMenuItem(
520 (String) columns.get(i).getHeaderValue());
521 itm.setSelected(headerContains(columns.get(i)) >= 0);
522
523 itm.addActionListener(al);
524 popup.add(itm);
525 }
526
527 if (popup.getSubElements().length > 0)
528 popup.show(e.getComponent(), e.getX(), e.getY());
529 }
530 }
531
532 @Override
533 public void mousePressed(MouseEvent e) {
534 mouseEventProcessing(e);
535 }
536
537 @Override
538 public void mouseReleased(MouseEvent e) {
539 mouseEventProcessing(e);
540 }
541 }
542
[631]543}
Note: See TracBrowser for help on using the repository browser.