| 1 |
package net.oni2.aeinstaller.gui; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.List; |
| 5 |
import java.util.ResourceBundle; |
| 6 |
import java.util.TreeMap; |
| 7 |
import java.util.TreeSet; |
| 8 |
|
| 9 |
import javax.swing.JButton; |
| 10 |
import javax.swing.JComboBox; |
| 11 |
import javax.swing.JComponent; |
| 12 |
import javax.swing.JFrame; |
| 13 |
import javax.swing.JLabel; |
| 14 |
import javax.swing.JMenu; |
| 15 |
import javax.swing.JOptionPane; |
| 16 |
import javax.swing.JSplitPane; |
| 17 |
import javax.swing.JTable; |
| 18 |
import javax.swing.ListSelectionModel; |
| 19 |
import javax.swing.RowSorter; |
| 20 |
import javax.swing.SortOrder; |
| 21 |
import javax.swing.SwingUtilities; |
| 22 |
import javax.swing.event.ListSelectionEvent; |
| 23 |
import javax.swing.event.ListSelectionListener; |
| 24 |
import javax.swing.table.TableRowSorter; |
| 25 |
|
| 26 |
import net.oni2.aeinstaller.backend.Settings; |
| 27 |
import net.oni2.aeinstaller.backend.Settings.Platform; |
| 28 |
import net.oni2.aeinstaller.backend.SizeFormatter; |
| 29 |
import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener; |
| 30 |
import net.oni2.aeinstaller.backend.depot.DepotManager; |
| 31 |
import net.oni2.aeinstaller.backend.mods.Mod; |
| 32 |
import net.oni2.aeinstaller.backend.mods.ModManager; |
| 33 |
import net.oni2.aeinstaller.backend.mods.Type; |
| 34 |
import net.oni2.aeinstaller.backend.oni.InstallProgressListener; |
| 35 |
import net.oni2.aeinstaller.backend.oni.Installer; |
| 36 |
import net.oni2.aeinstaller.gui.about.AboutDialog; |
| 37 |
import net.oni2.aeinstaller.gui.modtable.DownloadSizeListener; |
| 38 |
import net.oni2.aeinstaller.gui.modtable.ModTableFilter; |
| 39 |
import net.oni2.aeinstaller.gui.modtable.ModTableModel; |
| 40 |
import net.oni2.aeinstaller.gui.settings.SettingsDialog; |
| 41 |
|
| 42 |
import org.javabuilders.BuildResult; |
| 43 |
import org.javabuilders.annotations.DoInBackground; |
| 44 |
import org.javabuilders.event.BackgroundEvent; |
| 45 |
import org.javabuilders.swing.SwingJavaBuilder; |
| 46 |
import org.simplericity.macify.eawt.ApplicationEvent; |
| 47 |
import org.simplericity.macify.eawt.ApplicationListener; |
| 48 |
|
| 49 |
/** |
| 50 |
* @author Christian Illy |
| 51 |
*/ |
| 52 |
public class MainWin extends JFrame implements ApplicationListener, |
| 53 |
DownloadSizeListener { |
| 54 |
private static final long serialVersionUID = -4027395051382659650L; |
| 55 |
|
| 56 |
private ResourceBundle bundle = ResourceBundle.getBundle(getClass() |
| 57 |
.getName()); |
| 58 |
@SuppressWarnings("unused") |
| 59 |
private BuildResult result = SwingJavaBuilder.build(this, bundle); |
| 60 |
|
| 61 |
private JMenu mainMenu; |
| 62 |
|
| 63 |
private JSplitPane contents; |
| 64 |
|
| 65 |
private JComboBox cmbModTypes; |
| 66 |
private JTable tblMods; |
| 67 |
private ModTableModel model; |
| 68 |
private TableRowSorter<ModTableModel> sorter; |
| 69 |
private JLabel lblDownloadSizeVal; |
| 70 |
|
| 71 |
private JLabel lblSubmitterVal; |
| 72 |
private JLabel lblCreatorVal; |
| 73 |
private HTMLLinkLabel lblDescriptionVal; |
| 74 |
|
| 75 |
private JButton btnInstall; |
| 76 |
|
| 77 |
/** |
| 78 |
* Constructor of main window. |
| 79 |
*/ |
| 80 |
public MainWin() { |
| 81 |
this.setTitle(SwingJavaBuilder.getConfig().getResource("appname") |
| 82 |
+ " - v" |
| 83 |
+ SwingJavaBuilder.getConfig().getResource("appversion")); |
| 84 |
|
| 85 |
contents.setDividerLocation(400); |
| 86 |
|
| 87 |
if (Settings.getPlatform() == Platform.MACOS) { |
| 88 |
mainMenu.setVisible(false); |
| 89 |
} |
| 90 |
|
| 91 |
getRootPane().setDefaultButton(btnInstall); |
| 92 |
} |
| 93 |
|
| 94 |
private void initModTypeBox() { |
| 95 |
cmbModTypes.removeAllItems(); |
| 96 |
|
| 97 |
TreeMap<String, Type> types = new TreeMap<String, Type>(); |
| 98 |
for (Type t : ModManager.getInstance().getTypesWithContent()) { |
| 99 |
types.put(t.getName(), t); |
| 100 |
} |
| 101 |
for (Type t : types.values()) { |
| 102 |
cmbModTypes.addItem(t); |
| 103 |
} |
| 104 |
cmbModTypes.setSelectedIndex(0); |
| 105 |
} |
| 106 |
|
| 107 |
private void initTable() { |
| 108 |
tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 109 |
tblMods.getSelectionModel().addListSelectionListener( |
| 110 |
new ListSelectionListener() { |
| 111 |
|
| 112 |
@Override |
| 113 |
public void valueChanged(ListSelectionEvent e) { |
| 114 |
int viewRow = tblMods.getSelectedRow(); |
| 115 |
if (viewRow < 0) { |
| 116 |
modSelection(null); |
| 117 |
} else { |
| 118 |
int modelRow = tblMods |
| 119 |
.convertRowIndexToModel(viewRow); |
| 120 |
Mod mod = (Mod) model.getValueAt(modelRow, -1); |
| 121 |
modSelection(mod); |
| 122 |
} |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
// To get checkbox-cells with background of row |
| 127 |
((JComponent) tblMods.getDefaultRenderer(Boolean.class)) |
| 128 |
.setOpaque(true); |
| 129 |
|
| 130 |
model = new ModTableModel(); |
| 131 |
model.addDownloadSizeListener(this); |
| 132 |
|
| 133 |
tblMods.setModel(model); |
| 134 |
|
| 135 |
sorter = new TableRowSorter<ModTableModel>(model); |
| 136 |
tblMods.setRowSorter(sorter); |
| 137 |
|
| 138 |
sorter.setRowFilter(new ModTableFilter(null)); |
| 139 |
|
| 140 |
sorter.setSortable(2, false); |
| 141 |
|
| 142 |
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>(); |
| 143 |
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); |
| 144 |
sorter.setSortKeys(sortKeys); |
| 145 |
|
| 146 |
for (int i = 0; i < model.getColumnCount(); i++) { |
| 147 |
model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i)); |
| 148 |
} |
| 149 |
|
| 150 |
// for (int i = 3; i > 0; i--) { |
| 151 |
// tblMods.getColumnModel().removeColumn(tblMods.getColumnModel().getColumn(i)); |
| 152 |
// } |
| 153 |
} |
| 154 |
|
| 155 |
private boolean askClose() { |
| 156 |
int res = JOptionPane.showConfirmDialog(this, |
| 157 |
bundle.getString("askClose.text"), |
| 158 |
bundle.getString("askClose.title"), JOptionPane.YES_NO_OPTION, |
| 159 |
JOptionPane.QUESTION_MESSAGE); |
| 160 |
return res == JOptionPane.YES_OPTION; |
| 161 |
} |
| 162 |
|
| 163 |
private void exit() { |
| 164 |
setVisible(false); |
| 165 |
dispose(); |
| 166 |
} |
| 167 |
|
| 168 |
private void saveLocalData() { |
| 169 |
Settings.getInstance().serializeToFile(); |
| 170 |
DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename()); |
| 171 |
} |
| 172 |
|
| 173 |
@DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false) |
| 174 |
private void execDepotUpdate(final BackgroundEvent evt) { |
| 175 |
try { |
| 176 |
DepotManager.getInstance().updateInformation(false, |
| 177 |
new DepotCacheUpdateProgressListener() { |
| 178 |
|
| 179 |
@Override |
| 180 |
public void cacheUpdateProgress(String stepName, |
| 181 |
int current, int total) { |
| 182 |
evt.setProgressEnd(total); |
| 183 |
evt.setProgressValue(current); |
| 184 |
evt.setProgressMessage(stepName); |
| 185 |
} |
| 186 |
}); |
| 187 |
ModManager.getInstance().init(); |
| 188 |
initTable(); |
| 189 |
initModTypeBox(); |
| 190 |
|
| 191 |
tblMods.setVisible(true); |
| 192 |
} catch (Exception e) { |
| 193 |
e.printStackTrace(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
@SuppressWarnings("unused") |
| 198 |
private void checkUpdates() { |
| 199 |
if (Settings.getInstance().get("notifyupdates", true)) { |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
@SuppressWarnings("unused") |
| 204 |
private void focus() { |
| 205 |
SwingUtilities.invokeLater(new Runnable() { |
| 206 |
|
| 207 |
@Override |
| 208 |
public void run() { |
| 209 |
toFront(); |
| 210 |
repaint(); |
| 211 |
} |
| 212 |
}); |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
private void showSettings() { |
| 217 |
new SettingsDialog().setVisible(true); |
| 218 |
} |
| 219 |
|
| 220 |
private void showAbout() { |
| 221 |
new AboutDialog().setVisible(true); |
| 222 |
} |
| 223 |
|
| 224 |
@SuppressWarnings("unused") |
| 225 |
private void loadConfig() { |
| 226 |
// TODO method stub |
| 227 |
JOptionPane.showMessageDialog(this, "loadConfig", "todo", |
| 228 |
JOptionPane.INFORMATION_MESSAGE); |
| 229 |
} |
| 230 |
|
| 231 |
@SuppressWarnings("unused") |
| 232 |
private void saveConfig() { |
| 233 |
// TODO method stub |
| 234 |
JOptionPane.showMessageDialog(this, "saveConfig", "todo", |
| 235 |
JOptionPane.INFORMATION_MESSAGE); |
| 236 |
} |
| 237 |
|
| 238 |
@DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) |
| 239 |
private void reglobalize(final BackgroundEvent evt) { |
| 240 |
Installer.initializeEdition(new InstallProgressListener() { |
| 241 |
@Override |
| 242 |
public void installProgressUpdate(int done, int total, String step) { |
| 243 |
evt.setProgressEnd(total); |
| 244 |
evt.setProgressValue(done); |
| 245 |
evt.setProgressMessage(step); |
| 246 |
} |
| 247 |
}); |
| 248 |
} |
| 249 |
|
| 250 |
@SuppressWarnings("unused") |
| 251 |
private void tools() { |
| 252 |
// TODO method stub |
| 253 |
JOptionPane.showMessageDialog(this, "tools", "todo", |
| 254 |
JOptionPane.INFORMATION_MESSAGE); |
| 255 |
} |
| 256 |
|
| 257 |
@SuppressWarnings("unused") |
| 258 |
private void revertSelection() { |
| 259 |
// TODO method stub |
| 260 |
JOptionPane.showMessageDialog(this, "revertSelection", "todo", |
| 261 |
JOptionPane.INFORMATION_MESSAGE); |
| 262 |
} |
| 263 |
|
| 264 |
@DoInBackground(progressMessage = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false) |
| 265 |
private void checkMandatoryFiles(final BackgroundEvent evt) { |
| 266 |
//TODO |
| 267 |
System.out.println("Mandatory Tools:"); |
| 268 |
for (Mod m : ModManager.getInstance().getMandatoryTools()) { |
| 269 |
System.out.format(" %05d %15s - Local: %b - Update: %b", m.getPackageNumber(), m.getName(), m.isLocalAvailable(), m.isNewerAvailable()); |
| 270 |
} |
| 271 |
System.out.println(); |
| 272 |
|
| 273 |
System.out.println("Mandatory Mods:"); |
| 274 |
for (Mod m : ModManager.getInstance().getMandatoryMods()) { |
| 275 |
System.out.format(" %05d %15s - Local: %b - Update: %b", m.getPackageNumber(), m.getName(), m.isLocalAvailable(), m.isNewerAvailable()); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
@DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false) |
| 280 |
private void install(final BackgroundEvent evt) { |
| 281 |
// TODO: Conflicts/Dependencies |
| 282 |
|
| 283 |
TreeSet<Mod> mods = new TreeSet<Mod>(); |
| 284 |
mods.addAll(ModManager.getInstance().getMandatoryMods()); |
| 285 |
mods.addAll(model.getSelectedMods()); |
| 286 |
|
| 287 |
System.out.println("Install mods:"); |
| 288 |
for (Mod m : mods) { |
| 289 |
System.out |
| 290 |
.println(" " + m.getPackageNumber() + ": " + m.getName()); |
| 291 |
} |
| 292 |
|
| 293 |
Installer.install(mods, new InstallProgressListener() { |
| 294 |
@Override |
| 295 |
public void installProgressUpdate(int done, int total, String step) { |
| 296 |
evt.setProgressEnd(total); |
| 297 |
evt.setProgressValue(done); |
| 298 |
evt.setProgressMessage(step); |
| 299 |
} |
| 300 |
}); |
| 301 |
} |
| 302 |
|
| 303 |
private void modSelection(Mod m) { |
| 304 |
lblSubmitterVal.setText(""); |
| 305 |
lblCreatorVal.setText(""); |
| 306 |
lblDescriptionVal.setText(""); |
| 307 |
if (m != null) { |
| 308 |
lblSubmitterVal.setText(m.getName()); |
| 309 |
lblCreatorVal.setText(m.getCreator()); |
| 310 |
lblDescriptionVal.setText(m.getDescription()); |
| 311 |
} |
| 312 |
// TODO |
| 313 |
} |
| 314 |
|
| 315 |
@SuppressWarnings("unused") |
| 316 |
private void modTypeSelection() { |
| 317 |
Type t = (Type) cmbModTypes.getSelectedItem(); |
| 318 |
if (t != null) |
| 319 |
sorter.setRowFilter(new ModTableFilter(t)); |
| 320 |
else |
| 321 |
sorter.setRowFilter(new ModTableFilter(null)); |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
public void downloadSizeChanged(int newSize) { |
| 326 |
lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2)); |
| 327 |
} |
| 328 |
|
| 329 |
@DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) |
| 330 |
private void initialize(final BackgroundEvent evt) { |
| 331 |
if (!Installer.isEditionInitialized()) { |
| 332 |
int res = JOptionPane.showConfirmDialog(this, |
| 333 |
bundle.getString("askInitialize.text"), |
| 334 |
bundle.getString("askInitialize.title"), |
| 335 |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); |
| 336 |
if (res == JOptionPane.NO_OPTION) { |
| 337 |
exit(); |
| 338 |
} else { |
| 339 |
Installer.initializeEdition(new InstallProgressListener() { |
| 340 |
@Override |
| 341 |
public void installProgressUpdate(int done, int total, |
| 342 |
String step) { |
| 343 |
evt.setProgressEnd(total); |
| 344 |
evt.setProgressValue(done); |
| 345 |
evt.setProgressMessage(step); |
| 346 |
} |
| 347 |
}); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public void handleAbout(ApplicationEvent event) { |
| 354 |
event.setHandled(true); |
| 355 |
showAbout(); |
| 356 |
} |
| 357 |
|
| 358 |
@Override |
| 359 |
public void handleOpenApplication(ApplicationEvent event) { |
| 360 |
} |
| 361 |
|
| 362 |
@Override |
| 363 |
public void handleOpenFile(ApplicationEvent event) { |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public void handlePreferences(ApplicationEvent event) { |
| 368 |
showSettings(); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public void handlePrintFile(ApplicationEvent event) { |
| 373 |
} |
| 374 |
|
| 375 |
@Override |
| 376 |
public void handleQuit(ApplicationEvent event) { |
| 377 |
if (askClose()) { |
| 378 |
event.setHandled(true); |
| 379 |
saveLocalData(); |
| 380 |
exit(); |
| 381 |
} else { |
| 382 |
event.setHandled(false); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
@Override |
| 387 |
public void handleReOpenApplication(ApplicationEvent event) { |
| 388 |
} |
| 389 |
|
| 390 |
} |