package net.oni2.aeinstaller.gui; import java.awt.BorderLayout; import java.awt.Desktop; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.File; import java.net.URL; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.ResourceBundle; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; import javax.swing.AbstractAction; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JSplitPane; import javax.swing.SwingUtilities; import javax.swing.ToolTipManager; import javax.swing.filechooser.FileFilter; import net.oni2.aeinstaller.AEInstaller2; import net.oni2.aeinstaller.backend.AppExecution; 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.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.backend.oni.OniSplit; 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.ModSelectionListener; import net.oni2.aeinstaller.gui.modtable.ModTable; import net.oni2.aeinstaller.gui.settings.SettingsDialog; import net.oni2.aeinstaller.gui.toolmanager.ToolManager; 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, ModSelectionListener { private static final long serialVersionUID = -4027395051382659650L; private ResourceBundle bundle = ResourceBundle .getBundle("net.oni2.aeinstaller.localization." + getClass().getSimpleName()); @SuppressWarnings("unused") private BuildResult result = SwingJavaBuilder.build(this, bundle); private JMenu mainMenu; private JMenu toolsMenu; private TreeSet toolsMenuItems = new TreeSet(); private JSplitPane contents; private JComboBox cmbModTypes; private JRadioButton radAll; private JRadioButton radOnline; private JRadioButton radLocal; private ModTable tblMods; private JLabel lblDownloadSizeVal; private JLabel lblTitleVal; private JLabel lblSubmitterVal; private JLabel lblCreatorVal; private JLabel lblTypesVal; private JLabel lblPlatformVal; private JLabel lblPackageNumberVal; private JLabel lblVersionNumberVal; private HTMLLinkLabel lblDescriptionVal; private JButton btnInstall; private TreeSet execUpdates = null; private enum EInstallResult { DONE, OFFLINE, INCOMPATIBLE }; private EInstallResult installDone = EInstallResult.DONE; /** * Constructor of main window. */ public MainWin() { this.setTitle(SwingJavaBuilder.getConfig().getResource("appname") + " - v" + SwingJavaBuilder.getConfig().getResource("appversion")); setSize(getWidth()+150, getHeight()); contents.setDividerLocation(500); contents.setResizeWeight(0.4); if (Settings.getPlatform() == Platform.MACOS) { mainMenu.setVisible(false); } ToolTipManager.sharedInstance().setInitialDelay(250); getRootPane().setDefaultButton(btnInstall); lblDownloadSizeVal.setText(SizeFormatter.format(0, 2)); radAll.setSelected(true); tblMods.addModSelectionListener(this); tblMods.addDownloadSizeListener(this); } private void initModTypeBox() { cmbModTypes.removeAllItems(); TreeMap types = new TreeMap(); for (Type t : ModManager.getInstance().getTypesWithContent()) { types.put(t.getName(), t); } cmbModTypes.addItem("-All-"); for (Type t : types.values()) { cmbModTypes.addItem(t); } cmbModTypes.setSelectedIndex(0); } private void exit() { dispose(); System.exit(0); } private void saveLocalData() { Settings.getInstance().serializeToFile(); } @DoInBackground(progressMessage = "updateDepot.title", cancelable = false, indeterminateProgress = false) private void execDepotUpdate(final BackgroundEvent evt) { if (!Settings.getInstance().isOfflineMode()) { long start = new Date().getTime(); try { DepotManager.getInstance().updateInformation(false); } catch (Exception e) { e.printStackTrace(); } System.out.println("Took: " + (new Date().getTime() - start) + " msec"); } ModManager.getInstance().init(); tblMods.reloadData(); initModTypeBox(); tblMods.setVisible(true); } @SuppressWarnings("unused") private void checkUpdates(Object evtSource) { if ((evtSource != this) || Settings.getInstance().get("notifyupdates", true)) { if (Settings.getInstance().isOfflineMode()) { if (evtSource != this) { JOptionPane.showMessageDialog(this, bundle.getString("offlineMode.text"), bundle.getString("offlineMode.title"), JOptionPane.WARNING_MESSAGE); } } else { TreeSet mods = ModManager.getInstance().getUpdatableMods(); TreeSet tools = ModManager.getInstance() .getUpdatableTools(); int size = 0; JPanel panPackages = new JPanel(new GridLayout(0, 1)); execUpdates = new TreeSet(); execUpdates.addAll(mods); execUpdates.addAll(tools); for (final Mod m : mods) { size += m.getZipSize(); JCheckBox check = new JCheckBox("Mod: " + m.getName()); check.setSelected(true); check.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) execUpdates.add(m); else execUpdates.remove(m); } }); panPackages.add(check); } for (final Mod m : tools) { size += m.getZipSize(); JCheckBox check = new JCheckBox("Tool: " + m.getName()); check.setSelected(true); panPackages.add(check); } if (size > 0) { // Build info dialog content JPanel packages = new JPanel(new BorderLayout(0, 7)); JLabel lblIntro = new JLabel("" + bundle.getString("updatesAvailable.text") + ""); JLabel lblSize = new JLabel("" + String.format(bundle .getString("updatesAvailableSize.text"), SizeFormatter.format(size, 3)) + ""); packages.add(lblIntro, BorderLayout.NORTH); packages.add(panPackages, BorderLayout.CENTER); packages.add(lblSize, BorderLayout.SOUTH); JPanel pan = new JPanel(new BorderLayout(0, 25)); pan.add(packages, BorderLayout.CENTER); JCheckBox checkFutureUpdates = new JCheckBox( bundle.getString("checkOnStartup.text")); checkFutureUpdates.setSelected(Settings.getInstance().get( "notifyupdates", true)); checkFutureUpdates.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent evt) { Settings.getInstance().put("notifyupdates", evt.getStateChange() == ItemEvent.SELECTED); } }); pan.add(checkFutureUpdates, BorderLayout.SOUTH); // Show dialog int res = JOptionPane.showConfirmDialog(this, pan, bundle.getString("updatesAvailable.title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (res == JOptionPane.NO_OPTION) { execUpdates = null; } } } } } @SuppressWarnings("unused") private void doUpdate() { if (execUpdates != null && execUpdates.size() > 0) { Downloader dl = new Downloader(execUpdates); try { dl.setVisible(true); if (dl.isFinished()) { TreeSet installed = Installer.getInstalledTools(); TreeSet tools = new TreeSet(); for (Mod m : execUpdates) if (m.isTool() && installed.contains(m.getPackageNumber())) tools.add(m); if (tools.size() > 0) { Installer.installTools(tools); } } } finally { dl.dispose(); } } execUpdates = null; } @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()) tblMods.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, tblMods.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() { new ToolManager().setVisible(true); } @SuppressWarnings("unused") private void refreshToolsMenu() { for (JMenuItem i : toolsMenuItems) { toolsMenu.remove(i); } toolsMenuItems.clear(); for (Mod m : ModManager.getInstance().getInstalledTools()) { if (m.getExeFile() != null && m.getExeFile().exists()) { JMenuItem item = new JMenuItem(); final Vector params = new Vector(); params.add(m.getExeFile().getPath()); final File wd = m.getWorkingDir(); Icon ico = null; if (m.getIconFile() != null && m.getIconFile().exists()) { ico = new ImageIcon(m.getIconFile().getPath()); } else { URL icon = AEInstaller2.class .getResource("images/transparent.png"); ico = new ImageIcon(icon); } item.setAction(new AbstractAction(m.getName(), ico) { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent e) { AppExecution.execute(params, wd); } }); toolsMenuItems.add(item); toolsMenu.add(item); } } } private void revertSelection() { tblMods.revertSelection(); } @DoInBackground(progressMessage = "mandatoryFiles.title", cancelable = false, indeterminateProgress = false) private void checkMandatoryFiles(final BackgroundEvent evt) { if (!Settings.getInstance().isOfflineMode()) { 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(10); } catch (InterruptedException e) { 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(tblMods.getSelectedMods()); boolean instReady = false; installDone = EInstallResult.DONE; while (!instReady) { TreeSet toDownload = new TreeSet(); for (Mod m : mods) { if (!m.isLocalAvailable()) toDownload.add(m); } if (Settings.getInstance().isOfflineMode()) { installDone = EInstallResult.OFFLINE; break; } if (toDownload.size() > 0) { Downloader dl = new Downloader(toDownload); try { dl.setVisible(true); if (!dl.isFinished()) break; } finally { dl.dispose(); } } 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() .checkIncompabitilites(mods); if (conflicts.size() > 0) { installDone = EInstallResult.INCOMPATIBLE; System.err.println("Incompatible mods: " + conflicts.toString()); break; } else { instReady = true; } } } if (instReady) { TreeSet actuallyMods = new TreeSet(); TreeSet actuallyTools = new TreeSet(); for (Mod m : mods) { if (m.isTool()) actuallyTools.add(m); else actuallyMods.add(m); } if (actuallyTools.size() > 0) { Installer.installTools(actuallyTools); } Installer.install(actuallyMods, new InstallProgressListener() { @Override public void installProgressUpdate(int done, int total, String step) { evt.setProgressEnd(total); evt.setProgressValue(done); evt.setProgressMessage(step); } }); installDone = EInstallResult.DONE; } } @SuppressWarnings("unused") private void installDone() { ModManager.getInstance().updateInstalledMods(); revertSelection(); switch (installDone) { case DONE: JOptionPane.showMessageDialog(this, bundle.getString("installDone.text"), bundle.getString("installDone.title"), JOptionPane.INFORMATION_MESSAGE); break; case OFFLINE: JOptionPane.showMessageDialog(this, bundle.getString("offlineMode.text"), bundle.getString("offlineMode.title"), JOptionPane.WARNING_MESSAGE); break; case INCOMPATIBLE: break; } } @Override public void modSelectionChanged(ModTable source, Mod m) { lblTitleVal.setText(""); lblSubmitterVal.setText(""); lblCreatorVal.setText(""); lblDescriptionVal.setText(""); lblTypesVal.setText(""); lblPlatformVal.setText(""); lblPackageNumberVal.setText(""); lblVersionNumberVal.setText(""); if (m != null) { lblTitleVal.setText(m.getName()); lblSubmitterVal.setText(m.getSubmitter()); lblCreatorVal.setText(m.getCreator()); lblDescriptionVal.setText(m.getDescription()); String types = ""; for (Type t : m.getTypes()) { if (types.length() > 0) types += ", "; types += t.getName(); } lblTypesVal.setText(types); lblPlatformVal.setText(m.getPlatform().toString()); lblPackageNumberVal.setText(m.getPackageNumberString()); lblVersionNumberVal.setText(m.getVersion()); } } private void updateTableFilter() { Object o = cmbModTypes.getSelectedItem(); Type t = null; if (o instanceof Type) t = (Type) o; int downloadState = 0; if (radOnline.isSelected()) downloadState = 1; if (radLocal.isSelected()) downloadState = 2; tblMods.setFilter(t, downloadState); } @SuppressWarnings("unused") private void modTypeSelection() { updateTableFilter(); } @SuppressWarnings("unused") private void showTypeSelection() { updateTableFilter(); } @Override public void downloadSizeChanged(int newSize) { lblDownloadSizeVal.setText(SizeFormatter.format(newSize, 2)); } @SuppressWarnings("unused") private void checkInitialize() { if (!Installer.isEditionInitialized()) { if (!OniSplit.isOniSplitInstalled()) { JOptionPane.showMessageDialog(this, bundle.getString("noOniSplit.text"), bundle.getString("noOniSplit.title"), JOptionPane.ERROR_MESSAGE); exit(); } else { 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) { saveLocalData(); exit(); } } } } @DoInBackground(progressMessage = "initializingEdition.title", cancelable = false, indeterminateProgress = false) private void initialize(final BackgroundEvent evt) { if (!Installer.isEditionInitialized()) { 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(); File exe = null; switch (Settings.getPlatform()) { case WIN: exe = new File(Paths.getEditionBasePath(), "Oni.exe"); if (exe.exists()) params.add(exe.getPath()); break; case MACOS: exe = new File(Paths.getEditionBasePath(), "Oni.app/Contents/MacOS/Oni"); if (exe.exists()) params.add(exe.getPath()); break; case LINUX: String wine = Settings.getWinePath(); exe = new File(Paths.getEditionBasePath(), "Oni.exe"); if (exe.exists()) { if (wine != null) { params.add(wine); params.add(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) { AppExecution.execute(params, Paths.getEditionBasePath()); } } @SuppressWarnings("unused") private void oniWin() { Vector params = getBasicOniLaunchParams(); if (params.size() > 0) { params.add("-noswitch"); AppExecution.execute(params, Paths.getEditionBasePath()); } } @SuppressWarnings("unused") private void openEditionFolder() { try { Desktop.getDesktop().open(Paths.getEditionBasePath()); } catch (Exception e) { 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) { } }