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