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

Last change on this file since 641 was 639, checked in by alloc, 12 years ago

AEI2 0.96:

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