Gridarta Editor
NetPreferences.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.prefs;
21 
22 import java.awt.Component;
23 import java.awt.GridBagLayout;
24 import java.awt.event.ItemEvent;
25 import java.awt.event.ItemListener;
26 import java.net.InetSocketAddress;
27 import java.net.Proxy;
28 import java.util.EnumSet;
29 import java.util.prefs.Preferences;
30 import javax.swing.Box;
31 import javax.swing.JComboBox;
32 import javax.swing.JComponent;
33 import javax.swing.JPanel;
34 import javax.swing.JSpinner;
35 import javax.swing.JTextField;
36 import javax.swing.SpinnerNumberModel;
37 import javax.swing.border.Border;
38 import javax.swing.border.CompoundBorder;
39 import javax.swing.border.TitledBorder;
40 import javax.swing.text.JTextComponent;
41 import net.sf.gridarta.MainControl;
44 import net.sf.japi.swing.action.ActionBuilder;
45 import net.sf.japi.swing.action.ActionBuilderFactory;
46 import net.sf.japi.swing.prefs.AbstractPrefs;
47 import org.jetbrains.annotations.NotNull;
48 
54 public class NetPreferences extends AbstractPrefs implements ItemListener {
55 
59  private static final long serialVersionUID = 1L;
60 
64  @NotNull
65  private static final String NET_PREFERENCES_KEY_TYPE = "proxy.type";
66 
70  @NotNull
71  private static final String NET_PREFERENCES_KEY_HOST = "proxy.host";
72 
76  @NotNull
77  private static final String NET_PREFERENCES_KEY_PORT = "proxy.port";
78 
82  @NotNull
83  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
84 
88  @NotNull
89  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
90 
94  @NotNull
95  private final JComboBox<Proxy.Type> proxyType = createProxyType();
96 
100  @NotNull
101  private final JTextComponent proxyHost = new JTextField(PREFERENCES.get(NET_PREFERENCES_KEY_HOST, ""));
102 
106  @NotNull
107  private final JSpinner proxyPort = new JSpinner(new SpinnerNumberModel(PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128), 1, 65535, 1));
108 
112  public NetPreferences() {
113  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsNet.title"));
114  setListLabelIcon(ACTION_BUILDER.getIcon("prefsNet.icon"));
115  proxyType.addItemListener(this);
116 
117  add(createNetPanel());
118  add(Box.createVerticalGlue());
119  revert();
120  }
121 
127  @NotNull
128  private static Border createTitledBorder(@NotNull final String titleKey) {
129  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
130  }
131 
132  @Override
133  public boolean isChanged() {
134  return !(proxyType.getSelectedItem() == Proxy.Type.valueOf(PREFERENCES.get(NET_PREFERENCES_KEY_TYPE, "DIRECT")) && PREFERENCES.get(NET_PREFERENCES_KEY_HOST, "").equals(proxyHost.getText()) && PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128) == (Integer) proxyPort.getValue());
135 
136  }
137 
138  @Override
139  public void defaults() {
140  PREFERENCES.remove(NET_PREFERENCES_KEY_TYPE);
141  PREFERENCES.remove(NET_PREFERENCES_KEY_HOST);
142  PREFERENCES.remove(NET_PREFERENCES_KEY_PORT);
143  revert();
144  }
145 
146  @Override
147  public final void revert() {
148  //Cannot weaken tp Enum<Proxy.Type> because some javac versions (1.6.0_01, 1.6.0_16, 1.7.0-ea) report incomparable types
149  final Proxy.Type keyType = Proxy.Type.valueOf(PREFERENCES.get(NET_PREFERENCES_KEY_TYPE, "DIRECT"));
150  proxyType.setSelectedIndex(keyType.ordinal());
151  final boolean enableProxy = keyType != Proxy.Type.DIRECT;
152  proxyHost.setEnabled(enableProxy);
153  proxyPort.setEnabled(enableProxy);
154  proxyHost.setText(PREFERENCES.get(NET_PREFERENCES_KEY_HOST, ""));
155  proxyPort.setValue(PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128));
156  }
157 
158  @Override
159  public void apply() {
160  //JComboBox does not use type parameters
161  @SuppressWarnings("unchecked") final Enum<Proxy.Type> typeEnum = (Enum<Proxy.Type>) proxyType.getSelectedItem();
162  PREFERENCES.put(NET_PREFERENCES_KEY_TYPE, typeEnum.name());
163  PREFERENCES.put(NET_PREFERENCES_KEY_HOST, proxyHost.getText());
164  PREFERENCES.putInt(NET_PREFERENCES_KEY_PORT, (Integer) proxyPort.getValue());
165  }
166 
171  @NotNull
172  private Component createNetPanel() {
173  final JComponent panel = new JPanel(new GridBagLayout());
174  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
175  panel.setBorder(createTitledBorder("optionsNetProxy"));
176  preferencesHelper.addComponent(proxyType);
177  preferencesHelper.addComponent(proxyHost);
178  preferencesHelper.addComponent(proxyPort);
179  return panel;
180  }
181 
186  @NotNull
187  private static JComboBox<Proxy.Type> createProxyType() {
188 
189  return new JComboBox<>(EnumSet.allOf(Proxy.Type.class).toArray(new Proxy.Type[0]));
190  }
191 
196  @NotNull
197  public static Proxy getProxy() {
198  final Proxy.Type proxyType = Proxy.Type.valueOf(PREFERENCES.get(NET_PREFERENCES_KEY_TYPE, "DIRECT"));
199  if (proxyType == Proxy.Type.DIRECT) {
200  return Proxy.NO_PROXY;
201  }
202  return new Proxy(proxyType, new InetSocketAddress(PREFERENCES.get(NET_PREFERENCES_KEY_HOST, "proxy"), PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128)));
203  }
204 
205  @Override
206  public void itemStateChanged(@NotNull final ItemEvent e) {
207  final Proxy.Type proxyTypeSelection = (Proxy.Type) proxyType.getSelectedItem();
208  final boolean enableProxy = proxyTypeSelection != Proxy.Type.DIRECT;
209  proxyHost.setEnabled(enableProxy);
210  proxyPort.setEnabled(enableProxy);
211  }
212 
213 }
static final long serialVersionUID
The serial version UID.
static Proxy getProxy()
Returns the currently preferred proxy.
Graphical User Interface of Gridarta.
static final ActionBuilder ACTION_BUILDER
Action Builder.
static JComboBox< Proxy.Type > createProxyType()
Creates the JComboBox for selecting the proxy type.
static final String NET_PREFERENCES_KEY_HOST
The preferences key for the type.
void itemStateChanged(@NotNull final ItemEvent e)
void addComponent(@NotNull final Component component)
Adds a component to the container.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
Interface used as preferences location.
Preferences Module for networking preferences.
Utility class for ActionBuilder related functions.
static final Preferences PREFERENCES
Preferences.
Component createNetPanel()
Creates the sub-panel with the external applications.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static final String NET_PREFERENCES_KEY_PORT
The preferences key for the type.
static final String NET_PREFERENCES_KEY_TYPE
The preferences key for the type.
static Border createTitledBorder(@NotNull final String titleKey)
Creates a titled border.
final JComboBox< Proxy.Type > proxyType
JComboBox for selecting the proxy type.
final JTextComponent proxyHost
TextField for server executable.
final JSpinner proxyPort
TextField for client executable.
Defines common UI constants used in different dialogs.