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