| 1 |
package net.oni2.aeinstaller.gui.modtable; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.HashSet; |
| 5 |
import java.util.ResourceBundle; |
| 6 |
import java.util.TreeSet; |
| 7 |
import java.util.Vector; |
| 8 |
|
| 9 |
import javax.swing.table.AbstractTableModel; |
| 10 |
import javax.swing.table.TableColumn; |
| 11 |
|
| 12 |
import net.oni2.aeinstaller.backend.mods.Mod; |
| 13 |
import net.oni2.aeinstaller.backend.mods.ModManager; |
| 14 |
import net.oni2.aeinstaller.backend.mods.Type; |
| 15 |
|
| 16 |
/** |
| 17 |
* @author Christian Illy |
| 18 |
*/ |
| 19 |
public class ModTableModel extends AbstractTableModel { |
| 20 |
|
| 21 |
private static final long serialVersionUID = -8278155705802697354L; |
| 22 |
|
| 23 |
private ResourceBundle bundle = ResourceBundle.getBundle(getClass() |
| 24 |
.getName()); |
| 25 |
|
| 26 |
private Vector<Mod> items = new Vector<Mod>(); |
| 27 |
private Vector<Boolean> install = new Vector<Boolean>(); |
| 28 |
|
| 29 |
private HashSet<DownloadSizeListener> listeners = new HashSet<DownloadSizeListener>(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Create a new model |
| 33 |
*/ |
| 34 |
public ModTableModel() { |
| 35 |
reloadData(); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public Object getValueAt(int row, int col) { |
| 40 |
Mod mod = items.get(row); |
| 41 |
switch (col) { |
| 42 |
case -1: |
| 43 |
return mod; |
| 44 |
case 0: |
| 45 |
return mod.getName(); |
| 46 |
case 1: |
| 47 |
return mod.getPackageNumberString(); |
| 48 |
case 2: |
| 49 |
String type = ""; |
| 50 |
for (Type t : mod.getTypes()) { |
| 51 |
if (type.length() > 0) |
| 52 |
type += ", "; |
| 53 |
type += t.getName(); |
| 54 |
} |
| 55 |
return type; |
| 56 |
case 3: |
| 57 |
return mod.getPlatform().toString(); |
| 58 |
case 4: |
| 59 |
return install.get(row); |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public String getColumnName(int col) { |
| 66 |
switch (col) { |
| 67 |
case 0: |
| 68 |
return bundle.getString("mod.name"); |
| 69 |
case 1: |
| 70 |
return bundle.getString("mod.package_number"); |
| 71 |
case 2: |
| 72 |
return bundle.getString("mod.type"); |
| 73 |
case 3: |
| 74 |
return bundle.getString("mod.platform"); |
| 75 |
case 4: |
| 76 |
return bundle.getString("mod.install"); |
| 77 |
} |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public int getRowCount() { |
| 83 |
return items.size(); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public int getColumnCount() { |
| 88 |
return 5; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public Class<?> getColumnClass(int col) { |
| 93 |
switch (col) { |
| 94 |
case 0: |
| 95 |
return String.class; |
| 96 |
case 1: |
| 97 |
return String.class; |
| 98 |
case 2: |
| 99 |
return String.class; |
| 100 |
case 3: |
| 101 |
return String.class; |
| 102 |
case 4: |
| 103 |
return Boolean.class; |
| 104 |
} |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Set the constraints on the columns size for the given column |
| 110 |
* |
| 111 |
* @param colNum |
| 112 |
* Column number |
| 113 |
* @param col |
| 114 |
* Column object |
| 115 |
*/ |
| 116 |
public void setColumnConstraints(int colNum, TableColumn col) { |
| 117 |
int w; |
| 118 |
switch (colNum) { |
| 119 |
case 0: |
| 120 |
col.setPreferredWidth(150); |
| 121 |
break; |
| 122 |
case 1: |
| 123 |
w = 55; |
| 124 |
col.setPreferredWidth(w); |
| 125 |
col.setMinWidth(w); |
| 126 |
col.setMaxWidth(w); |
| 127 |
break; |
| 128 |
case 2: |
| 129 |
col.setPreferredWidth(100); |
| 130 |
break; |
| 131 |
case 3: |
| 132 |
w = 70; |
| 133 |
col.setPreferredWidth(w); |
| 134 |
col.setMinWidth(w); |
| 135 |
col.setMaxWidth(w); |
| 136 |
break; |
| 137 |
case 4: |
| 138 |
w = 60; |
| 139 |
col.setPreferredWidth(w); |
| 140 |
col.setMinWidth(w); |
| 141 |
col.setMaxWidth(w); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Reload the nodes data after an update to the cache |
| 148 |
*/ |
| 149 |
public void reloadData() { |
| 150 |
items.clear(); |
| 151 |
items.addAll(ModManager.getInstance().getMods()); |
| 152 |
revertSelection(); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Revert the selection to the mods that are currently installed |
| 157 |
*/ |
| 158 |
public void revertSelection() { |
| 159 |
install.clear(); |
| 160 |
for (int i = 0; i < items.size(); i++) { |
| 161 |
install.add(i, ModManager.getInstance() |
| 162 |
.isModInstalled(items.get(i))); |
| 163 |
} |
| 164 |
fireTableDataChanged(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Reload the selection after a config was loaded |
| 169 |
* |
| 170 |
* @param config |
| 171 |
* Config to load |
| 172 |
*/ |
| 173 |
public void reloadSelection(File config) { |
| 174 |
Vector<Integer> selected = ModManager.getInstance().loadModSelection( |
| 175 |
config); |
| 176 |
install.clear(); |
| 177 |
for (int i = 0; i < items.size(); i++) { |
| 178 |
install.add(i, selected.contains(items.get(i).getPackageNumber())); |
| 179 |
} |
| 180 |
fireTableDataChanged(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Get the items vector |
| 185 |
* |
| 186 |
* @return Items |
| 187 |
*/ |
| 188 |
public Vector<Mod> getItems() { |
| 189 |
return items; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @return Mods selected for installation |
| 194 |
*/ |
| 195 |
public TreeSet<Mod> getSelectedMods() { |
| 196 |
TreeSet<Mod> res = new TreeSet<Mod>(); |
| 197 |
for (int i = 0; i < items.size(); i++) { |
| 198 |
if (install.get(i)) |
| 199 |
res.add(items.get(i)); |
| 200 |
} |
| 201 |
return res; |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
| 206 |
return columnIndex == 4; |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
| 211 |
super.setValueAt(aValue, rowIndex, columnIndex); |
| 212 |
if (columnIndex == 4) { |
| 213 |
install.set(rowIndex, (Boolean) aValue); |
| 214 |
|
| 215 |
int size = 0; |
| 216 |
for (int i = 0; i < items.size(); i++) { |
| 217 |
if (install.get(i)) { |
| 218 |
Mod m = items.get(i); |
| 219 |
if (!m.isLocalAvailable()) |
| 220 |
size += m.getZipSize(); |
| 221 |
} |
| 222 |
} |
| 223 |
for (DownloadSizeListener dsl : listeners) |
| 224 |
dsl.downloadSizeChanged(size); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* @param lis |
| 230 |
* Listener to receive download size changed events |
| 231 |
*/ |
| 232 |
public void addDownloadSizeListener(DownloadSizeListener lis) { |
| 233 |
listeners.add(lis); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @param lis |
| 238 |
* Listener to no longer receive download size changed events |
| 239 |
*/ |
| 240 |
public void removeDownloadSizeListener(DownloadSizeListener lis) { |
| 241 |
listeners.remove(lis); |
| 242 |
} |
| 243 |
|
| 244 |
} |