Gridarta Editor
GUIPreferences.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.Container;
24 import java.awt.GridBagLayout;
25 import java.util.Arrays;
26 import java.util.Comparator;
27 import java.util.Locale;
28 import java.util.prefs.Preferences;
29 import javax.swing.AbstractButton;
30 import javax.swing.Box;
31 import javax.swing.JCheckBox;
32 import javax.swing.JComboBox;
33 import javax.swing.JComponent;
34 import javax.swing.JPanel;
35 import javax.swing.border.Border;
36 import javax.swing.border.CompoundBorder;
37 import javax.swing.border.TitledBorder;
38 import net.sf.gridarta.MainControl;
43 import net.sf.japi.swing.action.ActionBuilder;
44 import net.sf.japi.swing.action.ActionBuilderFactory;
45 import net.sf.japi.swing.misc.LocaleListCellRenderer;
46 import net.sf.japi.swing.prefs.AbstractPrefs;
47 import net.sf.japi.util.LocaleComparator;
48 import org.jetbrains.annotations.NotNull;
49 
55 public class GUIPreferences extends AbstractPrefs {
56 
60  public static final String PREFERENCES_LANGUAGE = "language";
61 
65  private static final long serialVersionUID = 1L;
66 
70  @NotNull
71  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
72 
76  @NotNull
77  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
78 
82  @NotNull
83  private static final String[] EMPTY_STRING_ARRAY = new String[0];
84 
88  @NotNull
90 
94  @NotNull
95  private JComboBox<Locale> localeBox;
96 
100  @NotNull
101  private AbstractButton showMainToolbar;
102 
106  @NotNull
107  private Locale[] locales;
108 
112  @NotNull
113  private final Comparator<Locale> comp = new LocaleComparator();
114 
119  public GUIPreferences(@NotNull final EditorSettings editorSettings) {
120  this.editorSettings = editorSettings;
121 
122  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsGUI.title"));
123  setListLabelIcon(ACTION_BUILDER.getIcon("prefsGUI.icon"));
124 
125  add(createGlobalPanel());
126  add(createLayoutPanel());
127  add(Box.createVerticalGlue());
128  }
129 
135  @NotNull
136  private static Border createTitledBorder(@NotNull final String titleKey) {
137  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
138  }
139 
140  @Override
141  public void apply() {
142  final Locale loc = (Locale) localeBox.getSelectedItem();
143  if (loc != null) {
144  PREFERENCES.put(PREFERENCES_LANGUAGE, loc.getLanguage());
145  } else {
146  PREFERENCES.remove(PREFERENCES_LANGUAGE);
147  }
148  editorSettings.setShowMainToolbar(showMainToolbar.isSelected());
149  }
150 
151  @Override
152  public void revert() {
153  final String current = PREFERENCES.get(PREFERENCES_LANGUAGE, null);
154  localeBox.setSelectedIndex(Arrays.binarySearch(locales, current != null ? new Locale(current) : null, comp));
155  showMainToolbar.setSelected(editorSettings.isShowMainToolbar());
156  }
157 
158  @Override
159  public void defaults() {
160  localeBox.setSelectedIndex(Arrays.binarySearch(locales, null, comp));
161  showMainToolbar.setSelected(EditorSettings.SHOW_MAIN_TOOLBAR_DEFAULT);
162  }
163 
164  @Override
165  public boolean isChanged() {
166  final Locale loc = (Locale) localeBox.getSelectedItem();
167  final String currentName = PREFERENCES.get(PREFERENCES_LANGUAGE, null);
168  final Locale current = currentName != null ? new Locale(currentName) : null;
169  return !((loc == null ? current == null : loc.equals(current)) && showMainToolbar.isSelected() == editorSettings.isShowMainToolbar());
170  }
171 
176  @NotNull
177  private Component buildLocaleBox() {
178  final Container lineLayout = Box.createHorizontalBox();
179 
180  final CharSequence availableLocales = ACTION_BUILDER.getString("availableLocales");
181  final String[] locNames = availableLocales != null ? StringUtils.PATTERN_WHITESPACE.split(availableLocales, 0) : EMPTY_STRING_ARRAY;
182  locales = new Locale[locNames.length + 1];
183  // locales[0] is intentionally null. It will be displayed as default and always get sorted to the top.
184  for (int i = 0; i < locNames.length; i++) {
185  locales[i + 1] = new Locale(locNames[i]);
186  }
187  Arrays.sort(locales, comp);
188 
189  lineLayout.add(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsLanguage")); // create label
190 
191  localeBox = new JComboBox<>(locales); // set "content"
192  localeBox.setRenderer(new LocaleListCellRenderer());
193  //localeBox.setPreferredSize(new Dimension(150, 25));
194  final String current = PREFERENCES.get(PREFERENCES_LANGUAGE, null);
195  localeBox.setSelectedIndex(Arrays.binarySearch(locales, current != null ? new Locale(current) : null, comp));
196 
197  lineLayout.add(localeBox);
198  return lineLayout;
199  }
200 
205  @NotNull
206  private Component createGlobalPanel() {
207  final JComponent panel = new JPanel(new GridBagLayout());
208  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
209  panel.setBorder(createTitledBorder("optionsGlobal"));
210 
211  preferencesHelper.addComponent(buildLocaleBox());
212 
213  return panel;
214  }
215 
220  @NotNull
221  private Component createLayoutPanel() {
222  final JComponent panel = new JPanel(new GridBagLayout());
223  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
224  panel.setBorder(createTitledBorder("optionsLayout"));
225 
226  showMainToolbar = new JCheckBox(ACTION_BUILDER.createAction(false, "optionsShowMainToolbar"));
227  showMainToolbar.setSelected(editorSettings.isShowMainToolbar());
228  preferencesHelper.addComponent(showMainToolbar);
229 
230  return panel;
231  }
232 
233 }
static final Preferences PREFERENCES
Preferences.
static final String PREFERENCES_LANGUAGE
Preferences key for language.
Utility class for string manipulation.
static Border createTitledBorder(@NotNull final String titleKey)
Creates a titled border.
Graphical User Interface of Gridarta.
AbstractButton showMainToolbar
Whether to show the main window&#39;s toolbar.
final EditorSettings editorSettings
The EditorSettings to use.
void addComponent(@NotNull final Component component)
Adds a component to the container.
static final long serialVersionUID
The serial version UID.
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.
boolean SHOW_MAIN_TOOLBAR_DEFAULT
Default value for whether the main window&#39;s toolbar is shown.
static final ActionBuilder ACTION_BUILDER
Action Builder.
Interface used as preferences location.
boolean isShowMainToolbar()
Returns whether the main toolbar should be shown.
GUIPreferences(@NotNull final EditorSettings editorSettings)
Creates a new instance.
static final Pattern PATTERN_WHITESPACE
Pattern to match whitespace excluding NL and CR.
Utility class for ActionBuilder related functions.
Component buildLocaleBox()
Constructs the combo box for the selection of locales.
final Comparator< Locale > comp
LocaleComparator.
Component createLayoutPanel()
Creates the sub-panel with the layout settings.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static final String [] EMPTY_STRING_ARRAY
An empty.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
JComboBox< Locale > localeBox
/** ComboBox for choosing the locale.
Settings that apply to the editor.
void setShowMainToolbar(boolean selected)
Sets whether the main toolbar should be shown.
Component createGlobalPanel()
Creates the sub-panel with the editor settings.
Defines common UI constants used in different dialogs.
Preferences Module for user interface preferences.