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