package net.oni2.aeinstaller.gui; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.ResourceBundle; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JOptionPane; import javax.swing.JSplitPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.SwingUtilities; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.filechooser.FileFilter; import javax.swing.table.TableRowSorter; import net.oni2.aeinstaller.backend.Paths; import net.oni2.aeinstaller.backend.Settings; import net.oni2.aeinstaller.backend.Settings.Platform; import net.oni2.aeinstaller.backend.SizeFormatter; import net.oni2.aeinstaller.backend.depot.DepotCacheUpdateProgressListener; import net.oni2.aeinstaller.backend.depot.DepotManager; import net.oni2.aeinstaller.backend.mods.Mod; import net.oni2.aeinstaller.backend.mods.ModManager; import net.oni2.aeinstaller.backend.mods.Type; import net.oni2.aeinstaller.backend.mods.download.ModDownloader; import net.oni2.aeinstaller.backend.mods.download.ModDownloader.State; import net.oni2.aeinstaller.backend.mods.download.ModDownloaderListener; import net.oni2.aeinstaller.backend.oni.InstallProgressListener; import net.oni2.aeinstaller.backend.oni.Installer; import net.oni2.aeinstaller.gui.about.AboutDialog; import net.oni2.aeinstaller.gui.downloadwindow.Downloader; import net.oni2.aeinstaller.gui.modtable.DownloadSizeListener; import net.oni2.aeinstaller.gui.modtable.ModTableFilter; import net.oni2.aeinstaller.gui.modtable.ModTableModel; import net.oni2.aeinstaller.gui.settings.SettingsDialog; import org.javabuilders.BuildResult; import org.javabuilders.annotations.DoInBackground; import org.javabuilders.event.BackgroundEvent; import org.javabuilders.swing.SwingJavaBuilder; import org.simplericity.macify.eawt.ApplicationEvent; import org.simplericity.macify.eawt.ApplicationListener; /** * @author Christian Illy */ public class MainWin extends JFrame implements ApplicationListener, DownloadSizeListener { private static final long serialVersionUID = -4027395051382659650L; private ResourceBundle bundle = ResourceBundle.getBundle(getClass() .getName()); @SuppressWarnings("unused") private BuildResult result = SwingJavaBuilder.build(this, bundle); private JMenu mainMenu; private JSplitPane contents; private JComboBox cmbModTypes; private JTable tblMods; private ModTableModel model; private TableRowSorter sorter; private JLabel lblDownloadSizeVal; private JLabel lblSubmitterVal; private JLabel lblCreatorVal; private HTMLLinkLabel lblDescriptionVal; private JButton btnInstall; /** * Constructor of main window. */ public MainWin() { this.setTitle(SwingJavaBuilder.getConfig().getResource("appname") + " - v" + SwingJavaBuilder.getConfig().getResource("appversion")); contents.setDividerLocation(400); if (Settings.getPlatform() == Platform.MACOS) { mainMenu.setVisible(false); } getRootPane().setDefaultButton(btnInstall); lblDownloadSizeVal.setText(SizeFormatter.format(0, 2)); } private void initModTypeBox() { cmbModTypes.removeAllItems(); TreeMap types = new TreeMap(); for (Type t : ModManager.getInstance().getTypesWithContent()) { types.put(t.getName(), t); } for (Type t : types.values()) { cmbModTypes.addItem(t); } cmbModTypes.setSelectedIndex(0); } private void initTable() { tblMods.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); tblMods.getSelectionModel().addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { int viewRow = tblMods.getSelectedRow(); if (viewRow < 0) { modSelection(null); } else { int modelRow = tblMods .convertRowIndexToModel(viewRow); Mod mod = (Mod) model.getValueAt(modelRow, -1); modSelection(mod); } } }); // To get checkbox-cells with background of row ((JComponent) tblMods.getDefaultRenderer(Boolean.class)) .setOpaque(true); model = new ModTableModel(); model.addDownloadSizeListener(this); tblMods.setModel(model); sorter = new TableRowSorter(model); tblMods.setRowSorter(sorter); sorter.setRowFilter(new ModTableFilter(null)); sorter.setSortable(2, false); List sortKeys = new ArrayList(); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); for (int i = 0; i < model.getColumnCount(); i++) { model.setColumnConstraints(i, tblMods.getColumnModel().getColumn(i)); } // for (int i = 3; i > 0; i--) { // tblMods.getColumnModel().removeColumn(tblMods.getColumnModel().getColumn(i)); // } } private void exit() { setVisible(false); dispose(); } private void saveLocalData() { Settings.getInstance().serializeToFile(); DepotManager.getInstance().saveToFile(Settings.getDepotCacheFilename()); } @DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false) private void execDepotUpdate(final BackgroundEvent evt) { try { DepotManager.getInstance().updateInformation(false, new DepotCacheUpdateProgressListener() { @Override public void cacheUpdateProgress(String stepName, int current, int total) { evt.setProgressEnd(total); evt.setProgressValue(current); evt.setProgressMessage(stepName); } }); ModManager.getInstance().init(); initTable(); initModTypeBox(); tblMods.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unused") private void checkUpdates() { if (Settings.getInstance().get("notifyupdates", true)) { } } @SuppressWarnings("unused") private void focus() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { toFront(); repaint(); } }); } private void showSettings() { new SettingsDialog().setVisible(true); } private void showAbout() { new AboutDialog().setVisible(true); } private JFileChooser getConfigOpenSaveDialog(boolean save) { JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(Paths.getEditionBasePath()); if (save) fc.setDialogType(JFileChooser.SAVE_DIALOG); else fc.setDialogType(JFileChooser.OPEN_DIALOG); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setFileFilter(new FileFilter() { @Override public String getDescription() { return "XML files"; } @Override public boolean accept(File arg0) { return (arg0.isDirectory()) || (arg0.getName().toLowerCase().endsWith(".xml")); } }); fc.setMultiSelectionEnabled(false); return fc; } @SuppressWarnings("unused") private void loadConfig() { JFileChooser fc = getConfigOpenSaveDialog(false); int res = fc.showOpenDialog(this); if (res == JFileChooser.APPROVE_OPTION) { if (fc.getSelectedFile().exists()) model.reloadSelection(fc.getSelectedFile()); } } @SuppressWarnings("unused") private void saveConfig() { JFileChooser fc = getConfigOpenSaveDialog(true); int res = fc.showSaveDialog(this); if (res == JFileChooser.APPROVE_OPTION) { File f = fc.getSelectedFile(); if (!f.getName().endsWith(".xml")) f = new File(f.getParentFile(), f.getName() + ".xml"); ModManager.getInstance().saveModSelection(f, model.getSelectedMods()); } } @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) private void reglobalize(final BackgroundEvent evt) { Installer.initializeEdition(new InstallProgressListener() { @Override public void installProgressUpdate(int done, int total, String step) { evt.setProgressEnd(total); evt.setProgressValue(done); evt.setProgressMessage(step); } }); } @SuppressWarnings("unused") private void tools() { // TODO method stub JOptionPane.showMessageDialog(this, "tools", "todo", JOptionPane.INFORMATION_MESSAGE); } @SuppressWarnings("unused") private void revertSelection() { model.revertSelection(); } @DoInBackground(progressMessage = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false) private void checkMandatoryFiles(final BackgroundEvent evt) { TreeSet mand = new TreeSet(); for (Mod m : ModManager.getInstance().getMandatoryTools()) { if (m.isNewerAvailable()) { mand.add(m); } } for (Mod m : ModManager.getInstance().getMandatoryMods()) { if (m.isNewerAvailable()) { mand.add(m); } } if (mand.size() > 0) { ModDownloader m = new ModDownloader(mand, new ModDownloaderListener() { @Override public void updateStatus(ModDownloader source, State state, int filesDown, int filesTotal, int bytesDown, int bytesTotal, int duration, int remaining, int speed) { evt.setProgressEnd(filesTotal); evt.setProgressValue(filesDown); } }); while (!m.isFinished()) { try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } evt.setProgressMessage(bundle.getString("mandatoryToolsInstall.title")); Installer.installTools(ModManager.getInstance().getMandatoryTools()); } @DoInBackground(progressMessage = "installing.title", cancelable = false, indeterminateProgress = false) private void install(final BackgroundEvent evt) { TreeSet mods = new TreeSet(); mods.addAll(ModManager.getInstance().getMandatoryMods()); mods.addAll(model.getSelectedMods()); boolean instReady = false; while (!instReady) { System.out.println("Checking downloads:"); TreeSet toDownload = new TreeSet(); for (Mod m : mods) { if (!m.isLocalAvailable()) toDownload.add(m); } if (toDownload.size() > 0) { System.out.println("Download files: " + toDownload.toString()); Downloader dl = new Downloader(toDownload); dl.setVisible(true); if (!dl.isFinished()) break; } HashMap> dependencies = ModManager.getInstance() .checkDependencies(mods); if (dependencies.size() > 0) { System.out.println("Unmet dependencies: " + dependencies.toString()); for (Mod m : dependencies.keySet()) { for (Mod mDep : dependencies.get(m)) mods.add(mDep); } } else { HashMap> conflicts = ModManager.getInstance() .checkConflicts(mods); if (conflicts.size() > 0) { System.err.println("Conflicting mods: " + conflicts.toString()); break; } else { instReady = true; } } } if (instReady) { Installer.install(mods, new InstallProgressListener() { @Override public void installProgressUpdate(int done, int total, String step) { evt.setProgressEnd(total); evt.setProgressValue(done); evt.setProgressMessage(step); } }); } JOptionPane.showMessageDialog(this, bundle.getString("installDone.text"), bundle.getString("installDone.title"), JOptionPane.INFORMATION_MESSAGE); } private void modSelection(Mod m) { lblSubmitterVal.setText(""); lblCreatorVal.setText(""); lblDescriptionVal.setText(""); if (m != null) { lblSubmitterVal.setText(m.getName()); lblCreatorVal.setText(m.getCreator()); lblDescriptionVal.setText(m.getDescription()); } // TODO } @SuppressWarnings("unused") private void modTypeSelection() { Type t = (Type) cmbModTypes.getSelectedItem(); if (t != null) sorter.setRowFilter(new ModTableFilter(t)); else sorter.setRowFilter(new ModTableFilter(null)); } @Override public void downloadSizeChanged(int newSize) { lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2)); } @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) private void initialize(final BackgroundEvent evt) { if (!Installer.isEditionInitialized()) { int res = JOptionPane.showConfirmDialog(this, bundle.getString("askInitialize.text"), bundle.getString("askInitialize.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (res == JOptionPane.NO_OPTION) { exit(); } else { Installer.initializeEdition(new InstallProgressListener() { @Override public void installProgressUpdate(int done, int total, String step) { evt.setProgressEnd(total); evt.setProgressValue(done); evt.setProgressMessage(step); } }); } } } private Vector getBasicOniLaunchParams() { Vector params = new Vector(); switch (Settings.getPlatform()) { case WIN: params.add(new File(Paths.getEditionBasePath(), "Oni.exe") .getPath()); break; case MACOS: params.add(new File(Paths.getEditionBasePath(), "Oni") .getPath()); break; case LINUX: String wine = Settings.getWinePath(); if (wine != null) { params.add(wine); params.add(new File(Paths.getEditionBasePath(), "Oni.exe") .getPath()); } break; default: } if (params.size() > 0) { params.add("-debugfiles"); } return params; } @SuppressWarnings("unused") private void oniFull() { Vector params = getBasicOniLaunchParams(); if (params.size() > 0) { try { new ProcessBuilder(params).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @SuppressWarnings("unused") private void oniWin() { Vector params = getBasicOniLaunchParams(); if (params.size() > 0) { params.add("-noswitch"); try { new ProcessBuilder(params).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void handleAbout(ApplicationEvent event) { event.setHandled(true); showAbout(); } @Override public void handleOpenApplication(ApplicationEvent event) { } @Override public void handleOpenFile(ApplicationEvent event) { } @Override public void handlePreferences(ApplicationEvent event) { showSettings(); } @Override public void handlePrintFile(ApplicationEvent event) { } @Override public void handleQuit(ApplicationEvent event) { event.setHandled(true); saveLocalData(); exit(); } @Override public void handleReOpenApplication(ApplicationEvent event) { } }