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