| 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 chkNotifyNewPackagesOnStart; |
| 42 |
private JCheckBox chkNotifyDepsAfterInstall; |
| 43 |
private JCheckBox chkCopyIntro; |
| 44 |
private JCheckBox chkCopyOutro; |
| 45 |
|
| 46 |
private JCheckBox chkUseProxy; |
| 47 |
private JTextField txtProxyHost; |
| 48 |
private JTextField txtProxyPort; |
| 49 |
|
| 50 |
/** |
| 51 |
* Open the settings |
| 52 |
*/ |
| 53 |
public SettingsDialog() { |
| 54 |
setResizable(false); |
| 55 |
|
| 56 |
AbstractAction closeAction = new AbstractAction() { |
| 57 |
|
| 58 |
private static final long serialVersionUID = 1L; |
| 59 |
|
| 60 |
public void actionPerformed(ActionEvent arg0) { |
| 61 |
dispose(); |
| 62 |
} |
| 63 |
}; |
| 64 |
KeyStroke ksCtrlW = KeyStroke |
| 65 |
.getKeyStroke('W', KeyEvent.CTRL_DOWN_MASK); |
| 66 |
getRootPane() |
| 67 |
.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) |
| 68 |
.put(ksCtrlW, "close"); |
| 69 |
getRootPane().getActionMap().put("close", closeAction); |
| 70 |
|
| 71 |
initValues(); |
| 72 |
|
| 73 |
setLocationRelativeTo(null); |
| 74 |
} |
| 75 |
|
| 76 |
private void initValues() { |
| 77 |
SettingsManager set = SettingsManager.getInstance(); |
| 78 |
ProxySettings prox = ProxySettings.getInstance(); |
| 79 |
|
| 80 |
laFModel = new LaFComboModel(); |
| 81 |
cmbLaF.setModel(laFModel); |
| 82 |
|
| 83 |
chkNotifyOnStart.setSelected(set.get("notifyupdates", true)); |
| 84 |
chkNotifyNewPackagesOnStart.setSelected(set.get("notifynewpackages", |
| 85 |
true)); |
| 86 |
chkNotifyDepsAfterInstall.setSelected(set.get("notifyDepsAfterInstall", |
| 87 |
false)); |
| 88 |
chkCopyIntro.setSelected(set.get("copyintro", false)); |
| 89 |
chkCopyOutro.setSelected(set.get("copyoutro", true)); |
| 90 |
|
| 91 |
chkUseProxy.setSelected(prox.isUseProxy()); |
| 92 |
txtProxyHost.setText(prox.getHostOrIp()); |
| 93 |
txtProxyPort.setText(String.valueOf(prox.getPort())); |
| 94 |
} |
| 95 |
|
| 96 |
@SuppressWarnings("unused") |
| 97 |
private boolean save() { |
| 98 |
ProxySettings prox = ProxySettings.getInstance(); |
| 99 |
|
| 100 |
boolean proxyUse = chkUseProxy.isSelected(); |
| 101 |
int proxyPort = -1; |
| 102 |
try { |
| 103 |
proxyPort = Integer.valueOf(txtProxyPort.getText()); |
| 104 |
} catch (NumberFormatException e) { |
| 105 |
} |
| 106 |
if ((proxyPort < 1) || (proxyPort > 65535)) { |
| 107 |
JOptionPane.showMessageDialog(this, |
| 108 |
bundle.getString("proxyIllegalPort.text"), |
| 109 |
bundle.getString("proxyIllegalPort.title"), |
| 110 |
JOptionPane.ERROR_MESSAGE); |
| 111 |
return false; |
| 112 |
} |
| 113 |
String proxyHost = txtProxyHost.getText(); |
| 114 |
|
| 115 |
if (!prox.getHostOrIp().equalsIgnoreCase(proxyHost) |
| 116 |
|| (prox.getPort() != proxyPort) |
| 117 |
|| (prox.isUseProxy() != proxyUse)) { |
| 118 |
boolean proxyOldUse = prox.isUseProxy(); |
| 119 |
int proxyOldPort = prox.getPort(); |
| 120 |
String proxyOldHost = prox.getHostOrIp(); |
| 121 |
|
| 122 |
prox.setUseProxy(proxyUse); |
| 123 |
prox.setHostOrIp(proxyHost); |
| 124 |
prox.setPort(proxyPort); |
| 125 |
if (proxyUse) { |
| 126 |
if (!prox.validate()) { |
| 127 |
JOptionPane.showMessageDialog(this, |
| 128 |
bundle.getString("proxyVerifyFailed.text"), |
| 129 |
bundle.getString("proxyVerifyFailed.title"), |
| 130 |
JOptionPane.ERROR_MESSAGE); |
| 131 |
prox.setUseProxy(proxyOldUse); |
| 132 |
prox.setHostOrIp(proxyOldHost); |
| 133 |
prox.setPort(proxyOldPort); |
| 134 |
return false; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
prox.serializeToFile(Paths.getProxySettingsFilename()); |
| 139 |
} |
| 140 |
|
| 141 |
SettingsManager set = SettingsManager.getInstance(); |
| 142 |
|
| 143 |
set.put("notifyupdates", chkNotifyOnStart.isSelected()); |
| 144 |
set.put("notifynewpackages", chkNotifyNewPackagesOnStart.isSelected()); |
| 145 |
set.put("notifyDepsAfterInstall", |
| 146 |
chkNotifyDepsAfterInstall.isSelected()); |
| 147 |
set.put("copyintro", chkCopyIntro.isSelected()); |
| 148 |
set.put("copyoutro", chkCopyOutro.isSelected()); |
| 149 |
|
| 150 |
String oldLaf = set.get("lookandfeel", UIManager.getLookAndFeel() |
| 151 |
.getClass().getName()); |
| 152 |
String newLaf = laFModel.getSelectedClassName(); |
| 153 |
|
| 154 |
if (!newLaf.equals(oldLaf)) { |
| 155 |
set.put("lookandfeel", newLaf); |
| 156 |
JOptionPane.showMessageDialog(this, |
| 157 |
bundle.getString("newLaF.text"), |
| 158 |
bundle.getString("newLaF.title"), |
| 159 |
JOptionPane.INFORMATION_MESSAGE); |
| 160 |
} |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
} |