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

Last change on this file since 644 was 638, checked in by alloc, 12 years ago

AEI2 0.95:

  • Made download-window non-abortable as soon as last file was downloaded (but not yet unpacked)
  • Fixed table to work with local-only packages again (regression introduced when adding last-change column)
  • Added mod version number to contents pane
File size: 5.4 KB
Line 
1package net.oni2.aeinstaller.gui.modtable;
2
3import java.io.File;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6import java.util.HashSet;
7import java.util.ResourceBundle;
8import java.util.TreeSet;
9import java.util.Vector;
10
11import javax.swing.table.AbstractTableModel;
12import javax.swing.table.TableColumn;
13
14import net.oni2.aeinstaller.backend.mods.Mod;
15import net.oni2.aeinstaller.backend.mods.ModManager;
16
17/**
18 * @author Christian Illy
19 */
20public 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 if (mod.getFile() != null)
62 return sdf.format(new Date(
63 mod.getFile().getTimestamp() * 1000));
64 else
65 return null;
66 }
67 return null;
68 }
69
70 @Override
71 public String getColumnName(int col) {
72 switch (col) {
73 case 0:
74 return bundle.getString("mod.install");
75 case 1:
76 return bundle.getString("mod.name");
77 case 2:
78 return bundle.getString("mod.package_number");
79 case 3:
80 return bundle.getString("mod.creator");
81 case 4:
82 return bundle.getString("mod.state");
83 case 5:
84 return bundle.getString("mod.date");
85 }
86 return null;
87 }
88
89 @Override
90 public int getRowCount() {
91 return items.size();
92 }
93
94 @Override
95 public int getColumnCount() {
96 return 6;
97 }
98
99 @Override
100 public Class<?> getColumnClass(int col) {
101 switch (col) {
102 case 0:
103 return Boolean.class;
104 case 1:
105 return String.class;
106 case 2:
107 return String.class;
108 case 3:
109 return String.class;
110 case 4:
111 return String.class;
112 case 5:
113 return String.class;
114 }
115 return null;
116 }
117
118 /**
119 * Set the constraints on the columns size for the given column
120 *
121 * @param colNum
122 * Column number
123 * @param col
124 * Column object
125 */
126 public void setColumnConstraints(int colNum, TableColumn col) {
127 int w;
128 switch (colNum) {
129 case 0:
130 w = 60;
131 col.setPreferredWidth(w);
132 col.setMinWidth(w);
133 col.setMaxWidth(w);
134 break;
135 case 1:
136 col.setPreferredWidth(150);
137 break;
138 case 2:
139 w = 55;
140 col.setPreferredWidth(w);
141 col.setMinWidth(w);
142 col.setMaxWidth(w);
143 break;
144 case 3:
145 col.setPreferredWidth(90);
146 break;
147 case 4:
148 w = 55;
149 col.setPreferredWidth(w);
150 col.setMinWidth(w);
151 col.setMaxWidth(w);
152 break;
153 case 5:
154 w = 95;
155 col.setPreferredWidth(w);
156 col.setMinWidth(w);
157 col.setMaxWidth(w);
158 break;
159 }
160 }
161
162 /**
163 * Reload the nodes data after an update to the cache
164 */
165 public void reloadData() {
166 items.clear();
167 items.addAll(ModManager.getInstance().getModsValidAndNotMandatory());
168 revertSelection();
169 }
170
171 /**
172 * Revert the selection to the mods that are currently installed
173 */
174 public void revertSelection() {
175 install.clear();
176 for (int i = 0; i < items.size(); i++) {
177 install.add(i, items.get(i).isInstalled());
178 }
179 fireTableDataChanged();
180 }
181
182 /**
183 * Reload the selection after a config was loaded
184 *
185 * @param config
186 * Config to load
187 */
188 public void reloadSelection(File config) {
189 Vector<Integer> selected = ModManager.getInstance().loadModSelection(
190 config);
191 install.clear();
192 for (int i = 0; i < items.size(); i++) {
193 install.add(i, selected.contains(items.get(i).getPackageNumber()));
194 }
195 fireTableDataChanged();
196 }
197
198 /**
199 * Get the items vector
200 *
201 * @return Items
202 */
203 public Vector<Mod> getItems() {
204 return items;
205 }
206
207 /**
208 * @return Mods selected for installation
209 */
210 public TreeSet<Mod> getSelectedMods() {
211 TreeSet<Mod> res = new TreeSet<Mod>();
212 for (int i = 0; i < items.size(); i++) {
213 if (install.get(i))
214 res.add(items.get(i));
215 }
216 return res;
217 }
218
219 @Override
220 public boolean isCellEditable(int rowIndex, int columnIndex) {
221 return columnIndex == 0;
222 }
223
224 @Override
225 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
226 super.setValueAt(aValue, rowIndex, columnIndex);
227 if (columnIndex == 0) {
228 install.set(rowIndex, (Boolean) aValue);
229
230 int size = 0;
231 for (int i = 0; i < items.size(); i++) {
232 if (install.get(i)) {
233 Mod m = items.get(i);
234 if (!m.isLocalAvailable())
235 size += m.getZipSize();
236 }
237 }
238 for (DownloadSizeListener dsl : listeners)
239 dsl.downloadSizeChanged(size);
240 }
241 }
242
243 /**
244 * @param lis
245 * Listener to receive download size changed events
246 */
247 public void addDownloadSizeListener(DownloadSizeListener lis) {
248 listeners.add(lis);
249 }
250
251 /**
252 * @param lis
253 * Listener to no longer receive download size changed events
254 */
255 public void removeDownloadSizeListener(DownloadSizeListener lis) {
256 listeners.remove(lis);
257 }
258
259}
Note: See TracBrowser for help on using the repository browser.