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

Last change on this file since 660 was 658, checked in by alloc, 12 years ago

AEI2 0.99i:

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