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