| 1 | package net.oni2.aeinstaller.gui.toolmanager;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.Dimension;
|
|---|
| 4 | import java.awt.Insets;
|
|---|
| 5 | import java.awt.event.ActionEvent;
|
|---|
| 6 | import java.awt.event.KeyEvent;
|
|---|
| 7 | import java.text.SimpleDateFormat;
|
|---|
| 8 | import java.util.Date;
|
|---|
| 9 | import java.util.ResourceBundle;
|
|---|
| 10 | import java.util.TreeMap;
|
|---|
| 11 | import java.util.TreeSet;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.AbstractAction;
|
|---|
| 14 | import javax.swing.DefaultListModel;
|
|---|
| 15 | import javax.swing.Icon;
|
|---|
| 16 | import javax.swing.ImageIcon;
|
|---|
| 17 | import javax.swing.JButton;
|
|---|
| 18 | import javax.swing.JComponent;
|
|---|
| 19 | import javax.swing.JDialog;
|
|---|
| 20 | import javax.swing.JLabel;
|
|---|
| 21 | import javax.swing.JList;
|
|---|
| 22 | import javax.swing.JOptionPane;
|
|---|
| 23 | import javax.swing.JSplitPane;
|
|---|
| 24 | import javax.swing.KeyStroke;
|
|---|
| 25 | import javax.swing.ListSelectionModel;
|
|---|
| 26 | import javax.swing.event.ListSelectionEvent;
|
|---|
| 27 | import javax.swing.event.ListSelectionListener;
|
|---|
| 28 |
|
|---|
| 29 | import net.oni2.aeinstaller.backend.Settings;
|
|---|
| 30 | import net.oni2.aeinstaller.backend.SizeFormatter;
|
|---|
| 31 | import net.oni2.aeinstaller.backend.packages.Package;
|
|---|
| 32 | import net.oni2.aeinstaller.backend.packages.PackageManager;
|
|---|
| 33 | import net.oni2.aeinstaller.backend.oni.Installer;
|
|---|
| 34 | import net.oni2.aeinstaller.gui.HTMLLinkLabel;
|
|---|
| 35 | import net.oni2.aeinstaller.gui.downloadwindow.Downloader;
|
|---|
| 36 |
|
|---|
| 37 | import org.javabuilders.BuildResult;
|
|---|
| 38 | import org.javabuilders.swing.SwingJavaBuilder;
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * @author Christian Illy
|
|---|
| 42 | */
|
|---|
| 43 | public class ToolManager extends JDialog implements ListSelectionListener {
|
|---|
| 44 | private static final long serialVersionUID = 343221630538866384L;
|
|---|
| 45 |
|
|---|
| 46 | private ResourceBundle bundle = ResourceBundle
|
|---|
| 47 | .getBundle("net.oni2.aeinstaller.localization."
|
|---|
| 48 | + getClass().getSimpleName());
|
|---|
| 49 | @SuppressWarnings("unused")
|
|---|
| 50 | private BuildResult result = SwingJavaBuilder.build(this, bundle);
|
|---|
| 51 |
|
|---|
| 52 | private JSplitPane contents;
|
|---|
| 53 |
|
|---|
| 54 | private JList lstTools;
|
|---|
| 55 |
|
|---|
| 56 | private JLabel lblTitleVal;
|
|---|
| 57 | private JLabel lblCreatorVal;
|
|---|
| 58 | private JLabel lblPlatformVal;
|
|---|
| 59 | private JLabel lblPackageNumberVal;
|
|---|
| 60 | private JLabel lblVersionNumberVal;
|
|---|
| 61 | private JLabel lblLastChangeVal;
|
|---|
| 62 | private HTMLLinkLabel lblDescriptionVal;
|
|---|
| 63 | private JLabel lblDownloadSizeVal;
|
|---|
| 64 |
|
|---|
| 65 | private JButton btnInstall;
|
|---|
| 66 |
|
|---|
| 67 | private Icon icoInstall = null;
|
|---|
| 68 | private Icon icoUninstall = null;
|
|---|
| 69 |
|
|---|
| 70 | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Open the dialog
|
|---|
| 74 | */
|
|---|
| 75 | public ToolManager() {
|
|---|
| 76 | setMinimumSize(new Dimension(getWidth() + 100, getHeight() + 100));
|
|---|
| 77 |
|
|---|
| 78 | AbstractAction closeAction = new AbstractAction() {
|
|---|
| 79 |
|
|---|
| 80 | private static final long serialVersionUID = 1L;
|
|---|
| 81 |
|
|---|
| 82 | public void actionPerformed(ActionEvent arg0) {
|
|---|
| 83 | dispose();
|
|---|
| 84 | }
|
|---|
| 85 | };
|
|---|
| 86 | KeyStroke ksCtrlW = KeyStroke
|
|---|
| 87 | .getKeyStroke('W', KeyEvent.CTRL_DOWN_MASK);
|
|---|
| 88 | getRootPane()
|
|---|
| 89 | .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
|---|
| 90 | .put(ksCtrlW, "close");
|
|---|
| 91 | getRootPane().getActionMap().put("close", closeAction);
|
|---|
| 92 |
|
|---|
| 93 | contents.setDividerLocation(200);
|
|---|
| 94 | contents.setResizeWeight(0.4);
|
|---|
| 95 |
|
|---|
| 96 | lstTools.addListSelectionListener(this);
|
|---|
| 97 | lstTools.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|---|
| 98 |
|
|---|
| 99 | DefaultListModel dlm = new DefaultListModel();
|
|---|
| 100 | TreeMap<String, Package> tools = PackageManager.getInstance().getTools();
|
|---|
| 101 | for (String name : tools.keySet())
|
|---|
| 102 | dlm.addElement(tools.get(name));
|
|---|
| 103 | lstTools.setModel(dlm);
|
|---|
| 104 |
|
|---|
| 105 | icoInstall = new ImageIcon(getClass().getResource(
|
|---|
| 106 | SwingJavaBuilder.getConfig().getResource("img.install")));
|
|---|
| 107 | icoUninstall = new ImageIcon(getClass().getResource(
|
|---|
| 108 | SwingJavaBuilder.getConfig().getResource("img.uninstall")));
|
|---|
| 109 |
|
|---|
| 110 | setLocationRelativeTo(null);
|
|---|
| 111 | lblDescriptionVal.setMargin(new Insets(-15, 0, 0, 0));
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | @SuppressWarnings("unused")
|
|---|
| 115 | private void install() {
|
|---|
| 116 | Object o = lstTools.getSelectedValue();
|
|---|
| 117 | if (o instanceof Package) {
|
|---|
| 118 | Package theMod = (Package) o;
|
|---|
| 119 |
|
|---|
| 120 | if (theMod.isInstalled()) {
|
|---|
| 121 | TreeSet<Package> tools = new TreeSet<Package>();
|
|---|
| 122 | tools.add(theMod);
|
|---|
| 123 | Installer.uninstallTools(tools);
|
|---|
| 124 | } else {
|
|---|
| 125 | if (!theMod.isLocalAvailable()) {
|
|---|
| 126 | if (Settings.getInstance().isOfflineMode()) {
|
|---|
| 127 | JOptionPane.showMessageDialog(this,
|
|---|
| 128 | bundle.getString("offlineMode.text"),
|
|---|
| 129 | bundle.getString("offlineMode.title"),
|
|---|
| 130 | JOptionPane.WARNING_MESSAGE);
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | TreeSet<Package> toDownload = new TreeSet<Package>();
|
|---|
| 135 | toDownload.add(theMod);
|
|---|
| 136 |
|
|---|
| 137 | Downloader dl = new Downloader(toDownload, null);
|
|---|
| 138 | try {
|
|---|
| 139 | dl.setVisible(true);
|
|---|
| 140 | if (!dl.isFinished())
|
|---|
| 141 | return;
|
|---|
| 142 | } finally {
|
|---|
| 143 | dl.dispose();
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | TreeSet<Package> tools = new TreeSet<Package>();
|
|---|
| 148 | tools.add(theMod);
|
|---|
| 149 | Installer.installTools(tools);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | valueChanged(null);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | @Override
|
|---|
| 156 | public void valueChanged(ListSelectionEvent evt) {
|
|---|
| 157 | lblTitleVal.setText("");
|
|---|
| 158 | lblCreatorVal.setText("");
|
|---|
| 159 | lblDescriptionVal.setText("");
|
|---|
| 160 | lblPlatformVal.setText("");
|
|---|
| 161 | lblPackageNumberVal.setText("");
|
|---|
| 162 | lblVersionNumberVal.setText("");
|
|---|
| 163 | lblLastChangeVal.setText("");
|
|---|
| 164 | lblDownloadSizeVal.setText("");
|
|---|
| 165 | btnInstall.setEnabled(false);
|
|---|
| 166 | btnInstall.setIcon(icoInstall);
|
|---|
| 167 |
|
|---|
| 168 | if (lstTools.getSelectedValue() instanceof Package) {
|
|---|
| 169 | Package m = (Package) lstTools.getSelectedValue();
|
|---|
| 170 | lblTitleVal.setText(m.getName());
|
|---|
| 171 | lblCreatorVal.setText(m.getCreator());
|
|---|
| 172 | lblDescriptionVal.setText(m.getDescription());
|
|---|
| 173 | lblPlatformVal.setText(m.getPlatform().toString());
|
|---|
| 174 | lblPackageNumberVal.setText(m.getPackageNumberString());
|
|---|
| 175 | lblVersionNumberVal.setText(m.getVersion());
|
|---|
| 176 | if (m.getFile() != null)
|
|---|
| 177 | lblLastChangeVal.setText(sdf.format(new Date(m.getFile()
|
|---|
| 178 | .getTimestamp() * 1000)));
|
|---|
| 179 | lblDownloadSizeVal.setText(SizeFormatter.format(m.getZipSize(), 3));
|
|---|
| 180 | btnInstall.setEnabled(true);
|
|---|
| 181 | if (m.isInstalled()) {
|
|---|
| 182 | btnInstall.setText(bundle.getString("btnInstall.un.text"));
|
|---|
| 183 | btnInstall.setToolTipText(bundle
|
|---|
| 184 | .getString("btnInstall.un.tooltip"));
|
|---|
| 185 | btnInstall.setIcon(icoUninstall);
|
|---|
| 186 | } else {
|
|---|
| 187 | btnInstall.setText(bundle.getString("btnInstall.text"));
|
|---|
| 188 | btnInstall.setToolTipText(bundle
|
|---|
| 189 | .getString("btnInstall.tooltip"));
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|