| 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 |
import net.oni2.resourcebundle.UTF8ResourceBundleLoader; |
| 18 |
|
| 19 |
/** |
| 20 |
* @author Christian Illy |
| 21 |
*/ |
| 22 |
public class ModTableModel extends AbstractTableModel { |
| 23 |
|
| 24 |
private static final long serialVersionUID = -8278155705802697354L; |
| 25 |
|
| 26 |
private ResourceBundle bundle = UTF8ResourceBundleLoader |
| 27 |
.getBundle("net.oni2.aeinstaller.localization.ModTable"); |
| 28 |
|
| 29 |
private Vector<Package> items = new Vector<Package>(); |
| 30 |
private Vector<Boolean> install = new Vector<Boolean>(); |
| 31 |
|
| 32 |
private HashSet<ModInstallSelectionListener> listeners = new HashSet<ModInstallSelectionListener>(); |
| 33 |
|
| 34 |
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 35 |
|
| 36 |
private ETableContentType contentType = ETableContentType.MODS; |
| 37 |
|
| 38 |
/** |
| 39 |
* Create a new model |
| 40 |
* |
| 41 |
* @param contentType |
| 42 |
* Content type to show |
| 43 |
*/ |
| 44 |
public ModTableModel(ETableContentType contentType) { |
| 45 |
this.contentType = contentType; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Object getValueAt(int row, int col) { |
| 50 |
Package mod = items.get(row); |
| 51 |
switch (col) { |
| 52 |
case -1: |
| 53 |
return mod; |
| 54 |
case 0: |
| 55 |
return install.get(row); |
| 56 |
case 1: |
| 57 |
return mod.getName(); |
| 58 |
case 2: |
| 59 |
return mod.getPackageNumberString(); |
| 60 |
case 3: |
| 61 |
return mod.getCreator(); |
| 62 |
case 4: |
| 63 |
String res = ""; |
| 64 |
res += (mod.isInstalled() ? "I" : "_"); |
| 65 |
res += (mod.isLocalAvailable() && mod.isNewerAvailable() ? "U" |
| 66 |
: "_"); |
| 67 |
res += (mod.isLocalAvailable() ? "D" : "_"); |
| 68 |
return res; |
| 69 |
case 5: |
| 70 |
if (mod.getCreatedMillis() > 0) |
| 71 |
return sdf.format(new Date(mod.getCreatedMillis() * 1000)); |
| 72 |
else |
| 73 |
return null; |
| 74 |
case 6: |
| 75 |
if (mod.getUpdatedMillis() > 0) |
| 76 |
return sdf.format(new Date(mod.getUpdatedMillis() * 1000)); |
| 77 |
else |
| 78 |
return null; |
| 79 |
} |
| 80 |
return null; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String getColumnName(int col) { |
| 85 |
switch (col) { |
| 86 |
case 0: |
| 87 |
return bundle.getString("mod.install"); |
| 88 |
case 1: |
| 89 |
return bundle.getString("mod.name"); |
| 90 |
case 2: |
| 91 |
return bundle.getString("mod.package_number"); |
| 92 |
case 3: |
| 93 |
return bundle.getString("mod.creator"); |
| 94 |
case 4: |
| 95 |
return bundle.getString("mod.state"); |
| 96 |
case 5: |
| 97 |
return bundle.getString("mod.added"); |
| 98 |
case 6: |
| 99 |
return bundle.getString("mod.date"); |
| 100 |
} |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public int getRowCount() { |
| 106 |
return items.size(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public int getColumnCount() { |
| 111 |
return 7; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Class<?> getColumnClass(int col) { |
| 116 |
switch (col) { |
| 117 |
case 0: |
| 118 |
return Boolean.class; |
| 119 |
case 1: |
| 120 |
return String.class; |
| 121 |
case 2: |
| 122 |
return String.class; |
| 123 |
case 3: |
| 124 |
return String.class; |
| 125 |
case 4: |
| 126 |
return String.class; |
| 127 |
case 5: |
| 128 |
return String.class; |
| 129 |
case 6: |
| 130 |
return String.class; |
| 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: |
| 147 |
w = 70; |
| 148 |
col.setPreferredWidth(w); |
| 149 |
col.setMinWidth(w); |
| 150 |
col.setMaxWidth(w); |
| 151 |
break; |
| 152 |
case 1: |
| 153 |
col.setPreferredWidth(150); |
| 154 |
break; |
| 155 |
case 2: |
| 156 |
w = 60; |
| 157 |
col.setPreferredWidth(w); |
| 158 |
col.setMinWidth(w); |
| 159 |
col.setMaxWidth(w); |
| 160 |
break; |
| 161 |
case 3: |
| 162 |
col.setPreferredWidth(90); |
| 163 |
break; |
| 164 |
case 4: |
| 165 |
w = 60; |
| 166 |
col.setPreferredWidth(w); |
| 167 |
col.setMinWidth(w); |
| 168 |
col.setMaxWidth(w); |
| 169 |
break; |
| 170 |
case 5: |
| 171 |
w = 100; |
| 172 |
col.setPreferredWidth(w); |
| 173 |
col.setMinWidth(w); |
| 174 |
col.setMaxWidth(w); |
| 175 |
break; |
| 176 |
case 6: |
| 177 |
w = 100; |
| 178 |
col.setPreferredWidth(w); |
| 179 |
col.setMinWidth(w); |
| 180 |
col.setMaxWidth(w); |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Reload the nodes data after an update to the cache |
| 187 |
*/ |
| 188 |
public void reloadData() { |
| 189 |
items.clear(); |
| 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 |
} |
| 203 |
revertSelection(); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Revert the selection to the mods that are currently installed |
| 208 |
*/ |
| 209 |
public void revertSelection() { |
| 210 |
install.clear(); |
| 211 |
for (int i = 0; i < items.size(); i++) { |
| 212 |
boolean installed = items.get(i).isInstalled(); |
| 213 |
install.add(i, installed); |
| 214 |
} |
| 215 |
updateDownloadSize(); |
| 216 |
fireTableDataChanged(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Reload the selection after a config was loaded |
| 221 |
* |
| 222 |
* @param config |
| 223 |
* Config to load |
| 224 |
*/ |
| 225 |
public void reloadSelection(File config) { |
| 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 |
} |
| 234 |
updateDownloadSize(); |
| 235 |
fireTableDataChanged(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get the items vector |
| 241 |
* |
| 242 |
* @return Items |
| 243 |
*/ |
| 244 |
public Vector<Package> getItems() { |
| 245 |
return items; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* @return Mods selected for installation |
| 250 |
*/ |
| 251 |
public TreeSet<Package> getSelectedMods() { |
| 252 |
TreeSet<Package> res = new TreeSet<Package>(); |
| 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 |
|
| 260 |
@Override |
| 261 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
| 262 |
return columnIndex == 0; |
| 263 |
} |
| 264 |
|
| 265 |
private void notifyDownloadSize(int size, int count) { |
| 266 |
for (ModInstallSelectionListener dsl : listeners) |
| 267 |
dsl.modInstallSelectionChanged(size, count); |
| 268 |
} |
| 269 |
|
| 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 |
|
| 287 |
@Override |
| 288 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
| 289 |
super.setValueAt(aValue, rowIndex, columnIndex); |
| 290 |
if (columnIndex == 0) { |
| 291 |
selectPackage(rowIndex, (Boolean) aValue); |
| 292 |
updateDownloadSize(); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 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 |
/** |
| 309 |
* @param lis |
| 310 |
* Listener to receive download size changed events |
| 311 |
*/ |
| 312 |
public void addDownloadSizeListener(ModInstallSelectionListener lis) { |
| 313 |
listeners.add(lis); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* @param lis |
| 318 |
* Listener to no longer receive download size changed events |
| 319 |
*/ |
| 320 |
public void removeDownloadSizeListener(ModInstallSelectionListener lis) { |
| 321 |
listeners.remove(lis); |
| 322 |
} |
| 323 |
|
| 324 |
} |