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