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