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