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