[591] | 1 | package net.oni2.aeinstaller.gui.modtable;
|
---|
| 2 |
|
---|
[604] | 3 | import java.io.File;
|
---|
[600] | 4 | import java.util.HashSet;
|
---|
[591] | 5 | import java.util.ResourceBundle;
|
---|
[600] | 6 | import java.util.TreeSet;
|
---|
[591] | 7 | import java.util.Vector;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.table.AbstractTableModel;
|
---|
| 10 | import javax.swing.table.TableColumn;
|
---|
| 11 |
|
---|
[600] | 12 | import net.oni2.aeinstaller.backend.mods.Mod;
|
---|
| 13 | import net.oni2.aeinstaller.backend.mods.ModManager;
|
---|
[591] | 14 |
|
---|
| 15 | /**
|
---|
| 16 | * @author Christian Illy
|
---|
| 17 | */
|
---|
| 18 | public class ModTableModel extends AbstractTableModel {
|
---|
| 19 |
|
---|
| 20 | private static final long serialVersionUID = -8278155705802697354L;
|
---|
| 21 |
|
---|
[629] | 22 | private ResourceBundle bundle = ResourceBundle
|
---|
| 23 | .getBundle("net.oni2.aeinstaller.localization."
|
---|
| 24 | + getClass().getSimpleName());
|
---|
[591] | 25 |
|
---|
[600] | 26 | private Vector<Mod> items = new Vector<Mod>();
|
---|
| 27 | private Vector<Boolean> install = new Vector<Boolean>();
|
---|
[591] | 28 |
|
---|
[600] | 29 | private HashSet<DownloadSizeListener> listeners = new HashSet<DownloadSizeListener>();
|
---|
| 30 |
|
---|
[591] | 31 | /**
|
---|
| 32 | * Create a new model
|
---|
| 33 | */
|
---|
| 34 | public ModTableModel() {
|
---|
| 35 | reloadData();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | @Override
|
---|
| 39 | public Object getValueAt(int row, int col) {
|
---|
[600] | 40 | Mod mod = items.get(row);
|
---|
[591] | 41 | switch (col) {
|
---|
| 42 | case -1:
|
---|
[600] | 43 | return mod;
|
---|
[591] | 44 | case 0:
|
---|
[606] | 45 | return install.get(row);
|
---|
| 46 | case 1:
|
---|
[600] | 47 | return mod.getName();
|
---|
[606] | 48 | case 2:
|
---|
[604] | 49 | return mod.getPackageNumberString();
|
---|
[630] | 50 | case 3:
|
---|
| 51 | return mod.getCreator();
|
---|
[591] | 52 | }
|
---|
| 53 | return null;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
| 57 | public String getColumnName(int col) {
|
---|
| 58 | switch (col) {
|
---|
| 59 | case 0:
|
---|
[606] | 60 | return bundle.getString("mod.install");
|
---|
| 61 | case 1:
|
---|
[591] | 62 | return bundle.getString("mod.name");
|
---|
[606] | 63 | case 2:
|
---|
[591] | 64 | return bundle.getString("mod.package_number");
|
---|
[630] | 65 | case 3:
|
---|
| 66 | return bundle.getString("mod.creator");
|
---|
[591] | 67 | }
|
---|
| 68 | return null;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @Override
|
---|
| 72 | public int getRowCount() {
|
---|
| 73 | return items.size();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | @Override
|
---|
| 77 | public int getColumnCount() {
|
---|
[630] | 78 | return 4;
|
---|
[591] | 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public Class<?> getColumnClass(int col) {
|
---|
| 83 | switch (col) {
|
---|
| 84 | case 0:
|
---|
[606] | 85 | return Boolean.class;
|
---|
[591] | 86 | case 1:
|
---|
[604] | 87 | return String.class;
|
---|
[591] | 88 | case 2:
|
---|
| 89 | return String.class;
|
---|
[630] | 90 | case 3:
|
---|
| 91 | return String.class;
|
---|
[591] | 92 | }
|
---|
| 93 | return null;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * Set the constraints on the columns size for the given column
|
---|
| 98 | *
|
---|
| 99 | * @param colNum
|
---|
| 100 | * Column number
|
---|
| 101 | * @param col
|
---|
| 102 | * Column object
|
---|
| 103 | */
|
---|
| 104 | public void setColumnConstraints(int colNum, TableColumn col) {
|
---|
| 105 | int w;
|
---|
| 106 | switch (colNum) {
|
---|
| 107 | case 0:
|
---|
[630] | 108 | w = 60;
|
---|
[591] | 109 | col.setPreferredWidth(w);
|
---|
| 110 | col.setMinWidth(w);
|
---|
| 111 | col.setMaxWidth(w);
|
---|
| 112 | break;
|
---|
[606] | 113 | case 1:
|
---|
| 114 | col.setPreferredWidth(150);
|
---|
| 115 | break;
|
---|
[591] | 116 | case 2:
|
---|
[606] | 117 | w = 55;
|
---|
[591] | 118 | col.setPreferredWidth(w);
|
---|
| 119 | col.setMinWidth(w);
|
---|
| 120 | col.setMaxWidth(w);
|
---|
| 121 | break;
|
---|
[630] | 122 | case 3:
|
---|
| 123 | col.setPreferredWidth(90);
|
---|
| 124 | break;
|
---|
[591] | 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Reload the nodes data after an update to the cache
|
---|
| 130 | */
|
---|
| 131 | public void reloadData() {
|
---|
[600] | 132 | items.clear();
|
---|
[608] | 133 | items.addAll(ModManager.getInstance().getModsValidAndNotMandatory());
|
---|
[604] | 134 | revertSelection();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Revert the selection to the mods that are currently installed
|
---|
| 139 | */
|
---|
| 140 | public void revertSelection() {
|
---|
[600] | 141 | install.clear();
|
---|
[593] | 142 | for (int i = 0; i < items.size(); i++) {
|
---|
[604] | 143 | install.add(i, ModManager.getInstance()
|
---|
| 144 | .isModInstalled(items.get(i)));
|
---|
[593] | 145 | }
|
---|
[604] | 146 | fireTableDataChanged();
|
---|
[591] | 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
[604] | 150 | * Reload the selection after a config was loaded
|
---|
| 151 | *
|
---|
| 152 | * @param config
|
---|
| 153 | * Config to load
|
---|
| 154 | */
|
---|
| 155 | public void reloadSelection(File config) {
|
---|
| 156 | Vector<Integer> selected = ModManager.getInstance().loadModSelection(
|
---|
| 157 | config);
|
---|
| 158 | install.clear();
|
---|
| 159 | for (int i = 0; i < items.size(); i++) {
|
---|
| 160 | install.add(i, selected.contains(items.get(i).getPackageNumber()));
|
---|
| 161 | }
|
---|
| 162 | fireTableDataChanged();
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
[591] | 166 | * Get the items vector
|
---|
| 167 | *
|
---|
| 168 | * @return Items
|
---|
| 169 | */
|
---|
[600] | 170 | public Vector<Mod> getItems() {
|
---|
[591] | 171 | return items;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[600] | 174 | /**
|
---|
| 175 | * @return Mods selected for installation
|
---|
| 176 | */
|
---|
| 177 | public TreeSet<Mod> getSelectedMods() {
|
---|
| 178 | TreeSet<Mod> res = new TreeSet<Mod>();
|
---|
| 179 | for (int i = 0; i < items.size(); i++) {
|
---|
| 180 | if (install.get(i))
|
---|
| 181 | res.add(items.get(i));
|
---|
| 182 | }
|
---|
| 183 | return res;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[593] | 186 | @Override
|
---|
| 187 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
[606] | 188 | return columnIndex == 0;
|
---|
[593] | 189 | }
|
---|
| 190 |
|
---|
| 191 | @Override
|
---|
| 192 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
| 193 | super.setValueAt(aValue, rowIndex, columnIndex);
|
---|
[606] | 194 | if (columnIndex == 0) {
|
---|
[593] | 195 | install.set(rowIndex, (Boolean) aValue);
|
---|
[600] | 196 |
|
---|
| 197 | int size = 0;
|
---|
| 198 | for (int i = 0; i < items.size(); i++) {
|
---|
| 199 | if (install.get(i)) {
|
---|
| 200 | Mod m = items.get(i);
|
---|
[605] | 201 | if (!m.isLocalAvailable())
|
---|
[600] | 202 | size += m.getZipSize();
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | for (DownloadSizeListener dsl : listeners)
|
---|
| 206 | dsl.downloadSizeChanged(size);
|
---|
[593] | 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[600] | 210 | /**
|
---|
| 211 | * @param lis
|
---|
| 212 | * Listener to receive download size changed events
|
---|
| 213 | */
|
---|
| 214 | public void addDownloadSizeListener(DownloadSizeListener lis) {
|
---|
| 215 | listeners.add(lis);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * @param lis
|
---|
| 220 | * Listener to no longer receive download size changed events
|
---|
| 221 | */
|
---|
| 222 | public void removeDownloadSizeListener(DownloadSizeListener lis) {
|
---|
| 223 | listeners.remove(lis);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[591] | 226 | }
|
---|