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 | import net.oni2.aeinstaller.gui.modtable.ModTable.ETableContentType;
|
---|
17 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * @author Christian Illy
|
---|
21 | */
|
---|
22 | public class ModTableModel extends AbstractTableModel {
|
---|
23 |
|
---|
24 | private static final long serialVersionUID = -8278155705802697354L;
|
---|
25 |
|
---|
26 | private ResourceBundle bundle = UTF8ResourceBundleLoader
|
---|
27 | .getBundle("net.oni2.aeinstaller.localization.ModTable");
|
---|
28 |
|
---|
29 | private Vector<Package> items = new Vector<Package>();
|
---|
30 | private Vector<Boolean> install = new Vector<Boolean>();
|
---|
31 |
|
---|
32 | private HashSet<ModInstallSelectionListener> listeners = new HashSet<ModInstallSelectionListener>();
|
---|
33 |
|
---|
34 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
---|
35 |
|
---|
36 | private ETableContentType contentType = ETableContentType.MODS;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create a new model
|
---|
40 | *
|
---|
41 | * @param contentType
|
---|
42 | * Content type to show
|
---|
43 | */
|
---|
44 | public ModTableModel(ETableContentType contentType) {
|
---|
45 | this.contentType = contentType;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public Object getValueAt(int row, int col) {
|
---|
50 | Package mod = items.get(row);
|
---|
51 | switch (col) {
|
---|
52 | case -1:
|
---|
53 | return mod;
|
---|
54 | case 0:
|
---|
55 | return install.get(row);
|
---|
56 | case 1:
|
---|
57 | return mod.getName();
|
---|
58 | case 2:
|
---|
59 | return mod.getPackageNumberString();
|
---|
60 | case 3:
|
---|
61 | return mod.getCreator();
|
---|
62 | case 4:
|
---|
63 | String res = "";
|
---|
64 | res += (mod.isInstalled() ? "I" : "_");
|
---|
65 | res += (mod.isLocalAvailable() && mod.isNewerAvailable() ? "U"
|
---|
66 | : "_");
|
---|
67 | res += (mod.isLocalAvailable() ? "D" : "_");
|
---|
68 | return res;
|
---|
69 | case 5:
|
---|
70 | if (mod.getNode() != null)
|
---|
71 | return sdf.format(new Date(
|
---|
72 | mod.getNode().getCreated() * 1000));
|
---|
73 | else
|
---|
74 | return null;
|
---|
75 | case 6:
|
---|
76 | if (mod.getFile() != null)
|
---|
77 | return sdf.format(new Date(
|
---|
78 | mod.getFile().getTimestamp() * 1000));
|
---|
79 | else
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 | return null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public String getColumnName(int col) {
|
---|
87 | switch (col) {
|
---|
88 | case 0:
|
---|
89 | return bundle.getString("mod.install");
|
---|
90 | case 1:
|
---|
91 | return bundle.getString("mod.name");
|
---|
92 | case 2:
|
---|
93 | return bundle.getString("mod.package_number");
|
---|
94 | case 3:
|
---|
95 | return bundle.getString("mod.creator");
|
---|
96 | case 4:
|
---|
97 | return bundle.getString("mod.state");
|
---|
98 | case 5:
|
---|
99 | return bundle.getString("mod.added");
|
---|
100 | case 6:
|
---|
101 | return bundle.getString("mod.date");
|
---|
102 | }
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public int getRowCount() {
|
---|
108 | return items.size();
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public int getColumnCount() {
|
---|
113 | return 7;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public Class<?> getColumnClass(int col) {
|
---|
118 | switch (col) {
|
---|
119 | case 0:
|
---|
120 | return Boolean.class;
|
---|
121 | case 1:
|
---|
122 | return String.class;
|
---|
123 | case 2:
|
---|
124 | return String.class;
|
---|
125 | case 3:
|
---|
126 | return String.class;
|
---|
127 | case 4:
|
---|
128 | return String.class;
|
---|
129 | case 5:
|
---|
130 | return String.class;
|
---|
131 | case 6:
|
---|
132 | return String.class;
|
---|
133 | }
|
---|
134 | return null;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Set the constraints on the columns size for the given column
|
---|
139 | *
|
---|
140 | * @param colNum
|
---|
141 | * Column number
|
---|
142 | * @param col
|
---|
143 | * Column object
|
---|
144 | */
|
---|
145 | public void setColumnConstraints(int colNum, TableColumn col) {
|
---|
146 | int w;
|
---|
147 | switch (colNum) {
|
---|
148 | case 0:
|
---|
149 | w = 70;
|
---|
150 | col.setPreferredWidth(w);
|
---|
151 | col.setMinWidth(w);
|
---|
152 | col.setMaxWidth(w);
|
---|
153 | break;
|
---|
154 | case 1:
|
---|
155 | col.setPreferredWidth(150);
|
---|
156 | break;
|
---|
157 | case 2:
|
---|
158 | w = 60;
|
---|
159 | col.setPreferredWidth(w);
|
---|
160 | col.setMinWidth(w);
|
---|
161 | col.setMaxWidth(w);
|
---|
162 | break;
|
---|
163 | case 3:
|
---|
164 | col.setPreferredWidth(90);
|
---|
165 | break;
|
---|
166 | case 4:
|
---|
167 | w = 60;
|
---|
168 | col.setPreferredWidth(w);
|
---|
169 | col.setMinWidth(w);
|
---|
170 | col.setMaxWidth(w);
|
---|
171 | break;
|
---|
172 | case 5:
|
---|
173 | w = 100;
|
---|
174 | col.setPreferredWidth(w);
|
---|
175 | col.setMinWidth(w);
|
---|
176 | col.setMaxWidth(w);
|
---|
177 | break;
|
---|
178 | case 6:
|
---|
179 | w = 100;
|
---|
180 | col.setPreferredWidth(w);
|
---|
181 | col.setMinWidth(w);
|
---|
182 | col.setMaxWidth(w);
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Reload the nodes data after an update to the cache
|
---|
189 | */
|
---|
190 | public void reloadData() {
|
---|
191 | items.clear();
|
---|
192 | switch (contentType) {
|
---|
193 | case MODS:
|
---|
194 | items.addAll(PackageManager.getInstance()
|
---|
195 | .getModsValidAndNotCore());
|
---|
196 | break;
|
---|
197 | case TOOLS:
|
---|
198 | items.addAll(PackageManager.getInstance().getTools());
|
---|
199 | break;
|
---|
200 | case CORE:
|
---|
201 | items.addAll(PackageManager.getInstance().getCoreTools());
|
---|
202 | items.addAll(PackageManager.getInstance().getCoreMods());
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | revertSelection();
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Revert the selection to the mods that are currently installed
|
---|
210 | */
|
---|
211 | public void revertSelection() {
|
---|
212 | install.clear();
|
---|
213 | for (int i = 0; i < items.size(); i++) {
|
---|
214 | boolean installed = items.get(i).isInstalled();
|
---|
215 | install.add(i, installed);
|
---|
216 | }
|
---|
217 | updateDownloadSize();
|
---|
218 | fireTableDataChanged();
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Reload the selection after a config was loaded
|
---|
223 | *
|
---|
224 | * @param config
|
---|
225 | * Config to load
|
---|
226 | */
|
---|
227 | public void reloadSelection(File config) {
|
---|
228 | if (contentType == ETableContentType.MODS) {
|
---|
229 | Vector<Integer> selected = PackageManager.getInstance()
|
---|
230 | .loadModSelection(config);
|
---|
231 | install.clear();
|
---|
232 | for (int i = 0; i < items.size(); i++) {
|
---|
233 | install.add(i,
|
---|
234 | selected.contains(items.get(i).getPackageNumber()));
|
---|
235 | }
|
---|
236 | updateDownloadSize();
|
---|
237 | fireTableDataChanged();
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Get the items vector
|
---|
243 | *
|
---|
244 | * @return Items
|
---|
245 | */
|
---|
246 | public Vector<Package> getItems() {
|
---|
247 | return items;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * @return Mods selected for installation
|
---|
252 | */
|
---|
253 | public TreeSet<Package> getSelectedMods() {
|
---|
254 | TreeSet<Package> res = new TreeSet<Package>();
|
---|
255 | for (int i = 0; i < items.size(); i++) {
|
---|
256 | if (install.get(i))
|
---|
257 | res.add(items.get(i));
|
---|
258 | }
|
---|
259 | return res;
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public boolean isCellEditable(int rowIndex, int columnIndex) {
|
---|
264 | return columnIndex == 0;
|
---|
265 | }
|
---|
266 |
|
---|
267 | private void notifyDownloadSize(int size, int count) {
|
---|
268 | for (ModInstallSelectionListener dsl : listeners)
|
---|
269 | dsl.modInstallSelectionChanged(size, count);
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Check the current download size and notify listeners
|
---|
274 | */
|
---|
275 | public void updateDownloadSize() {
|
---|
276 | int size = 0;
|
---|
277 | int count = 0;
|
---|
278 | for (int i = 0; i < items.size(); i++) {
|
---|
279 | if (install.get(i)) {
|
---|
280 | count++;
|
---|
281 | Package m = items.get(i);
|
---|
282 | if (!m.isLocalAvailable())
|
---|
283 | size += m.getZipSize();
|
---|
284 | }
|
---|
285 | }
|
---|
286 | notifyDownloadSize(size, count);
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Override
|
---|
290 | public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
---|
291 | super.setValueAt(aValue, rowIndex, columnIndex);
|
---|
292 | if (columnIndex == 0) {
|
---|
293 | selectPackage(rowIndex, (Boolean) aValue);
|
---|
294 | updateDownloadSize();
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Select package in given row
|
---|
300 | *
|
---|
301 | * @param row
|
---|
302 | * Row of package
|
---|
303 | * @param select
|
---|
304 | * Select or not
|
---|
305 | */
|
---|
306 | public void selectPackage(int row, boolean select) {
|
---|
307 | install.set(row, select);
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * @param lis
|
---|
312 | * Listener to receive download size changed events
|
---|
313 | */
|
---|
314 | public void addDownloadSizeListener(ModInstallSelectionListener lis) {
|
---|
315 | listeners.add(lis);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * @param lis
|
---|
320 | * Listener to no longer receive download size changed events
|
---|
321 | */
|
---|
322 | public void removeDownloadSizeListener(ModInstallSelectionListener lis) {
|
---|
323 | listeners.remove(lis);
|
---|
324 | }
|
---|
325 |
|
---|
326 | }
|
---|