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