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