Gridarta Editor
ResPreferences.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.io.File;
27 import java.util.Arrays;
28 import javax.swing.Box;
29 import javax.swing.JComboBox;
30 import javax.swing.JComponent;
31 import javax.swing.JFileChooser;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.border.Border;
35 import javax.swing.border.CompoundBorder;
36 import javax.swing.border.TitledBorder;
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 net.sf.japi.util.Arrays2;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
51 
58 public class ResPreferences extends AbstractPrefs {
59 
63  private static final long serialVersionUID = 1L;
64 
68  @NotNull
69  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
70 
74  @NotNull
76 
80  @NotNull
82 
86  @NotNull
88 
92  @NotNull
94 
98  @NotNull
100 
104  @Nullable
106 
110  @NotNull
111  private JComboBox<ConfigSource> configSourceComboBox;
112 
116  @Nullable
117  private JComboBox<String> imageSetBox;
118 
122  @Nullable
123  private String[] imageSets;
124 
128  @NotNull
129  private final ItemListener itemListener = new ItemListener() {
130 
131  @Override
132  public void itemStateChanged(final ItemEvent e) {
133  if (e.getStateChange() == ItemEvent.SELECTED) {
134  final ConfigSource configSource = (ConfigSource) e.getItem();
135  archField.setEnabled(configSource.isArchDirectoryInputFieldEnabled());
136  }
137  }
138 
139  };
140 
147  public ResPreferences(@NotNull final ProjectSettings projectSettings, @NotNull final EditorSettings editorSettings, @NotNull final ConfigSourceFactory configSourceFactory) {
148  this.projectSettings = projectSettings;
149  this.editorSettings = editorSettings;
150  this.configSourceFactory = configSourceFactory;
151  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsRes.title"));
152  setListLabelIcon(ACTION_BUILDER.getIcon("prefsRes.icon"));
153 
154  add(createResourcePanel());
155  add(createGlobalPanel());
156  add(Box.createVerticalGlue());
157  }
158 
164  @NotNull
165  private static Border createTitledBorder(final String titleKey) {
166  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
167  }
168 
169  @Override
170  public void apply() {
171  projectSettings.setArchDirectory(archField.getFile());
172  projectSettings.setMapsDirectory(mapField.getFile());
173  if (editorSettings.hasMediaDirectory()) {
174  assert mediaField != null;
175  projectSettings.setMediaDirectory(mediaField.getFile());
176  }
177  projectSettings.setConfigSourceName(((ConfigSource) configSourceComboBox.getSelectedItem()).getName());
178 
179  if (editorSettings.hasImageSet()) {
180  assert imageSetBox != null;
181  final String imageSet = (String) imageSetBox.getSelectedItem();
182  projectSettings.setImageSet(imageSet == null || imageSet.equals("disabled") ? "none" : imageSet);
183  }
184 
185  ACTION_BUILDER.showOnetimeMessageDialog(this, JOptionPane.WARNING_MESSAGE, "optionsRestart");
186  }
187 
188  @Override
189  public void revert() {
190  archField.setFile(projectSettings.getArchDirectory());
191  mapField.setFile(projectSettings.getMapsDirectory());
192  if (editorSettings.hasMediaDirectory()) {
193  final File mediaDirectory = projectSettings.getMediaDirectory();
194  assert mediaField != null;
195  mediaField.setFile(mediaDirectory);
196  }
197  configSourceComboBox.setSelectedItem(configSourceFactory.getConfigSource(projectSettings.getConfigSourceName()));
198  if (editorSettings.hasImageSet()) {
199  final String imageSet = getCurrentImageSet();
200  assert imageSets != null;
201  final int index = Arrays2.linearEqualitySearch(imageSet, imageSets);
202  assert imageSetBox != null;
203  imageSetBox.setSelectedIndex(index);
204  }
205  }
206 
207  @Override
208  public void defaults() {
209  archField.setFile(editorSettings.getArchDirectoryDefault());
210  mapField.setFile(editorSettings.getMapsDirectoryDefault());
211  if (editorSettings.hasMediaDirectory()) {
212  final File mediaDirectory = editorSettings.getMediaDirectoryDefault();
213  assert mediaField != null;
214  mediaField.setFile(mediaDirectory);
215  }
216  configSourceComboBox.setSelectedItem(configSourceFactory.getDefaultConfigSource());
217  if (editorSettings.hasImageSet()) {
218  assert imageSets != null;
219  final int index = Arrays2.linearEqualitySearch("disabled", imageSets);
220  assert imageSetBox != null;
221  imageSetBox.setSelectedIndex(index);
222  }
223  }
224 
225  @Override
226  public boolean isChanged() {
227  final String imageSet;
228  if (editorSettings.hasImageSet()) {
229  assert imageSetBox != null;
230  imageSet = convertImageSet((String) imageSetBox.getSelectedItem());
231  } else {
232  imageSet = convertImageSet(null);
233  }
234  if (!archField.getFile().equals(projectSettings.getArchDirectory()) || !mapField.getFile().equals(projectSettings.getMapsDirectory())) {
235  return true;
236  }
237 
238  if (editorSettings.hasMediaDirectory()) {
239  assert mediaField != null;
240  if (!mediaField.getFile().equals(projectSettings.getMediaDirectory())) {
241  return true;
242  }
243  }
244 
245  return !(imageSet.equals(getCurrentImageSet()) && ((ConfigSource) configSourceComboBox.getSelectedItem()).getName().equals(projectSettings.getConfigSourceName()));
246  }
247 
252  private Component createResourcePanel() {
253  final JComponent panel = new JPanel(new GridBagLayout());
254  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
255  panel.setBorder(createTitledBorder("optionsResPaths"));
256  archField = new JFileField(this, "optionsResArch", null, projectSettings.getArchDirectory(), JFileChooser.DIRECTORIES_ONLY);
257  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsResArch"));
258  preferencesHelper.addComponent(archField);
259  mapField = new JFileField(this, "optionsResMaps", null, projectSettings.getMapsDirectory(), JFileChooser.DIRECTORIES_ONLY);
260  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsResMaps"));
261  preferencesHelper.addComponent(mapField);
262  if (editorSettings.hasMediaDirectory()) {
263  mediaField = new JFileField(this, "optionsResMedia", null, projectSettings.getMediaDirectory(), JFileChooser.DIRECTORIES_ONLY);
264  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsResMedia"));
265  assert mediaField != null;
266  preferencesHelper.addComponent(mediaField);
267  }
268  return panel;
269  }
270 
275  @NotNull
276  private Component createGlobalPanel() {
277  final JComponent panel = new JPanel(new GridBagLayout());
278  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
279  panel.setBorder(createTitledBorder("optionsGlobal"));
280 
281  configSourceComboBox = new JComboBox<>(configSourceFactory.getConfigSources());
282  final ConfigSource configSource = configSourceFactory.getConfigSource(projectSettings.getConfigSourceName());
283  configSourceComboBox.setSelectedItem(configSource);
284  configSourceComboBox.addItemListener(itemListener);
285  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsConfigSource.text"));
286  preferencesHelper.addComponent(configSourceComboBox);
287  archField.setEnabled(configSource.isArchDirectoryInputFieldEnabled());
288 
289  if (editorSettings.hasImageSet()) {
290  preferencesHelper.addComponent(ActionBuilderUtils.newLabel(ACTION_BUILDER, "optionsImageSet"));
291  preferencesHelper.addComponent(buildImageSetBox());
292  }
293 
294  return panel;
295  }
296 
301  @NotNull
302  private Component buildImageSetBox() {
303  final String[] imageSetNames = StringUtils.PATTERN_WHITESPACE.split(ActionBuilderUtils.getString(ACTION_BUILDER, "availableImageSets"), 0);
304  Arrays.sort(imageSetNames);
305  imageSets = new String[imageSetNames.length + 1];
306  imageSets[0] = "disabled";
307  System.arraycopy(imageSetNames, 0, imageSets, 1, imageSetNames.length);
308 
309  imageSetBox = new JComboBox<>(imageSets); // set "content"
310  final String imageSet = getCurrentImageSet();
311  assert imageSets != null;
312  final int index = Arrays2.linearEqualitySearch(imageSet, imageSets);
313  assert imageSetBox != null;
314  imageSetBox.setSelectedIndex(index);
315 
316  assert imageSetBox != null;
317  return imageSetBox;
318  }
319 
324  @NotNull
325  private String getCurrentImageSet() {
326  return convertImageSet(projectSettings.getImageSet());
327  }
328 
334  @NotNull
335  private static String convertImageSet(@Nullable final String imageSet) {
336  return imageSet == null || imageSet.isEmpty() || imageSet.equals("none") ? "disabled" : imageSet;
337  }
338 
339 }
final ConfigSourceFactory configSourceFactory
The ConfigSourceFactory to use.
Component createResourcePanel()
Creates the sub-panel with the resource paths.
Utility class for string manipulation.
String getImageSet()
Returns the image set.
final EditorSettings editorSettings
The editor settings instance.
Graphical User Interface of Gridarta.
Settings that apply to a project.
static final long serialVersionUID
The serial version UID.
void setMediaDirectory(@NotNull File mediaDirectory)
Sets the media directory.
A component for selecting files.
Definition: JFileField.java:45
void setConfigSourceName(@NotNull String configSourceName)
Sets the name of the configuration source.
ResPreferences(@NotNull final ProjectSettings projectSettings, @NotNull final EditorSettings editorSettings, @NotNull final ConfigSourceFactory configSourceFactory)
Create a ResPreferences pane.
static String convertImageSet(@Nullable final String imageSet)
Returns a human readable name for a given image set.
void addComponent(@NotNull final Component component)
Adds a component to the container.
File getMediaDirectoryDefault()
Returns the default media directory.
ConfigSource getDefaultConfigSource()
Returns the default ConfigSource.
JFileField mediaField
TextField for media directory path.
File getMapsDirectoryDefault()
Returns the default maps directory.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
ConfigSource [] getConfigSources()
Returns all defined configuration sources.
Base package of all Gridarta classes.
static Border createTitledBorder(final String titleKey)
Creates a titled border.
Possible source locations for configuration files.
String [] imageSets
Contains all supported image sets.
boolean hasMediaDirectory()
Returns whether a media directory is used.
Component buildImageSetBox()
Constructs the combo box for the selection of image sets.
JFileField mapField
TextField for map directory path.
JComboBox< String > imageSetBox
ComboBox for choosing the image set.
boolean hasImageSet()
Returns whether an image set is used.
final ProjectSettings projectSettings
The project settings instance.
void setArchDirectory(@NotNull File archDirectory)
Sets the archetype directory.
void setMapsDirectory(@NotNull File mapsDirectory)
Sets the default maps directory.
JComboBox< ConfigSource > configSourceComboBox
ComboBox for choosing the configuration source.
String getConfigSourceName()
Returns the name of the configuration source.
void setFile(@NotNull final File file)
Sets the currently selected file.
final ItemListener itemListener
The ItemListener attached to configSourceComboBox.
boolean isArchDirectoryInputFieldEnabled()
Whether the "archetype directory" input field in the settings dialog should be enabled.
static final Pattern PATTERN_WHITESPACE
Pattern to match whitespace excluding NL and CR.
String getCurrentImageSet()
Returns the name of the currently selected image set.
Utility class for ActionBuilder related functions.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
ConfigSource getConfigSource(@NotNull String name)
Returns a ConfigSource by name.
File getArchDirectory()
Returns the archetype directory.
File getMapsDirectory()
Returns the default maps directory.
void setImageSet(@NotNull String imageSet)
Sets the image set.
static JLabel newLabel(@NotNull final ActionBuilder actionBuilder, @NotNull final String key)
Creates a new JLabel from a resource key.
File getMediaDirectory()
Returns the media directory.
File getArchDirectoryDefault()
Returns the default archetype directory.
static final ActionBuilder ACTION_BUILDER
Action Builder.
JFileField archField
TextField for arch directory path.
File getFile()
Returns the currently selected file.
Component createGlobalPanel()
Creates the sub-panel with the editor settings.
Settings that apply to the editor.
Preferences Module for resource preferences.
Defines common UI constants used in different dialogs.