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

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

AEI2:

  • Added mod download prior to installation
  • Dependency checking (needs verification)
  • Conflicts checking basis (not implemented)
  • Run Oni through AEI
File size: 5.0 KB
RevLine 
[591]1package net.oni2.aeinstaller.gui.modtable;
2
[604]3import java.io.File;
[600]4import java.util.HashSet;
[591]5import java.util.ResourceBundle;
[600]6import java.util.TreeSet;
[591]7import java.util.Vector;
8
9import javax.swing.table.AbstractTableModel;
10import javax.swing.table.TableColumn;
11
[600]12import net.oni2.aeinstaller.backend.mods.Mod;
13import net.oni2.aeinstaller.backend.mods.ModManager;
14import net.oni2.aeinstaller.backend.mods.Type;
[591]15
16/**
17 * @author Christian Illy
18 */
19public class ModTableModel extends AbstractTableModel {
20
21 private static final long serialVersionUID = -8278155705802697354L;
22
23 private ResourceBundle bundle = ResourceBundle.getBundle(getClass()
24 .getName());
25
[600]26 private Vector<Mod> items = new Vector<Mod>();
27 private Vector<Boolean> install = new Vector<Boolean>();
[591]28
[600]29 private HashSet<DownloadSizeListener> listeners = new HashSet<DownloadSizeListener>();
30
[591]31 /**
32 * Create a new model
33 */
34 public ModTableModel() {
35 reloadData();
36 }
37
38 @Override
39 public Object getValueAt(int row, int col) {
[600]40 Mod mod = items.get(row);
[591]41 switch (col) {
42 case -1:
[600]43 return mod;
[591]44 case 0:
[600]45 return mod.getName();
[591]46 case 1:
[604]47 return mod.getPackageNumberString();
[591]48 case 2:
49 String type = "";
[600]50 for (Type t : mod.getTypes()) {
[591]51 if (type.length() > 0)
52 type += ", ";
[600]53 type += t.getName();
[591]54 }
55 return type;
56 case 3:
[600]57 return mod.getPlatform().toString();
[593]58 case 4:
59 return install.get(row);
[591]60 }
61 return null;
62 }
63
64 @Override
65 public String getColumnName(int col) {
66 switch (col) {
67 case 0:
68 return bundle.getString("mod.name");
69 case 1:
70 return bundle.getString("mod.package_number");
71 case 2:
72 return bundle.getString("mod.type");
73 case 3:
74 return bundle.getString("mod.platform");
[593]75 case 4:
76 return bundle.getString("mod.install");
[591]77 }
78 return null;
79 }
80
81 @Override
82 public int getRowCount() {
83 return items.size();
84 }
85
86 @Override
87 public int getColumnCount() {
[593]88 return 5;
[591]89 }
90
91 @Override
92 public Class<?> getColumnClass(int col) {
93 switch (col) {
94 case 0:
95 return String.class;
96 case 1:
[604]97 return String.class;
[591]98 case 2:
99 return String.class;
100 case 3:
101 return String.class;
[593]102 case 4:
103 return Boolean.class;
[591]104 }
105 return null;
106 }
107
108 /**
109 * Set the constraints on the columns size for the given column
110 *
111 * @param colNum
112 * Column number
113 * @param col
114 * Column object
115 */
116 public void setColumnConstraints(int colNum, TableColumn col) {
117 int w;
118 switch (colNum) {
119 case 0:
120 col.setPreferredWidth(150);
121 break;
122 case 1:
123 w = 55;
124 col.setPreferredWidth(w);
125 col.setMinWidth(w);
126 col.setMaxWidth(w);
127 break;
128 case 2:
129 col.setPreferredWidth(100);
130 break;
131 case 3:
132 w = 70;
133 col.setPreferredWidth(w);
134 col.setMinWidth(w);
135 col.setMaxWidth(w);
136 break;
[593]137 case 4:
138 w = 60;
139 col.setPreferredWidth(w);
140 col.setMinWidth(w);
141 col.setMaxWidth(w);
142 break;
[591]143 }
144 }
145
146 /**
147 * Reload the nodes data after an update to the cache
148 */
149 public void reloadData() {
[600]150 items.clear();
151 items.addAll(ModManager.getInstance().getMods());
[604]152 revertSelection();
153 }
154
155 /**
156 * Revert the selection to the mods that are currently installed
157 */
158 public void revertSelection() {
[600]159 install.clear();
[593]160 for (int i = 0; i < items.size(); i++) {
[604]161 install.add(i, ModManager.getInstance()
162 .isModInstalled(items.get(i)));
[593]163 }
[604]164 fireTableDataChanged();
[591]165 }
166
167 /**
[604]168 * Reload the selection after a config was loaded
169 *
170 * @param config
171 * Config to load
172 */
173 public void reloadSelection(File config) {
174 Vector<Integer> selected = ModManager.getInstance().loadModSelection(
175 config);
176 install.clear();
177 for (int i = 0; i < items.size(); i++) {
178 install.add(i, selected.contains(items.get(i).getPackageNumber()));
179 }
180 fireTableDataChanged();
181 }
182
183 /**
[591]184 * Get the items vector
185 *
186 * @return Items
187 */
[600]188 public Vector<Mod> getItems() {
[591]189 return items;
190 }
191
[600]192 /**
193 * @return Mods selected for installation
194 */
195 public TreeSet<Mod> getSelectedMods() {
196 TreeSet<Mod> res = new TreeSet<Mod>();
197 for (int i = 0; i < items.size(); i++) {
198 if (install.get(i))
199 res.add(items.get(i));
200 }
201 return res;
202 }
203
[593]204 @Override
205 public boolean isCellEditable(int rowIndex, int columnIndex) {
206 return columnIndex == 4;
207 }
208
209 @Override
210 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
211 super.setValueAt(aValue, rowIndex, columnIndex);
212 if (columnIndex == 4) {
213 install.set(rowIndex, (Boolean) aValue);
[600]214
215 int size = 0;
216 for (int i = 0; i < items.size(); i++) {
217 if (install.get(i)) {
218 Mod m = items.get(i);
[605]219 if (!m.isLocalAvailable())
[600]220 size += m.getZipSize();
221 }
222 }
223 for (DownloadSizeListener dsl : listeners)
224 dsl.downloadSizeChanged(size);
[593]225 }
226 }
227
[600]228 /**
229 * @param lis
230 * Listener to receive download size changed events
231 */
232 public void addDownloadSizeListener(DownloadSizeListener lis) {
233 listeners.add(lis);
234 }
235
236 /**
237 * @param lis
238 * Listener to no longer receive download size changed events
239 */
240 public void removeDownloadSizeListener(DownloadSizeListener lis) {
241 listeners.remove(lis);
242 }
243
[591]244}
Note: See TracBrowser for help on using the repository browser.