[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 |
|
---|
[648] | 14 | import net.oni2.aeinstaller.backend.packages.Package;
|
---|
| 15 | import net.oni2.aeinstaller.backend.packages.PackageManager;
|
---|
[658] | 16 | import net.oni2.aeinstaller.gui.modtable.ModTable.ETableContentType;
|
---|
[840] | 17 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
[591] | 18 |
|
---|
| 19 | /**
|
---|
| 20 | * @author Christian Illy
|
---|
| 21 | */
|
---|
| 22 | public class ModTableModel extends AbstractTableModel {
|
---|
| 23 |
|
---|
| 24 | private static final long serialVersionUID = -8278155705802697354L;
|
---|
| 25 |
|
---|
[840] | 26 | private ResourceBundle bundle = UTF8ResourceBundleLoader
|
---|
[631] | 27 | .getBundle("net.oni2.aeinstaller.localization.ModTable");
|
---|
[591] | 28 |
|
---|
[648] | 29 | private Vector<Package> items = new Vector<Package>();
|
---|
[600] | 30 | private Vector<Boolean> install = new Vector<Boolean>();
|
---|
[591] | 31 |
|
---|
[673] | 32 | private HashSet<ModInstallSelectionListener> listeners = new HashSet<ModInstallSelectionListener>();
|
---|
[600] | 33 |
|
---|
[646] | 34 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
---|
| 35 |
|
---|
[658] | 36 | private ETableContentType contentType = ETableContentType.MODS;
|
---|
| 37 |
|
---|
[591] | 38 | /**
|
---|
| 39 | * Create a new model
|
---|
[658] | 40 | *
|
---|
| 41 | * @param contentType
|
---|
| 42 | * Content type to show
|
---|
[591] | 43 | */
|
---|
[658] | 44 | public ModTableModel(ETableContentType contentType) {
|
---|
| 45 | this.contentType = contentType;
|
---|
[591] | 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public Object getValueAt(int row, int col) {
|
---|
[648] | 50 | Package mod = items.get(row);
|
---|
[591] | 51 | switch (col) {
|
---|
| 52 | case -1:
|
---|
[600] | 53 | return mod;
|
---|
[591] | 54 | case 0:
|
---|
[606] | 55 | return install.get(row);
|
---|
| 56 | case 1:
|
---|
[600] | 57 | return mod.getName();
|
---|
[606] | 58 | case 2:
|
---|
[604] | 59 | return mod.getPackageNumberString();
|
---|
[630] | 60 | case 3:
|
---|
| 61 | return mod.getCreator();
|
---|
[631] | 62 | case 4:
|
---|
| 63 | String res = "";
|
---|
[648] | 64 | res += (mod.isInstalled() ? "I" : "_");
|
---|
[631] | 65 | res += (mod.isLocalAvailable() && mod.isNewerAvailable() ? "U"
|
---|
| 66 | : "_");
|
---|
| 67 | res += (mod.isLocalAvailable() ? "D" : "_");
|
---|
| 68 | return res;
|
---|
[637] | 69 | case 5:
|
---|
[857] | 70 | if (mod.getCreatedMillis() > 0)
|
---|
| 71 | return sdf.format(new Date(mod.getCreatedMillis() * 1000));
|
---|
[657] | 72 | else
|
---|
| 73 | return null;
|
---|
| 74 | case 6:
|
---|
[857] | 75 | if (mod.getUpdatedMillis() > 0)
|
---|
| 76 | return sdf.format(new Date(mod.getUpdatedMillis() * 1000));
|
---|
[638] | 77 | else
|
---|
| 78 | return null;
|
---|
[591] | 79 | }
|
---|
| 80 | return null;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | @Override
|
---|
| 84 | public String getColumnName(int col) {
|
---|
| 85 | switch (col) {
|
---|
| 86 | case 0:
|
---|
[606] | 87 | return bundle.getString("mod.install");
|
---|
| 88 | case 1:
|
---|
[591] | 89 | return bundle.getString("mod.name");
|
---|
[606] | 90 | case 2:
|
---|
[591] | 91 | return bundle.getString("mod.package_number");
|
---|
[630] | 92 | case 3:
|
---|
| 93 | return bundle.getString("mod.creator");
|
---|
[631] | 94 | case 4:
|
---|
| 95 | return bundle.getString("mod.state");
|
---|
[637] | 96 | case 5:
|
---|
[657] | 97 | return bundle.getString("mod.added");
|
---|
| 98 | case 6:
|
---|
[637] | 99 | return bundle.getString("mod.date");
|
---|
[591] | 100 | }
|
---|
| 101 | return null;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | @Override
|
---|
| 105 | public int getRowCount() {
|
---|
| 106 | return items.size();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Override
|
---|
| 110 | public int getColumnCount() {
|
---|
[657] | 111 | return 7;
|
---|
[591] | 112 | }
|
---|
| 113 |
|
---|
| 114 | @Override
|
---|
| 115 | public Class<?> getColumnClass(int col) {
|
---|
| 116 | switch (col) {
|
---|
| 117 | case 0:
|
---|
[606] | 118 | return Boolean.class;
|
---|
[591] | 119 | case 1:
|
---|
[604] | 120 | return String.class;
|
---|
[591] | 121 | case 2:
|
---|
| 122 | return String.class;
|
---|
[630] | 123 | case 3:
|
---|
| 124 | return String.class;
|
---|
[631] | 125 | case 4:
|
---|
| 126 | return String.class;
|
---|
[637] | 127 | case 5:
|
---|
| 128 | return String.class;
|
---|
[657] | 129 | case 6:
|
---|
| 130 | return String.class;
|
---|
[591] | 131 | }
|
---|
| 132 | return null;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Set the constraints on the columns size for the given column
|
---|
| 137 | *
|
---|
| 138 | * @param colNum
|
---|
| 139 | * Column number
|
---|
| 140 | * @param col
|
---|
| 141 | * Column object
|
---|
| 142 | */
|
---|
| 143 | public void setColumnConstraints(int colNum, TableColumn col) {
|
---|
| 144 | int w;
|
---|
| 145 | switch (colNum) {
|
---|
| 146 | case 0:
|
---|
[651] | 147 | w = 70;
|
---|
[591] | 148 | col.setPreferredWidth(w);
|
---|
| 149 | col.setMinWidth(w);
|
---|
| 150 | col.setMaxWidth(w);
|
---|
| 151 | break;
|
---|
[606] | 152 | case 1:
|
---|
| 153 | col.setPreferredWidth(150);
|
---|
| 154 | break;
|
---|
[591] | 155 | case 2:
|
---|
[651] | 156 | w = 60;
|
---|
[591] | 157 | col.setPreferredWidth(w);
|
---|
| 158 | col.setMinWidth(w);
|
---|
| 159 | col.setMaxWidth(w);
|
---|
| 160 | break;
|
---|
[630] | 161 | case 3:
|
---|
| 162 | col.setPreferredWidth(90);
|
---|
| 163 | break;
|
---|
[631] | 164 | case 4:
|
---|
[651] | 165 | w = 60;
|
---|
[631] | 166 | col.setPreferredWidth(w);
|
---|
| 167 | col.setMinWidth(w);
|
---|
| 168 | col.setMaxWidth(w);
|
---|
| 169 | break;
|
---|
[637] | 170 | case 5:
|
---|
[657] | 171 | w = 100;
|
---|
[637] | 172 | col.setPreferredWidth(w);
|
---|
| 173 | col.setMinWidth(w);
|
---|
| 174 | col.setMaxWidth(w);
|
---|
| 175 | break;
|
---|
[657] | 176 | case 6:
|
---|
| 177 | w = 100;
|
---|
| 178 | col.setPreferredWidth(w);
|
---|
| 179 | col.setMinWidth(w);
|
---|
| 180 | col.setMaxWidth(w);
|
---|
| 181 | break;
|
---|
[591] | 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Reload the nodes data after an update to the cache
|
---|
| 187 | */
|
---|
| 188 | public void reloadData() {
|
---|
[600] | 189 | items.clear();
|
---|
[658] | 190 | switch (contentType) {
|
---|
| 191 | case MODS:
|
---|
| 192 | items.addAll(PackageManager.getInstance()
|
---|
| 193 | .getModsValidAndNotCore());
|
---|
| 194 | break;
|
---|
| 195 | case TOOLS:
|
---|
| 196 | items.addAll(PackageManager.getInstance().getTools());
|
---|
| 197 | break;
|
---|
| 198 | case CORE:
|
---|
| 199 | items.addAll(PackageManager.getInstance().getCoreTools());
|
---|
| 200 | items.addAll(PackageManager.getInstance().getCoreMods());
|
---|
| 201 | break;
|
---|
| 202 | }
|
---|
[604] | 203 | revertSelection();
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Revert the selection to the mods that are currently installed
|
---|
| 208 | */
|
---|
| 209 | public void revertSelection() {
|
---|
[600] | 210 | install.clear();
|
---|
[593] | 211 | for (int i = 0; i < items.size(); i++) {
|
---|
[673] | 212 | boolean installed = items.get(i).isInstalled();
|
---|
| 213 | install.add(i, installed);
|
---|
[593] | 214 | }
|
---|
[808] | 215 | updateDownloadSize();
|
---|
[604] | 216 | fireTableDataChanged();
|
---|
[591] | 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
[604] | 220 | * Reload the selection after a config was loaded
|
---|
| 221 | *
|
---|
| 222 | * @param config
|
---|
| 223 | * Config to load
|
---|
| 224 | */
|
---|
| 225 | public void reloadSelection(File config) {
|
---|
[658] | 226 | if (contentType == ETableContentType.MODS) {
|
---|
| 227 | Vector<Integer> selected = PackageManager.getInstance()
|
---|
| 228 | .loadModSelection(config);
|
---|
| 229 | install.clear();
|
---|
| 230 | for (int i = 0; i < items.size(); i++) {
|
---|
| 231 | install.add(i,
|
---|
| 232 | selected.contains(items.get(i).getPackageNumber()));
|
---|
| 233 | }
|
---|
[808] | 234 | updateDownloadSize();
|
---|
[658] | 235 | fireTableDataChanged();
|
---|
[604] | 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
[591] | 240 | * Get the items vector
|
---|
| 241 | *
|
---|
| 242 | * @return Items
|
---|
| 243 | */
|
---|
[648] | 244 | public Vector<Package> getItems() {
|
---|
[591] | 245 | return items;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[600] | 248 | /**
|
---|
| 249 | * @return Mods selected for installation
|
---|
| 250 | */
|
---|
[648] | 251 | public TreeSet<Package> getSelectedMods() {
|
---|
| 252 | TreeSet<Package> res = new TreeSet<Package>();
|
---|
[600] | 253 | for (int i = 0; i < items.size(); i++) {
|
---|
| 254 | if (install.get(i))
|
---|
| 255 | res.add(items.get(i));
|
---|
| 256 | }
|
---|
| 257 | return res;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[593] | 260 | @Override
|
---|
| 261 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
[606] | 262 | return columnIndex == 0;
|
---|
[593] | 263 | }
|
---|
[648] | 264 |
|
---|
[673] | 265 | private void notifyDownloadSize(int size, int count) {
|
---|
| 266 | for (ModInstallSelectionListener dsl : listeners)
|
---|
| 267 | dsl.modInstallSelectionChanged(size, count);
|
---|
[646] | 268 | }
|
---|
[593] | 269 |
|
---|
[752] | 270 | /**
|
---|
| 271 | * Check the current download size and notify listeners
|
---|
| 272 | */
|
---|
| 273 | public void updateDownloadSize() {
|
---|
| 274 | int size = 0;
|
---|
| 275 | int count = 0;
|
---|
| 276 | for (int i = 0; i < items.size(); i++) {
|
---|
| 277 | if (install.get(i)) {
|
---|
| 278 | count++;
|
---|
| 279 | Package m = items.get(i);
|
---|
| 280 | if (!m.isLocalAvailable())
|
---|
| 281 | size += m.getZipSize();
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | notifyDownloadSize(size, count);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[593] | 287 | @Override
|
---|
| 288 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
| 289 | super.setValueAt(aValue, rowIndex, columnIndex);
|
---|
[606] | 290 | if (columnIndex == 0) {
|
---|
[752] | 291 | selectPackage(rowIndex, (Boolean) aValue);
|
---|
| 292 | updateDownloadSize();
|
---|
[593] | 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[600] | 296 | /**
|
---|
[752] | 297 | * Select package in given row
|
---|
| 298 | *
|
---|
| 299 | * @param row
|
---|
| 300 | * Row of package
|
---|
| 301 | * @param select
|
---|
| 302 | * Select or not
|
---|
| 303 | */
|
---|
| 304 | public void selectPackage(int row, boolean select) {
|
---|
| 305 | install.set(row, select);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /**
|
---|
[600] | 309 | * @param lis
|
---|
| 310 | * Listener to receive download size changed events
|
---|
| 311 | */
|
---|
[673] | 312 | public void addDownloadSizeListener(ModInstallSelectionListener lis) {
|
---|
[600] | 313 | listeners.add(lis);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /**
|
---|
| 317 | * @param lis
|
---|
| 318 | * Listener to no longer receive download size changed events
|
---|
| 319 | */
|
---|
[673] | 320 | public void removeDownloadSizeListener(ModInstallSelectionListener lis) {
|
---|
[600] | 321 | listeners.remove(lis);
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[591] | 324 | }
|
---|