Gridarta Editor
MapValidatorPreferences.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.util.HashMap;
25 import java.util.Map;
26 import javax.swing.AbstractButton;
27 import javax.swing.Box;
28 import javax.swing.JCheckBox;
29 import javax.swing.JComponent;
30 import javax.swing.JPanel;
31 import javax.swing.border.Border;
32 import javax.swing.border.CompoundBorder;
33 import javax.swing.border.TitledBorder;
39 import net.sf.japi.swing.action.ActionBuilder;
40 import net.sf.japi.swing.action.ActionBuilderFactory;
41 import net.sf.japi.swing.prefs.AbstractPrefs;
42 import org.jetbrains.annotations.NotNull;
43 
49 public class MapValidatorPreferences extends AbstractPrefs {
50 
54  private static final long serialVersionUID = 1L;
55 
59  @NotNull
60  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
61 
65  private final boolean autoDefault;
66 
70  @NotNull
72 
76  @NotNull
77  private final Map<Validator<?, ?, ?>, JCheckBox> checkBoxes = new HashMap<>();
78 
82  @NotNull
83  private final AbstractButton autoValidate = new JCheckBox(ActionBuilderUtils.getString(ACTION_BUILDER, "autoValidate.text"), AutoValidator.isEnabled());
84 
91  public MapValidatorPreferences(@NotNull final DelegatingMapValidator<?, ?, ?> validators, final boolean autoDefault) {
92  this.autoDefault = autoDefault;
93  this.validators = validators;
94  setListLabelText(ActionBuilderUtils.getString(ACTION_BUILDER, "prefsMapValidator.title"));
95  setListLabelIcon(ACTION_BUILDER.getIcon("prefsMapValidator.icon"));
96 
97  add(createValidationPanel());
98  add(createValidatorsPanel());
99  add(Box.createVerticalGlue());
100  }
101 
107  private static Border createTitledBorder(final String titleKey) {
108  return new CompoundBorder(new TitledBorder(ActionBuilderUtils.getString(ACTION_BUILDER, titleKey)), GUIConstants.DIALOG_BORDER);
109  }
110 
111  @Override
112  public void apply() {
113  for (final Map.Entry<Validator<?, ?, ?>, JCheckBox> entry : checkBoxes.entrySet()) {
114  entry.getKey().setEnabled(entry.getValue().isSelected());
115  }
116  AutoValidator.setEnabled(autoValidate.isSelected());
117  }
118 
119  @Override
120  public void revert() {
121  for (final Map.Entry<Validator<?, ?, ?>, JCheckBox> entry : checkBoxes.entrySet()) {
122  entry.getValue().setSelected(entry.getKey().isEnabled());
123  }
124  autoValidate.setSelected(AutoValidator.isEnabled());
125  }
126 
127  @Override
128  public void defaults() {
129  for (final Map.Entry<Validator<?, ?, ?>, JCheckBox> entry : checkBoxes.entrySet()) {
130  entry.getValue().setSelected(entry.getKey().isDefaultEnabled());
131  }
132  autoValidate.setSelected(autoDefault);
133  }
134 
135  @Override
136  public boolean isChanged() {
137  for (final Map.Entry<Validator<?, ?, ?>, JCheckBox> entry : checkBoxes.entrySet()) {
138  if (entry.getValue().isSelected() != entry.getKey().isEnabled()) {
139  return true;
140  }
141  }
142  return autoValidate.isSelected() != AutoValidator.isEnabled();
143  }
144 
149  @NotNull
150  private Component createValidationPanel() {
151  final JComponent panel = new JPanel(new GridBagLayout());
152  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
153  panel.setBorder(createTitledBorder("optionsValidation"));
154  preferencesHelper.addComponent(autoValidate);
155  return panel;
156  }
157 
162  @NotNull
163  private Component createValidatorsPanel() {
164  final JComponent panel = new JPanel(new GridBagLayout());
165  final PreferencesHelper preferencesHelper = new PreferencesHelper(panel);
166  panel.setBorder(createTitledBorder("optionsValidators"));
167  for (final Validator<?, ?, ?> validator : validators) {
168  final JCheckBox checkBox = new JCheckBox(ActionBuilderUtils.getString(ACTION_BUILDER, validator.getKey() + ".title"), validator.isEnabled());
169  checkBoxes.put(validator, checkBox);
170  preferencesHelper.addComponent(checkBox);
171  }
172  return panel;
173  }
174 
175 }
static final long serialVersionUID
The serial version UID.
final AbstractButton autoValidate
Check box to set whether map files should be really checked.
Graphical User Interface of Gridarta.
Component createValidationPanel()
Creates the sub-panel with the generic validation settings.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
This package contains the framework for validating maps.
Component createValidatorsPanel()
Creates the sub-panel with the validators information.
static Border createTitledBorder(final String titleKey)
Creates a titled border.
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.
final boolean autoDefault
Whether the auto-validator is enabled by default.
static boolean isEnabled()
Return whether the auto-validator is enabled.
Super-interface for validators.
Definition: Validator.java:31
MapValidatorPreferences(@NotNull final DelegatingMapValidator<?, ?, ?> validators, final boolean autoDefault)
Creates a new instance.
static void setEnabled(final boolean enabled)
Set whether the auto-validator is enabled.
A Map Validator that delegates to other MapValidators.
Utility class for ActionBuilder related functions.
Implements the auto-validator for map validation.
Preferences Module for map validator preferences.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
final DelegatingMapValidator<?, ?, ?> validators
The delegating validator that contains all the validators.
final Map< Validator<?, ?, ?>, JCheckBox > checkBoxes
Checkboxes to enabled the individual validator checks.
Defines common UI constants used in different dialogs.