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-2023 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.net.Proxy.Type;
29 import java.util.EnumSet;
30 import java.util.prefs.Preferences;
31 import javax.swing.Box;
32 import javax.swing.JComboBox;
33 import javax.swing.JComponent;
34 import javax.swing.JPanel;
35 import javax.swing.JSpinner;
36 import javax.swing.JTextField;
37 import javax.swing.SpinnerNumberModel;
38 import javax.swing.border.Border;
39 import javax.swing.border.CompoundBorder;
40 import javax.swing.border.TitledBorder;
41 import javax.swing.text.JTextComponent;
42 import net.sf.gridarta.MainControl;
45 import net.sf.japi.swing.action.ActionBuilder;
46 import net.sf.japi.swing.action.ActionBuilderFactory;
47 import net.sf.japi.swing.prefs.AbstractPrefs;
48 import org.jetbrains.annotations.NotNull;
49 
55 public class NetPreferences extends AbstractPrefs implements ItemListener {
56 
60  private static final long serialVersionUID = 1L;
61 
65  @NotNull
66  private static final String NET_PREFERENCES_KEY_TYPE = "proxy.type";
67 
71  @NotNull
72  private static final String NET_PREFERENCES_KEY_HOST = "proxy.host";
73 
77  @NotNull
78  private static final String NET_PREFERENCES_KEY_PORT = "proxy.port";
79 
83  @NotNull
84  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
85 
89  @NotNull
90  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
91 
95  @NotNull
96  private final JComboBox<Type> proxyType = createProxyType();
97 
101  @NotNull
102  private final JTextComponent proxyHost = new JTextField(PREFERENCES.get(NET_PREFERENCES_KEY_HOST, ""));
103 
107  @NotNull
108  private final JSpinner proxyPort = new JSpinner(new SpinnerNumberModel(PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128), 1, 65535, 1));
109 
113  public NetPreferences() {
114  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsNet.title"));
115  setListLabelIcon(ACTION_BUILDER.getIcon("prefsNet.icon"));
116  proxyType.addItemListener(this);
117 
118  add(createNetPanel());
119  add(Box.createVerticalGlue());
120  revert();
121  }
122 
128  @NotNull
129  private static Border createTitledBorder(@NotNull final String titleKey) {
130  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
131  }
132 
133  @Override
134  public boolean isChanged() {
135  return proxyType.getSelectedItem() != 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();
136 
137  }
138 
139  @Override
140  public void defaults() {
144  revert();
145  }
146 
147  @Override
148  public final void revert() {
149  //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
150  final Type keyType = Type.valueOf(PREFERENCES.get(NET_PREFERENCES_KEY_TYPE, "DIRECT"));
151  proxyType.setSelectedIndex(keyType.ordinal());
152  final boolean enableProxy = keyType != Type.DIRECT;
153  proxyHost.setEnabled(enableProxy);
154  proxyPort.setEnabled(enableProxy);
156  proxyPort.setValue(PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128));
157  }
158 
159  @Override
160  public void apply() {
161  //JComboBox does not use type parameters
162  @SuppressWarnings("unchecked") final Enum<Type> typeEnum = (Enum<Type>) proxyType.getSelectedItem();
163  PREFERENCES.put(NET_PREFERENCES_KEY_TYPE, typeEnum.name());
165  PREFERENCES.putInt(NET_PREFERENCES_KEY_PORT, (Integer) proxyPort.getValue());
166  }
167 
172  @NotNull
173  private Component createNetPanel() {
174  final JComponent panel = new JPanel(new GridBagLayout());
175  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
176  panel.setBorder(createTitledBorder("optionsNetProxy"));
177  preferencesHelper.addComponent(proxyType);
178  preferencesHelper.addComponent(proxyHost);
179  preferencesHelper.addComponent(proxyPort);
180  return panel;
181  }
182 
187  @NotNull
188  private static JComboBox<Type> createProxyType() {
189 
190  return new JComboBox<>(EnumSet.allOf(Type.class).toArray(new Type[0]));
191  }
192 
197  @NotNull
198  public static Proxy getProxy() {
199  final Type proxyType = Type.valueOf(PREFERENCES.get(NET_PREFERENCES_KEY_TYPE, "DIRECT"));
200  return proxyType == Type.DIRECT ? Proxy.NO_PROXY : new Proxy(proxyType, new InetSocketAddress(PREFERENCES.get(NET_PREFERENCES_KEY_HOST, "proxy"), PREFERENCES.getInt(NET_PREFERENCES_KEY_PORT, 3128)));
201  }
202 
203  @Override
204  public void itemStateChanged(@NotNull final ItemEvent e) {
205  final Type proxyTypeSelection = (Type) proxyType.getSelectedItem();
206  final boolean enableProxy = proxyTypeSelection != Type.DIRECT;
207  proxyHost.setEnabled(enableProxy);
208  proxyPort.setEnabled(enableProxy);
209  }
210 
211 }
net.sf.gridarta.gui.dialog.prefs.PreferencesHelper
Helper class for preference panes.
Definition: PreferencesHelper.java:31
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.utils.GUIConstants
Defines common UI constants used in different dialogs.
Definition: GUIConstants.java:33
net.sf.gridarta.gui.dialog.prefs.NetPreferences.proxyType
final JComboBox< Type > proxyType
JComboBox for selecting the proxy type.
Definition: NetPreferences.java:96
net.sf
net.sf.gridarta.gui.dialog.prefs.NetPreferences.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: NetPreferences.java:84
net.sf.gridarta.gui.dialog.prefs.NetPreferences.revert
final void revert()
Definition: NetPreferences.java:148
net.sf.gridarta.gui.dialog.prefs.NetPreferences.apply
void apply()
Definition: NetPreferences.java:160
net.sf.gridarta.gui.dialog.prefs.NetPreferences.NET_PREFERENCES_KEY_HOST
static final String NET_PREFERENCES_KEY_HOST
The preferences key for the type.
Definition: NetPreferences.java:72
net.sf.gridarta.gui.dialog.prefs.NetPreferences.itemStateChanged
void itemStateChanged(@NotNull final ItemEvent e)
Definition: NetPreferences.java:204
net.sf.gridarta.gui.dialog.prefs.NetPreferences
Preferences Module for networking preferences.
Definition: NetPreferences.java:55
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net
net.sf.gridarta.gui.dialog.prefs.NetPreferences.NetPreferences
NetPreferences()
Creates a new instance.
Definition: NetPreferences.java:113
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.gui.dialog.prefs.NetPreferences.NET_PREFERENCES_KEY_TYPE
static final String NET_PREFERENCES_KEY_TYPE
The preferences key for the type.
Definition: NetPreferences.java:66
net.sf.gridarta.gui.dialog.prefs.NetPreferences.createNetPanel
Component createNetPanel()
Creates the sub-panel with the external applications.
Definition: NetPreferences.java:173
net.sf.gridarta.gui.dialog.prefs.NetPreferences.proxyHost
final JTextComponent proxyHost
TextField for server executable.
Definition: NetPreferences.java:102
net.sf.gridarta.gui.dialog.prefs.NetPreferences.defaults
void defaults()
Definition: NetPreferences.java:140
net.sf.gridarta.gui.dialog.prefs.NetPreferences.createProxyType
static JComboBox< Type > createProxyType()
Creates the JComboBox for selecting the proxy type.
Definition: NetPreferences.java:188
net.sf.gridarta.gui.dialog.prefs.NetPreferences.isChanged
boolean isChanged()
Definition: NetPreferences.java:134
net.sf.gridarta.utils.ActionBuilderUtils
Utility class for ActionBuilder related functions.
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.dialog.prefs.NetPreferences.proxyPort
final JSpinner proxyPort
TextField for client executable.
Definition: NetPreferences.java:108
net.sf.gridarta.gui.dialog.prefs.NetPreferences.PREFERENCES
static final Preferences PREFERENCES
Preferences.
Definition: NetPreferences.java:90
net.sf.gridarta.gui.dialog.prefs.NetPreferences.getProxy
static Proxy getProxy()
Returns the currently preferred proxy.
Definition: NetPreferences.java:198
net.sf.gridarta.gui.dialog.prefs.NetPreferences.createTitledBorder
static Border createTitledBorder(@NotNull final String titleKey)
Creates a titled border.
Definition: NetPreferences.java:129
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.prefs.NetPreferences.NET_PREFERENCES_KEY_PORT
static final String NET_PREFERENCES_KEY_PORT
The preferences key for the type.
Definition: NetPreferences.java:78
net.sf.gridarta.gui.dialog.prefs.NetPreferences.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: NetPreferences.java:60
net.sf.gridarta.gui.utils.GUIConstants.DIALOG_BORDER
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
Definition: GUIConstants.java:38
net.sf.gridarta.gui.dialog.prefs.PreferencesHelper.addComponent
void addComponent(@NotNull final Component component)
Adds a component to the container.
Definition: PreferencesHelper.java:61
net.sf.gridarta.MainControl
Interface used as preferences location.
Definition: MainControl.java:27