| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.plugin.builtin.preference; |
| 5 | |
|
| 6 | |
import java.awt.FlowLayout; |
| 7 | |
import java.awt.event.ActionEvent; |
| 8 | |
import java.awt.event.ActionListener; |
| 9 | |
import java.util.logging.Logger; |
| 10 | |
|
| 11 | |
import javax.swing.BoxLayout; |
| 12 | |
import javax.swing.JCheckBox; |
| 13 | |
import javax.swing.JLabel; |
| 14 | |
import javax.swing.JPanel; |
| 15 | |
|
| 16 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 17 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 18 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin; |
| 19 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 20 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
| 21 | |
|
| 22 | |
import ca.digitalcave.moss.swing.MossHintTextField; |
| 23 | |
|
| 24 | 36 | public class NetworkPreferences extends BuddiPreferencePlugin { |
| 25 | |
public static final long serialVersionUID = 0; |
| 26 | |
|
| 27 | |
private final JCheckBox useProxy; |
| 28 | |
private final MossHintTextField proxy; |
| 29 | |
private final MossHintTextField port; |
| 30 | |
|
| 31 | 695 | public NetworkPreferences() { |
| 32 | 695 | useProxy = new JCheckBox(TextFormatter.getTranslation(BuddiKeys.USE_PROXY_SERVER)); |
| 33 | 695 | proxy = new MossHintTextField(TextFormatter.getTranslation(BuddiKeys.HINT_PROXY_SERVER_NAME)); |
| 34 | 695 | port = new MossHintTextField(TextFormatter.getTranslation(BuddiKeys.HINT_PORT)); |
| 35 | |
|
| 36 | 695 | useProxy.addActionListener(new ActionListener(){ |
| 37 | |
public void actionPerformed(ActionEvent e) { |
| 38 | 9 | proxy.setEnabled(useProxy.isSelected()); |
| 39 | 9 | port.setEnabled(useProxy.isSelected()); |
| 40 | 9 | } |
| 41 | |
}); |
| 42 | 695 | } |
| 43 | |
|
| 44 | |
public void load() { |
| 45 | 695 | useProxy.setSelected(PrefsModel.getInstance().isShowProxySettings()); |
| 46 | 695 | proxy.setText(PrefsModel.getInstance().getProxyServer()); |
| 47 | 695 | port.setText(PrefsModel.getInstance().getPort() + ""); |
| 48 | |
|
| 49 | 695 | proxy.setEnabled(useProxy.isSelected()); |
| 50 | 695 | port.setEnabled(useProxy.isSelected()); |
| 51 | 695 | } |
| 52 | |
|
| 53 | |
public boolean save() { |
| 54 | 43 | PrefsModel.getInstance().setShowProxySettings(useProxy.isSelected()); |
| 55 | 43 | if (useProxy.isSelected()){ |
| 56 | 1 | PrefsModel.getInstance().setProxyServer(proxy.getText()); |
| 57 | |
try { |
| 58 | 1 | PrefsModel.getInstance().setPort(Integer.parseInt(port.getText().replaceAll("\\D", ""))); |
| 59 | |
} |
| 60 | 0 | catch (NumberFormatException nfe){ |
| 61 | 0 | Logger.getLogger(this.getClass().getName()).warning("Incorrect port number; setting to 80"); |
| 62 | 1 | } |
| 63 | |
} |
| 64 | |
else { |
| 65 | 42 | PrefsModel.getInstance().setProxyServer(""); |
| 66 | 42 | PrefsModel.getInstance().setPort(0); |
| 67 | |
} |
| 68 | |
|
| 69 | 43 | return false; |
| 70 | |
} |
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public JPanel getPreferencesPanel() { |
| 74 | 695 | JPanel p = new JPanel(); |
| 75 | 695 | p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
| 76 | |
|
| 77 | 695 | JPanel useProxyPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 78 | 695 | JPanel proxyPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 79 | |
|
| 80 | 695 | proxy.setPreferredSize(InternalFormatter.getComponentSize(proxy, 200)); |
| 81 | 695 | port.setPreferredSize(InternalFormatter.getComponentSize(port, 50)); |
| 82 | |
|
| 83 | 695 | useProxyPanel.add(useProxy); |
| 84 | 695 | proxyPanel.add(new JLabel(TextFormatter.getTranslation(BuddiKeys.PROXY_SERVER))); |
| 85 | 695 | proxyPanel.add(proxy); |
| 86 | 695 | proxyPanel.add(new JLabel(":")); |
| 87 | 695 | proxyPanel.add(port); |
| 88 | |
|
| 89 | 695 | p.add(useProxyPanel); |
| 90 | 695 | p.add(proxyPanel); |
| 91 | |
|
| 92 | 695 | return p; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public String getName() { |
| 96 | 695 | return BuddiKeys.NETWORK.toString(); |
| 97 | |
} |
| 98 | |
} |