[631] | 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 | if (mod.isLocalAvailable()) {
|
---|
| 79 | JPopupMenu popup = new JPopupMenu();
|
---|
| 80 | JMenuItem openModFolder = new JMenuItem(
|
---|
| 81 | bundle.getString("openModFolder.text"));
|
---|
| 82 | openModFolder
|
---|
| 83 | .addActionListener(new ActionListener() {
|
---|
| 84 | @Override
|
---|
| 85 | public void actionPerformed(
|
---|
| 86 | ActionEvent arg0) {
|
---|
| 87 | try {
|
---|
| 88 | Desktop.getDesktop().open(
|
---|
| 89 | mod.getLocalPath());
|
---|
| 90 | } catch (IOException e) {
|
---|
| 91 | e.printStackTrace();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | });
|
---|
| 95 | popup.add(openModFolder);
|
---|
| 96 | popup.show(e.getComponent(), e.getX(), e.getY());
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | @Override
|
---|
| 103 | public void mousePressed(MouseEvent e) {
|
---|
| 104 | common(e);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Override
|
---|
| 108 | public void mouseReleased(MouseEvent e) {
|
---|
| 109 | common(e);
|
---|
| 110 | }
|
---|
| 111 | });
|
---|
| 112 | // To get checkbox-cells with background of row
|
---|
| 113 | ((JComponent) getDefaultRenderer(Boolean.class)).setOpaque(true);
|
---|
| 114 |
|
---|
| 115 | model = new ModTableModel();
|
---|
| 116 |
|
---|
| 117 | setModel(model);
|
---|
| 118 |
|
---|
| 119 | sorter = new TableRowSorter<ModTableModel>(model);
|
---|
| 120 | setRowSorter(sorter);
|
---|
| 121 |
|
---|
| 122 | setFilter(null, 0);
|
---|
| 123 |
|
---|
| 124 | List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
|
---|
| 125 | sortKeys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
|
---|
| 126 | sorter.setSortKeys(sortKeys);
|
---|
| 127 |
|
---|
| 128 | for (int i = 0; i < model.getColumnCount(); i++) {
|
---|
| 129 | model.setColumnConstraints(i, getColumnModel().getColumn(i));
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | @Override
|
---|
| 134 | public String getToolTipText(MouseEvent e) {
|
---|
| 135 | int r = rowAtPoint(e.getPoint());
|
---|
| 136 | int c = columnAtPoint(e.getPoint());
|
---|
| 137 | if (r >= 0 && r < getRowCount()) {
|
---|
| 138 | int modelCol = convertColumnIndexToModel(c);
|
---|
| 139 | if (modelCol == 4) {
|
---|
| 140 | final Mod mod = (Mod) getValueAt(r, -1);
|
---|
| 141 |
|
---|
| 142 | String tt = "<html>";
|
---|
| 143 | tt += String.format("%s: %s<br>",
|
---|
| 144 | bundle.getString("state.installed"),
|
---|
| 145 | bundle.getString((mod.isInstalled() ? "yes" : "no")));
|
---|
| 146 | tt += String.format(
|
---|
| 147 | "%s: %s<br>",
|
---|
| 148 | bundle.getString("state.updatable"),
|
---|
| 149 | bundle.getString((mod.isLocalAvailable()
|
---|
| 150 | && mod.isNewerAvailable() ? "yes" : "no")));
|
---|
| 151 | tt += String.format("%s: %s</html>", bundle
|
---|
| 152 | .getString("state.downloaded"), bundle.getString((mod
|
---|
| 153 | .isLocalAvailable() ? "yes" : "no")));
|
---|
| 154 | return tt;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | return super.getToolTipText(e);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /**
|
---|
| 161 | * @param listener
|
---|
| 162 | * Listener to add
|
---|
| 163 | */
|
---|
| 164 | public void addModSelectionListener(ModSelectionListener listener) {
|
---|
| 165 | modSelListeners.add(listener);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * @param listener
|
---|
| 170 | * Listener to remove
|
---|
| 171 | */
|
---|
| 172 | public void removeModSelectionListener(ModSelectionListener listener) {
|
---|
| 173 | modSelListeners.remove(listener);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | private void notifyModSelectionListeners(Mod m) {
|
---|
| 177 | for (ModSelectionListener l : modSelListeners) {
|
---|
| 178 | l.modSelectionChanged(this, m);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * @param listener
|
---|
| 184 | * Listener to add
|
---|
| 185 | */
|
---|
| 186 | public void addDownloadSizeListener(DownloadSizeListener listener) {
|
---|
| 187 | model.addDownloadSizeListener(listener);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * @param listener
|
---|
| 192 | * Listener to remove
|
---|
| 193 | */
|
---|
| 194 | public void removeDownloadSizeListener(DownloadSizeListener listener) {
|
---|
| 195 | model.removeDownloadSizeListener(listener);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * Reload the nodes data after an update to the cache
|
---|
| 200 | */
|
---|
| 201 | public void reloadData() {
|
---|
| 202 | model.reloadData();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /**
|
---|
| 206 | * Revert the selection to the mods that are currently installed
|
---|
| 207 | */
|
---|
| 208 | public void revertSelection() {
|
---|
| 209 | model.revertSelection();
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * Reload the selection after a config was loaded
|
---|
| 214 | *
|
---|
| 215 | * @param config
|
---|
| 216 | * Config to load
|
---|
| 217 | */
|
---|
| 218 | public void reloadSelection(File config) {
|
---|
| 219 | model.reloadSelection(config);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
| 223 | * @return Mods selected for installation
|
---|
| 224 | */
|
---|
| 225 | public TreeSet<Mod> getSelectedMods() {
|
---|
| 226 | return model.getSelectedMods();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * @param type
|
---|
| 231 | * Type of mods to show (null for all)
|
---|
| 232 | * @param downloadState
|
---|
| 233 | * Show only: 0 = all, 1 = online, 2 = downloaded
|
---|
| 234 | */
|
---|
| 235 | public void setFilter(Type type, int downloadState) {
|
---|
| 236 | sorter.setRowFilter(new ModTableFilter(type, downloadState));
|
---|
| 237 | }
|
---|
| 238 | }
|
---|