source: java/installer2/src/net/oni2/aeinstaller/gui/modtable/ModTableModel.java@ 852

Last change on this file since 852 was 840, checked in by alloc, 12 years ago

AEI2.07:

  • Allow for non-ISO-8859-1 characters in properties files (more exactly require them to be UTF8)
File size: 7.1 KB
RevLine 
[591]1package net.oni2.aeinstaller.gui.modtable;
2
[604]3import java.io.File;
[637]4import java.text.SimpleDateFormat;
5import java.util.Date;
[600]6import java.util.HashSet;
[591]7import java.util.ResourceBundle;
[600]8import java.util.TreeSet;
[591]9import java.util.Vector;
10
11import javax.swing.table.AbstractTableModel;
12import javax.swing.table.TableColumn;
13
[648]14import net.oni2.aeinstaller.backend.packages.Package;
15import net.oni2.aeinstaller.backend.packages.PackageManager;
[658]16import net.oni2.aeinstaller.gui.modtable.ModTable.ETableContentType;
[840]17import net.oni2.resourcebundle.UTF8ResourceBundleLoader;
[591]18
19/**
20 * @author Christian Illy
21 */
22public class ModTableModel extends AbstractTableModel {
23
24 private static final long serialVersionUID = -8278155705802697354L;
25
[840]26 private ResourceBundle bundle = UTF8ResourceBundleLoader
[631]27 .getBundle("net.oni2.aeinstaller.localization.ModTable");
[591]28
[648]29 private Vector<Package> items = new Vector<Package>();
[600]30 private Vector<Boolean> install = new Vector<Boolean>();
[591]31
[673]32 private HashSet<ModInstallSelectionListener> listeners = new HashSet<ModInstallSelectionListener>();
[600]33
[646]34 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
35
[658]36 private ETableContentType contentType = ETableContentType.MODS;
37
[591]38 /**
39 * Create a new model
[658]40 *
41 * @param contentType
42 * Content type to show
[591]43 */
[658]44 public ModTableModel(ETableContentType contentType) {
45 this.contentType = contentType;
[591]46 }
47
48 @Override
49 public Object getValueAt(int row, int col) {
[648]50 Package mod = items.get(row);
[591]51 switch (col) {
52 case -1:
[600]53 return mod;
[591]54 case 0:
[606]55 return install.get(row);
56 case 1:
[600]57 return mod.getName();
[606]58 case 2:
[604]59 return mod.getPackageNumberString();
[630]60 case 3:
61 return mod.getCreator();
[631]62 case 4:
63 String res = "";
[648]64 res += (mod.isInstalled() ? "I" : "_");
[631]65 res += (mod.isLocalAvailable() && mod.isNewerAvailable() ? "U"
66 : "_");
67 res += (mod.isLocalAvailable() ? "D" : "_");
68 return res;
[637]69 case 5:
[657]70 if (mod.getNode() != null)
71 return sdf.format(new Date(
72 mod.getNode().getCreated() * 1000));
73 else
74 return null;
75 case 6:
[638]76 if (mod.getFile() != null)
77 return sdf.format(new Date(
78 mod.getFile().getTimestamp() * 1000));
79 else
80 return null;
[591]81 }
82 return null;
83 }
84
85 @Override
86 public String getColumnName(int col) {
87 switch (col) {
88 case 0:
[606]89 return bundle.getString("mod.install");
90 case 1:
[591]91 return bundle.getString("mod.name");
[606]92 case 2:
[591]93 return bundle.getString("mod.package_number");
[630]94 case 3:
95 return bundle.getString("mod.creator");
[631]96 case 4:
97 return bundle.getString("mod.state");
[637]98 case 5:
[657]99 return bundle.getString("mod.added");
100 case 6:
[637]101 return bundle.getString("mod.date");
[591]102 }
103 return null;
104 }
105
106 @Override
107 public int getRowCount() {
108 return items.size();
109 }
110
111 @Override
112 public int getColumnCount() {
[657]113 return 7;
[591]114 }
115
116 @Override
117 public Class<?> getColumnClass(int col) {
118 switch (col) {
119 case 0:
[606]120 return Boolean.class;
[591]121 case 1:
[604]122 return String.class;
[591]123 case 2:
124 return String.class;
[630]125 case 3:
126 return String.class;
[631]127 case 4:
128 return String.class;
[637]129 case 5:
130 return String.class;
[657]131 case 6:
132 return String.class;
[591]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:
[651]149 w = 70;
[591]150 col.setPreferredWidth(w);
151 col.setMinWidth(w);
152 col.setMaxWidth(w);
153 break;
[606]154 case 1:
155 col.setPreferredWidth(150);
156 break;
[591]157 case 2:
[651]158 w = 60;
[591]159 col.setPreferredWidth(w);
160 col.setMinWidth(w);
161 col.setMaxWidth(w);
162 break;
[630]163 case 3:
164 col.setPreferredWidth(90);
165 break;
[631]166 case 4:
[651]167 w = 60;
[631]168 col.setPreferredWidth(w);
169 col.setMinWidth(w);
170 col.setMaxWidth(w);
171 break;
[637]172 case 5:
[657]173 w = 100;
[637]174 col.setPreferredWidth(w);
175 col.setMinWidth(w);
176 col.setMaxWidth(w);
177 break;
[657]178 case 6:
179 w = 100;
180 col.setPreferredWidth(w);
181 col.setMinWidth(w);
182 col.setMaxWidth(w);
183 break;
[591]184 }
185 }
186
187 /**
188 * Reload the nodes data after an update to the cache
189 */
190 public void reloadData() {
[600]191 items.clear();
[658]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 }
[604]205 revertSelection();
206 }
207
208 /**
209 * Revert the selection to the mods that are currently installed
210 */
211 public void revertSelection() {
[600]212 install.clear();
[593]213 for (int i = 0; i < items.size(); i++) {
[673]214 boolean installed = items.get(i).isInstalled();
215 install.add(i, installed);
[593]216 }
[808]217 updateDownloadSize();
[604]218 fireTableDataChanged();
[591]219 }
220
221 /**
[604]222 * Reload the selection after a config was loaded
223 *
224 * @param config
225 * Config to load
226 */
227 public void reloadSelection(File config) {
[658]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 }
[808]236 updateDownloadSize();
[658]237 fireTableDataChanged();
[604]238 }
239 }
240
241 /**
[591]242 * Get the items vector
243 *
244 * @return Items
245 */
[648]246 public Vector<Package> getItems() {
[591]247 return items;
248 }
249
[600]250 /**
251 * @return Mods selected for installation
252 */
[648]253 public TreeSet<Package> getSelectedMods() {
254 TreeSet<Package> res = new TreeSet<Package>();
[600]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
[593]262 @Override
263 public boolean isCellEditable(int rowIndex, int columnIndex) {
[606]264 return columnIndex == 0;
[593]265 }
[648]266
[673]267 private void notifyDownloadSize(int size, int count) {
268 for (ModInstallSelectionListener dsl : listeners)
269 dsl.modInstallSelectionChanged(size, count);
[646]270 }
[593]271
[752]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
[593]289 @Override
290 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
291 super.setValueAt(aValue, rowIndex, columnIndex);
[606]292 if (columnIndex == 0) {
[752]293 selectPackage(rowIndex, (Boolean) aValue);
294 updateDownloadSize();
[593]295 }
296 }
297
[600]298 /**
[752]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 /**
[600]311 * @param lis
312 * Listener to receive download size changed events
313 */
[673]314 public void addDownloadSizeListener(ModInstallSelectionListener lis) {
[600]315 listeners.add(lis);
316 }
317
318 /**
319 * @param lis
320 * Listener to no longer receive download size changed events
321 */
[673]322 public void removeDownloadSizeListener(ModInstallSelectionListener lis) {
[600]323 listeners.remove(lis);
324 }
325
[591]326}
Note: See TracBrowser for help on using the repository browser.