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

Last change on this file since 647 was 645, checked in by alloc, 12 years ago

AEI2 0.99b:

  • Show updated mandatory packages
  • About: Added OCF-link
  • Saving table sort across sessions
File size: 8.1 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;
18
19import javax.swing.JComponent;
20import javax.swing.JMenuItem;
21import javax.swing.JPopupMenu;
22import javax.swing.JTable;
[639]23import javax.swing.JViewport;
[631]24import javax.swing.ListSelectionModel;
25import javax.swing.RowSorter;
26import javax.swing.SortOrder;
27import javax.swing.event.ListSelectionEvent;
[645]28import javax.swing.event.RowSorterEvent;
[631]29import javax.swing.table.TableRowSorter;
30
[645]31import net.oni2.aeinstaller.backend.Settings;
[631]32import net.oni2.aeinstaller.backend.mods.Mod;
33import net.oni2.aeinstaller.backend.mods.Type;
34
35/**
36 * @author Christian Illy
37 */
38public class ModTable extends JTable {
39 private static final long serialVersionUID = 1L;
40
41 private ResourceBundle bundle = ResourceBundle
42 .getBundle("net.oni2.aeinstaller.localization.ModTable");
43
44 private HashSet<ModSelectionListener> modSelListeners = new HashSet<ModSelectionListener>();
45
46 private ModTableModel model;
47 private TableRowSorter<ModTableModel> sorter;
48
49 /**
50 * Create a new ModTable
51 */
52 public ModTable() {
53 super();
54
55 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
[639]56 getSelectionModel().addListSelectionListener(this);
57 addMouseListener(new MouseEventHandler());
58 addKeyListener(new KeyEventHandler());
[631]59 // To get checkbox-cells with background of row
60 ((JComponent) getDefaultRenderer(Boolean.class)).setOpaque(true);
61
62 model = new ModTableModel();
63
64 setModel(model);
65
66 sorter = new TableRowSorter<ModTableModel>(model);
67 setRowSorter(sorter);
68
69 setFilter(null, 0);
70
71 List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
[645]72
73 int sortCol = Settings.getInstance().get("modSortColumn", 1);
74 SortOrder sortOrder = SortOrder.valueOf(Settings.getInstance().get(
75 "modSortOrder", "ASCENDING"));
76
77 sortKeys.add(new RowSorter.SortKey(sortCol, sortOrder));
[631]78 sorter.setSortKeys(sortKeys);
79
80 for (int i = 0; i < model.getColumnCount(); i++) {
81 model.setColumnConstraints(i, getColumnModel().getColumn(i));
82 }
83 }
84
85 @Override
86 public String getToolTipText(MouseEvent e) {
87 int r = rowAtPoint(e.getPoint());
88 int c = columnAtPoint(e.getPoint());
89 if (r >= 0 && r < getRowCount()) {
90 int modelCol = convertColumnIndexToModel(c);
91 if (modelCol == 4) {
92 final Mod mod = (Mod) getValueAt(r, -1);
93
94 String tt = "<html>";
95 tt += String.format("%s: %s<br>",
96 bundle.getString("state.installed"),
97 bundle.getString((mod.isInstalled() ? "yes" : "no")));
98 tt += String.format(
99 "%s: %s<br>",
100 bundle.getString("state.updatable"),
101 bundle.getString((mod.isLocalAvailable()
102 && mod.isNewerAvailable() ? "yes" : "no")));
103 tt += String.format("%s: %s</html>", bundle
104 .getString("state.downloaded"), bundle.getString((mod
105 .isLocalAvailable() ? "yes" : "no")));
106 return tt;
107 }
108 }
109 return super.getToolTipText(e);
110 }
111
112 /**
113 * @param listener
114 * Listener to add
115 */
116 public void addModSelectionListener(ModSelectionListener listener) {
117 modSelListeners.add(listener);
118 }
119
120 /**
121 * @param listener
122 * Listener to remove
123 */
124 public void removeModSelectionListener(ModSelectionListener listener) {
125 modSelListeners.remove(listener);
126 }
127
128 private void notifyModSelectionListeners(Mod m) {
129 for (ModSelectionListener l : modSelListeners) {
130 l.modSelectionChanged(this, m);
131 }
132 }
133
134 /**
135 * @param listener
136 * Listener to add
137 */
138 public void addDownloadSizeListener(DownloadSizeListener listener) {
139 model.addDownloadSizeListener(listener);
140 }
141
142 /**
143 * @param listener
144 * Listener to remove
145 */
146 public void removeDownloadSizeListener(DownloadSizeListener listener) {
147 model.removeDownloadSizeListener(listener);
148 }
149
150 /**
151 * Reload the nodes data after an update to the cache
152 */
153 public void reloadData() {
154 model.reloadData();
155 }
156
157 /**
158 * Revert the selection to the mods that are currently installed
159 */
160 public void revertSelection() {
161 model.revertSelection();
162 }
163
164 /**
165 * Reload the selection after a config was loaded
166 *
167 * @param config
168 * Config to load
169 */
170 public void reloadSelection(File config) {
171 model.reloadSelection(config);
172 }
173
174 /**
175 * @return Mods selected for installation
176 */
177 public TreeSet<Mod> getSelectedMods() {
178 return model.getSelectedMods();
179 }
180
181 /**
182 * @param type
183 * Type of mods to show (null for all)
184 * @param downloadState
185 * Show only: 0 = all, 1 = online, 2 = downloaded
186 */
187 public void setFilter(Type type, int downloadState) {
188 sorter.setRowFilter(new ModTableFilter(type, downloadState));
189 }
[639]190
191 @Override
[645]192 public void sorterChanged(RowSorterEvent evt) {
193 super.sorterChanged(evt);
194 if (evt.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
195 @SuppressWarnings("unchecked")
196 RowSorter<ModTableModel> rs = (RowSorter<ModTableModel>) getRowSorter();
197 List<? extends RowSorter.SortKey> keys = rs.getSortKeys();
198 if (keys.size() > 0) {
199 int col = keys.get(0).getColumn();
200 SortOrder so = keys.get(0).getSortOrder();
201 Settings.getInstance().put("modSortColumn", col);
202 Settings.getInstance().put("modSortOrder", so.toString());
203 }
204 }
205 }
206
207 @Override
[639]208 public void valueChanged(ListSelectionEvent e) {
209 super.valueChanged(e);
210 int viewRow = getSelectedRow();
211 if (viewRow < 0) {
212 notifyModSelectionListeners(null);
213 } else {
214 Mod mod = (Mod) getValueAt(viewRow, -1);
215 notifyModSelectionListeners(mod);
216 }
217 }
218
219 private class MouseEventHandler extends MouseAdapter {
220 private void mouseEventProcessing(MouseEvent e) {
221 int r = rowAtPoint(e.getPoint());
222 if (r >= 0 && r < getRowCount())
223 setRowSelectionInterval(r, r);
224 else
225 clearSelection();
226
227 int rowindex = getSelectedRow();
228 if (rowindex >= 0) {
229 if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
230 final Mod mod = (Mod) getValueAt(rowindex, -1);
231
232 JPopupMenu popup = new JPopupMenu();
233
234 if (mod.isLocalAvailable()) {
235 // Open package folder item
236 JMenuItem openModFolder = new JMenuItem(
237 bundle.getString("openModFolder.text"));
238 openModFolder.addActionListener(new ActionListener() {
239 @Override
240 public void actionPerformed(ActionEvent arg0) {
241 try {
242 Desktop.getDesktop().open(
243 mod.getLocalPath());
244 } catch (IOException e) {
245 e.printStackTrace();
246 }
247 }
248 });
249 popup.add(openModFolder);
250 }
251
252 if (mod.getUrl() != null) {
253 // Open Depot page item
254 JMenuItem openDepotPage = new JMenuItem(
255 bundle.getString("openDepotPage.text"));
256 openDepotPage.addActionListener(new ActionListener() {
257 @Override
258 public void actionPerformed(ActionEvent arg0) {
259 try {
260 Desktop.getDesktop().browse(mod.getUrl());
261 } catch (IOException e) {
262 e.printStackTrace();
263 }
264 }
265 });
266 popup.add(openDepotPage);
267 }
268
269 if (popup.getSubElements().length > 0)
270 popup.show(e.getComponent(), e.getX(), e.getY());
271 }
272 }
273 }
274
275 @Override
276 public void mousePressed(MouseEvent e) {
277 mouseEventProcessing(e);
278 }
279
280 @Override
281 public void mouseReleased(MouseEvent e) {
282 mouseEventProcessing(e);
283 }
284 }
285
286 private class KeyEventHandler extends KeyAdapter {
287 @Override
288 public void keyTyped(KeyEvent e) {
289 super.keyTyped(e);
290
291 if (e.getModifiers() == 0) {
292 String key = String.valueOf(e.getKeyChar()).toLowerCase();
293 for (int i = 0; i < getRowCount(); i++) {
294 Mod m = (Mod) getValueAt(i, -1);
295 if (m.getName().toLowerCase().startsWith(key)) {
296 setRowSelectionInterval(i, i);
297 JViewport viewport = (JViewport) getParent();
[645]298 Rectangle rect = getCellRect(i, 0, true);
[639]299 Rectangle r2 = viewport.getVisibleRect();
300 scrollRectToVisible(new Rectangle(rect.x, rect.y,
301 (int) r2.getWidth(), (int) r2.getHeight()));
302 break;
303 }
304 }
305 }
306 }
307 }
[631]308}
Note: See TracBrowser for help on using the repository browser.