| 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 | 
 | 
|---|
| 17 | /**
 | 
|---|
| 18 |  * @author Christian Illy
 | 
|---|
| 19 |  */
 | 
|---|
| 20 | public class ModTableModel extends AbstractTableModel {
 | 
|---|
| 21 | 
 | 
|---|
| 22 |         private static final long serialVersionUID = -8278155705802697354L;
 | 
|---|
| 23 | 
 | 
|---|
| 24 |         private ResourceBundle bundle = ResourceBundle
 | 
|---|
| 25 |                         .getBundle("net.oni2.aeinstaller.localization.ModTable");
 | 
|---|
| 26 | 
 | 
|---|
| 27 |         private Vector<Package> items = new Vector<Package>();
 | 
|---|
| 28 |         private Vector<Boolean> install = new Vector<Boolean>();
 | 
|---|
| 29 | 
 | 
|---|
| 30 |         private HashSet<DownloadSizeListener> listeners = new HashSet<DownloadSizeListener>();
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         /**
 | 
|---|
| 35 |          * Create a new model
 | 
|---|
| 36 |          */
 | 
|---|
| 37 |         public ModTableModel() {
 | 
|---|
| 38 |         }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         @Override
 | 
|---|
| 41 |         public Object getValueAt(int row, int col) {
 | 
|---|
| 42 |                 Package mod = items.get(row);
 | 
|---|
| 43 |                 switch (col) {
 | 
|---|
| 44 |                         case -1:
 | 
|---|
| 45 |                                 return mod;
 | 
|---|
| 46 |                         case 0:
 | 
|---|
| 47 |                                 return install.get(row);
 | 
|---|
| 48 |                         case 1:
 | 
|---|
| 49 |                                 return mod.getName();
 | 
|---|
| 50 |                         case 2:
 | 
|---|
| 51 |                                 return mod.getPackageNumberString();
 | 
|---|
| 52 |                         case 3:
 | 
|---|
| 53 |                                 return mod.getCreator();
 | 
|---|
| 54 |                         case 4:
 | 
|---|
| 55 |                                 String res = "";
 | 
|---|
| 56 |                                 res += (mod.isInstalled() ? "I" : "_");
 | 
|---|
| 57 |                                 res += (mod.isLocalAvailable() && mod.isNewerAvailable() ? "U"
 | 
|---|
| 58 |                                                 : "_");
 | 
|---|
| 59 |                                 res += (mod.isLocalAvailable() ? "D" : "_");
 | 
|---|
| 60 |                                 return res;
 | 
|---|
| 61 |                         case 5:
 | 
|---|
| 62 |                                 if (mod.getFile() != null)
 | 
|---|
| 63 |                                         return sdf.format(new Date(
 | 
|---|
| 64 |                                                         mod.getFile().getTimestamp() * 1000));
 | 
|---|
| 65 |                                 else
 | 
|---|
| 66 |                                         return null;
 | 
|---|
| 67 |                 }
 | 
|---|
| 68 |                 return null;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         @Override
 | 
|---|
| 72 |         public String getColumnName(int col) {
 | 
|---|
| 73 |                 switch (col) {
 | 
|---|
| 74 |                         case 0:
 | 
|---|
| 75 |                                 return bundle.getString("mod.install");
 | 
|---|
| 76 |                         case 1:
 | 
|---|
| 77 |                                 return bundle.getString("mod.name");
 | 
|---|
| 78 |                         case 2:
 | 
|---|
| 79 |                                 return bundle.getString("mod.package_number");
 | 
|---|
| 80 |                         case 3:
 | 
|---|
| 81 |                                 return bundle.getString("mod.creator");
 | 
|---|
| 82 |                         case 4:
 | 
|---|
| 83 |                                 return bundle.getString("mod.state");
 | 
|---|
| 84 |                         case 5:
 | 
|---|
| 85 |                                 return bundle.getString("mod.date");
 | 
|---|
| 86 |                 }
 | 
|---|
| 87 |                 return null;
 | 
|---|
| 88 |         }
 | 
|---|
| 89 | 
 | 
|---|
| 90 |         @Override
 | 
|---|
| 91 |         public int getRowCount() {
 | 
|---|
| 92 |                 return items.size();
 | 
|---|
| 93 |         }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         @Override
 | 
|---|
| 96 |         public int getColumnCount() {
 | 
|---|
| 97 |                 return 6;
 | 
|---|
| 98 |         }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         @Override
 | 
|---|
| 101 |         public Class<?> getColumnClass(int col) {
 | 
|---|
| 102 |                 switch (col) {
 | 
|---|
| 103 |                         case 0:
 | 
|---|
| 104 |                                 return Boolean.class;
 | 
|---|
| 105 |                         case 1:
 | 
|---|
| 106 |                                 return String.class;
 | 
|---|
| 107 |                         case 2:
 | 
|---|
| 108 |                                 return String.class;
 | 
|---|
| 109 |                         case 3:
 | 
|---|
| 110 |                                 return String.class;
 | 
|---|
| 111 |                         case 4:
 | 
|---|
| 112 |                                 return String.class;
 | 
|---|
| 113 |                         case 5:
 | 
|---|
| 114 |                                 return String.class;
 | 
|---|
| 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:
 | 
|---|
| 131 |                                 w = 60;
 | 
|---|
| 132 |                                 col.setPreferredWidth(w);
 | 
|---|
| 133 |                                 col.setMinWidth(w);
 | 
|---|
| 134 |                                 col.setMaxWidth(w);
 | 
|---|
| 135 |                                 break;
 | 
|---|
| 136 |                         case 1:
 | 
|---|
| 137 |                                 col.setPreferredWidth(150);
 | 
|---|
| 138 |                                 break;
 | 
|---|
| 139 |                         case 2:
 | 
|---|
| 140 |                                 w = 55;
 | 
|---|
| 141 |                                 col.setPreferredWidth(w);
 | 
|---|
| 142 |                                 col.setMinWidth(w);
 | 
|---|
| 143 |                                 col.setMaxWidth(w);
 | 
|---|
| 144 |                                 break;
 | 
|---|
| 145 |                         case 3:
 | 
|---|
| 146 |                                 col.setPreferredWidth(90);
 | 
|---|
| 147 |                                 break;
 | 
|---|
| 148 |                         case 4:
 | 
|---|
| 149 |                                 w = 55;
 | 
|---|
| 150 |                                 col.setPreferredWidth(w);
 | 
|---|
| 151 |                                 col.setMinWidth(w);
 | 
|---|
| 152 |                                 col.setMaxWidth(w);
 | 
|---|
| 153 |                                 break;
 | 
|---|
| 154 |                         case 5:
 | 
|---|
| 155 |                                 w = 95;
 | 
|---|
| 156 |                                 col.setPreferredWidth(w);
 | 
|---|
| 157 |                                 col.setMinWidth(w);
 | 
|---|
| 158 |                                 col.setMaxWidth(w);
 | 
|---|
| 159 |                                 break;
 | 
|---|
| 160 |                 }
 | 
|---|
| 161 |         }
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         /**
 | 
|---|
| 164 |          * Reload the nodes data after an update to the cache
 | 
|---|
| 165 |          */
 | 
|---|
| 166 |         public void reloadData() {
 | 
|---|
| 167 |                 items.clear();
 | 
|---|
| 168 |                 items.addAll(PackageManager.getInstance().getModsValidAndNotCore());
 | 
|---|
| 169 |                 revertSelection();
 | 
|---|
| 170 |         }
 | 
|---|
| 171 | 
 | 
|---|
| 172 |         /**
 | 
|---|
| 173 |          * Revert the selection to the mods that are currently installed
 | 
|---|
| 174 |          */
 | 
|---|
| 175 |         public void revertSelection() {
 | 
|---|
| 176 |                 install.clear();
 | 
|---|
| 177 |                 for (int i = 0; i < items.size(); i++) {
 | 
|---|
| 178 |                         install.add(i, items.get(i).isInstalled());
 | 
|---|
| 179 |                 }
 | 
|---|
| 180 |                 notifyDownloadSize(0);
 | 
|---|
| 181 |                 fireTableDataChanged();
 | 
|---|
| 182 |         }
 | 
|---|
| 183 | 
 | 
|---|
| 184 |         /**
 | 
|---|
| 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 = PackageManager.getInstance()
 | 
|---|
| 192 |                                 .loadModSelection(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 |         /**
 | 
|---|
| 201 |          * Get the items vector
 | 
|---|
| 202 |          * 
 | 
|---|
| 203 |          * @return Items
 | 
|---|
| 204 |          */
 | 
|---|
| 205 |         public Vector<Package> getItems() {
 | 
|---|
| 206 |                 return items;
 | 
|---|
| 207 |         }
 | 
|---|
| 208 | 
 | 
|---|
| 209 |         /**
 | 
|---|
| 210 |          * @return Mods selected for installation
 | 
|---|
| 211 |          */
 | 
|---|
| 212 |         public TreeSet<Package> getSelectedMods() {
 | 
|---|
| 213 |                 TreeSet<Package> res = new TreeSet<Package>();
 | 
|---|
| 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 | 
 | 
|---|
| 221 |         @Override
 | 
|---|
| 222 |         public boolean isCellEditable(int rowIndex, int columnIndex) {
 | 
|---|
| 223 |                 return columnIndex == 0;
 | 
|---|
| 224 |         }
 | 
|---|
| 225 | 
 | 
|---|
| 226 |         private void notifyDownloadSize(int size) {
 | 
|---|
| 227 |                 for (DownloadSizeListener dsl : listeners)
 | 
|---|
| 228 |                         dsl.downloadSizeChanged(size);
 | 
|---|
| 229 |         }
 | 
|---|
| 230 | 
 | 
|---|
| 231 |         @Override
 | 
|---|
| 232 |         public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
 | 
|---|
| 233 |                 super.setValueAt(aValue, rowIndex, columnIndex);
 | 
|---|
| 234 |                 if (columnIndex == 0) {
 | 
|---|
| 235 |                         install.set(rowIndex, (Boolean) aValue);
 | 
|---|
| 236 | 
 | 
|---|
| 237 |                         int size = 0;
 | 
|---|
| 238 |                         for (int i = 0; i < items.size(); i++) {
 | 
|---|
| 239 |                                 if (install.get(i)) {
 | 
|---|
| 240 |                                         Package m = items.get(i);
 | 
|---|
| 241 |                                         if (!m.isLocalAvailable())
 | 
|---|
| 242 |                                                 size += m.getZipSize();
 | 
|---|
| 243 |                                 }
 | 
|---|
| 244 |                         }
 | 
|---|
| 245 |                         notifyDownloadSize(size);
 | 
|---|
| 246 |                 }
 | 
|---|
| 247 |         }
 | 
|---|
| 248 | 
 | 
|---|
| 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 | 
 | 
|---|
| 265 | }
 | 
|---|