| 1 | package net.oni2.aeinstaller.gui.settings; | 
|---|
| 2 |  | 
|---|
| 3 | import java.awt.event.ActionEvent; | 
|---|
| 4 | import java.awt.event.KeyEvent; | 
|---|
| 5 | import java.util.ResourceBundle; | 
|---|
| 6 |  | 
|---|
| 7 | import javax.swing.AbstractAction; | 
|---|
| 8 | import javax.swing.JCheckBox; | 
|---|
| 9 | import javax.swing.JComboBox; | 
|---|
| 10 | import javax.swing.JComponent; | 
|---|
| 11 | import javax.swing.JDialog; | 
|---|
| 12 | import javax.swing.JOptionPane; | 
|---|
| 13 | import javax.swing.JTextField; | 
|---|
| 14 | import javax.swing.KeyStroke; | 
|---|
| 15 | import javax.swing.UIManager; | 
|---|
| 16 |  | 
|---|
| 17 | import net.oni2.ProxySettings; | 
|---|
| 18 | import net.oni2.SettingsManager; | 
|---|
| 19 | import net.oni2.aeinstaller.backend.Paths; | 
|---|
| 20 | import net.oni2.resourcebundle.UTF8ResourceBundleLoader; | 
|---|
| 21 |  | 
|---|
| 22 | import org.javabuilders.BuildResult; | 
|---|
| 23 | import org.javabuilders.swing.SwingJavaBuilder; | 
|---|
| 24 |  | 
|---|
| 25 | /** | 
|---|
| 26 | * @author Christian Illy | 
|---|
| 27 | */ | 
|---|
| 28 | public class SettingsDialog extends JDialog { | 
|---|
| 29 | private static final long serialVersionUID = -5719515325671846620L; | 
|---|
| 30 |  | 
|---|
| 31 | private ResourceBundle bundle = UTF8ResourceBundleLoader | 
|---|
| 32 | .getBundle("net.oni2.aeinstaller.localization." | 
|---|
| 33 | + getClass().getSimpleName()); | 
|---|
| 34 | @SuppressWarnings("unused") | 
|---|
| 35 | private BuildResult result = SwingJavaBuilder.build(this, bundle); | 
|---|
| 36 |  | 
|---|
| 37 | private JComboBox cmbLaF; | 
|---|
| 38 | private LaFComboModel laFModel; | 
|---|
| 39 |  | 
|---|
| 40 | private JCheckBox chkNotifyOnStart; | 
|---|
| 41 | private JCheckBox chkNotifyDepsAfterInstall; | 
|---|
| 42 | private JCheckBox chkCopyIntro; | 
|---|
| 43 | private JCheckBox chkCopyOutro; | 
|---|
| 44 |  | 
|---|
| 45 | private JCheckBox chkUseProxy; | 
|---|
| 46 | private JTextField txtProxyHost; | 
|---|
| 47 | private JTextField txtProxyPort; | 
|---|
| 48 |  | 
|---|
| 49 | /** | 
|---|
| 50 | * Open the settings | 
|---|
| 51 | */ | 
|---|
| 52 | public SettingsDialog() { | 
|---|
| 53 | setResizable(false); | 
|---|
| 54 |  | 
|---|
| 55 | AbstractAction closeAction = new AbstractAction() { | 
|---|
| 56 |  | 
|---|
| 57 | private static final long serialVersionUID = 1L; | 
|---|
| 58 |  | 
|---|
| 59 | public void actionPerformed(ActionEvent arg0) { | 
|---|
| 60 | dispose(); | 
|---|
| 61 | } | 
|---|
| 62 | }; | 
|---|
| 63 | KeyStroke ksCtrlW = KeyStroke | 
|---|
| 64 | .getKeyStroke('W', KeyEvent.CTRL_DOWN_MASK); | 
|---|
| 65 | getRootPane() | 
|---|
| 66 | .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) | 
|---|
| 67 | .put(ksCtrlW, "close"); | 
|---|
| 68 | getRootPane().getActionMap().put("close", closeAction); | 
|---|
| 69 |  | 
|---|
| 70 | initValues(); | 
|---|
| 71 |  | 
|---|
| 72 | setLocationRelativeTo(null); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | private void initValues() { | 
|---|
| 76 | SettingsManager set = SettingsManager.getInstance(); | 
|---|
| 77 | ProxySettings prox = ProxySettings.getInstance(); | 
|---|
| 78 |  | 
|---|
| 79 | laFModel = new LaFComboModel(); | 
|---|
| 80 | cmbLaF.setModel(laFModel); | 
|---|
| 81 |  | 
|---|
| 82 | chkNotifyOnStart.setSelected(set.get("notifyupdates", true)); | 
|---|
| 83 | chkNotifyDepsAfterInstall.setSelected(set.get("notifyDepsAfterInstall", | 
|---|
| 84 | false)); | 
|---|
| 85 | chkCopyIntro.setSelected(set.get("copyintro", false)); | 
|---|
| 86 | chkCopyOutro.setSelected(set.get("copyoutro", true)); | 
|---|
| 87 |  | 
|---|
| 88 | chkUseProxy.setSelected(prox.isUseProxy()); | 
|---|
| 89 | txtProxyHost.setText(prox.getHostOrIp()); | 
|---|
| 90 | txtProxyPort.setText(String.valueOf(prox.getPort())); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | @SuppressWarnings("unused") | 
|---|
| 94 | private boolean save() { | 
|---|
| 95 |  | 
|---|
| 96 | boolean proxyUse = chkUseProxy.isSelected(); | 
|---|
| 97 | int proxyPort = -1; | 
|---|
| 98 | try { | 
|---|
| 99 | proxyPort = Integer.valueOf(txtProxyPort.getText()); | 
|---|
| 100 | } catch (NumberFormatException e) { | 
|---|
| 101 | } | 
|---|
| 102 | if ((proxyPort < 1) || (proxyPort > 65535)) { | 
|---|
| 103 | JOptionPane.showMessageDialog(this, | 
|---|
| 104 | bundle.getString("proxyIllegalPort.text"), | 
|---|
| 105 | bundle.getString("proxyIllegalPort.title"), | 
|---|
| 106 | JOptionPane.ERROR_MESSAGE); | 
|---|
| 107 | return false; | 
|---|
| 108 | } | 
|---|
| 109 | String proxyHost = txtProxyHost.getText(); | 
|---|
| 110 |  | 
|---|
| 111 | ProxySettings prox = ProxySettings.getInstance(); | 
|---|
| 112 | if (!prox.getHostOrIp().equalsIgnoreCase(proxyHost) | 
|---|
| 113 | || (prox.getPort() != proxyPort) | 
|---|
| 114 | || (prox.isUseProxy() != proxyUse)) { | 
|---|
| 115 | boolean proxyOldUse = prox.isUseProxy(); | 
|---|
| 116 | int proxyOldPort = prox.getPort(); | 
|---|
| 117 | String proxyOldHost = prox.getHostOrIp(); | 
|---|
| 118 |  | 
|---|
| 119 | prox.setUseProxy(proxyUse); | 
|---|
| 120 | prox.setHostOrIp(proxyHost); | 
|---|
| 121 | prox.setPort(proxyPort); | 
|---|
| 122 | if (!prox.validate()) { | 
|---|
| 123 | JOptionPane.showMessageDialog(this, | 
|---|
| 124 | bundle.getString("proxyVerifyFailed.text"), | 
|---|
| 125 | bundle.getString("proxyVerifyFailed.title"), | 
|---|
| 126 | JOptionPane.ERROR_MESSAGE); | 
|---|
| 127 | prox.setUseProxy(proxyOldUse); | 
|---|
| 128 | prox.setHostOrIp(proxyOldHost); | 
|---|
| 129 | prox.setPort(proxyOldPort); | 
|---|
| 130 | return false; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | prox.serializeToFile(Paths.getProxySettingsFilename()); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | SettingsManager set = SettingsManager.getInstance(); | 
|---|
| 137 |  | 
|---|
| 138 | set.put("notifyupdates", chkNotifyOnStart.isSelected()); | 
|---|
| 139 | set.put("notifyDepsAfterInstall", | 
|---|
| 140 | chkNotifyDepsAfterInstall.isSelected()); | 
|---|
| 141 | set.put("copyintro", chkCopyIntro.isSelected()); | 
|---|
| 142 | set.put("copyoutro", chkCopyOutro.isSelected()); | 
|---|
| 143 |  | 
|---|
| 144 | String oldLaf = set.get("lookandfeel", UIManager.getLookAndFeel() | 
|---|
| 145 | .getClass().getName()); | 
|---|
| 146 | String newLaf = laFModel.getSelectedClassName(); | 
|---|
| 147 |  | 
|---|
| 148 | if (!newLaf.equals(oldLaf)) { | 
|---|
| 149 | set.put("lookandfeel", newLaf); | 
|---|
| 150 | JOptionPane.showMessageDialog(this, | 
|---|
| 151 | bundle.getString("newLaF.text"), | 
|---|
| 152 | bundle.getString("newLaF.title"), | 
|---|
| 153 | JOptionPane.INFORMATION_MESSAGE); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | return true; | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|