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