| 1 | package net.oni2.aeinstaller.gui.modtable; | 
 
 
 
 
 | 2 |  | 
 
 
 
 
 | 3 | import java.awt.Desktop; | 
 
 
 
 
 | 4 | import java.awt.Rectangle; | 
 
 
 
 
 | 5 | import java.awt.event.ActionEvent; | 
 
 
 
 
 | 6 | import java.awt.event.ActionListener; | 
 
 
 
 
 | 7 | import java.awt.event.KeyAdapter; | 
 
 
 
 
 | 8 | import java.awt.event.KeyEvent; | 
 
 
 
 
 | 9 | import java.awt.event.MouseAdapter; | 
 
 
 
 
 | 10 | import java.awt.event.MouseEvent; | 
 
 
 
 
 | 11 | import java.io.File; | 
 
 
 
 
 | 12 | import java.io.IOException; | 
 
 
 
 
 | 13 | import java.util.ArrayList; | 
 
 
 
 
 | 14 | import java.util.HashSet; | 
 
 
 
 
 | 15 | import java.util.List; | 
 
 
 
 
 | 16 | import java.util.ResourceBundle; | 
 
 
 
 
 | 17 | import java.util.TreeSet; | 
 
 
 
 
 | 18 |  | 
 
 
 
 
 | 19 | import javax.swing.JComponent; | 
 
 
 
 
 | 20 | import javax.swing.JMenuItem; | 
 
 
 
 
 | 21 | import javax.swing.JPopupMenu; | 
 
 
 
 
 | 22 | import javax.swing.JTable; | 
 
 
 
 
 | 23 | import javax.swing.JViewport; | 
 
 
 
 
 | 24 | import javax.swing.ListSelectionModel; | 
 
 
 
 
 | 25 | import javax.swing.RowSorter; | 
 
 
 
 
 | 26 | import javax.swing.SortOrder; | 
 
 
 
 
 | 27 | import javax.swing.event.ListSelectionEvent; | 
 
 
 
 
 | 28 | import javax.swing.event.RowSorterEvent; | 
 
 
 
 
 | 29 | import javax.swing.table.TableRowSorter; | 
 
 
 
 
 | 30 |  | 
 
 
 
 
 | 31 | import net.oni2.aeinstaller.backend.Settings; | 
 
 
 
 
 | 32 | import net.oni2.aeinstaller.backend.mods.Mod; | 
 
 
 
 
 | 33 | import net.oni2.aeinstaller.backend.mods.Type; | 
 
 
 
 
 | 34 |  | 
 
 
 
 
 | 35 | /** | 
 
 
 
 
 | 36 | * @author Christian Illy | 
 
 
 
 
 | 37 | */ | 
 
 
 
 
 | 38 | public 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); | 
 
 
 
 
 | 56 | getSelectionModel().addListSelectionListener(this); | 
 
 
 
 
 | 57 | addMouseListener(new MouseEventHandler()); | 
 
 
 
 
 | 58 | addKeyListener(new KeyEventHandler()); | 
 
 
 
 
 | 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>(); | 
 
 
 
 
 | 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)); | 
 
 
 
 
 | 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 | } | 
 
 
 
 
 | 190 |  | 
 
 
 
 
 | 191 | @Override | 
 
 
 
 
 | 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 | 
 
 
 
 
 | 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(); | 
 
 
 
 
 | 298 | Rectangle rect = getCellRect(i, 0, true); | 
 
 
 
 
 | 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 | } | 
 
 
 
 
 | 308 | } |