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

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

AEI2 0.90:

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