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