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