Gridarta Editor
AbstractValidator.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.model.validation;
21 
26 import net.sf.japi.swing.action.ActionBuilder;
27 import net.sf.japi.swing.action.ActionBuilderFactory;
28 import org.jetbrains.annotations.NotNull;
29 
37 public abstract class AbstractValidator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Validator<G, A, R> {
38 
42  @NotNull
43  private static final String VALIDATOR_PREFIX = "Validator.";
44 
48  @NotNull
49  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
50 
54  @NotNull
56 
60  @NotNull
61  private final String key;
62 
66  private boolean enabled;
67 
71  private final boolean defaultEnabled;
72 
80  protected AbstractValidator(@NotNull final ValidatorPreferences validatorPreferences) throws IllegalArgumentException {
81  this.validatorPreferences = validatorPreferences;
82  final String name = getClass().getSimpleName();
83  if (!name.endsWith("Checker")) {
84  throw new IllegalArgumentException("Class name must end with \"Checker\"");
85  }
86  key = VALIDATOR_PREFIX + name.substring(0, name.lastIndexOf("Checker"));
87  final String defaultEnabledString = ActionBuilderUtils.getString(ACTION_BUILDER, key + ".default");
88  defaultEnabled = Boolean.parseBoolean(defaultEnabledString);
89  enabled = validatorPreferences.loadEnabled(key, defaultEnabled);
90  }
91 
97  protected AbstractValidator(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final String key) {
98  this.validatorPreferences = validatorPreferences;
99  this.key = key;
100  final String defaultEnabledString = ActionBuilderUtils.getString(ACTION_BUILDER, key + ".default");
101  defaultEnabled = Boolean.parseBoolean(defaultEnabledString);
102  enabled = validatorPreferences.loadEnabled(key, defaultEnabled);
103  }
104 
105  @NotNull
106  @Override
107  public String getKey() {
108  return key;
109  }
110 
111  @Override
112  public void setEnabled(final boolean enabled) {
113  if (this.enabled == enabled) {
114  return;
115  }
116 
117  this.enabled = enabled;
118  validatorPreferences.saveEnabled(key, enabled);
119  }
120 
121  @Override
122  public boolean isEnabled() {
123  return enabled;
124  }
125 
126  @Override
127  public boolean isDefaultEnabled() {
128  return defaultEnabled;
129  }
130 
131 }
final boolean defaultEnabled
Whether this MapValidator is enabled by default.
This is the base class for validators.
AbstractValidator(@NotNull final ValidatorPreferences validatorPreferences)
Create an AbstractMapValidator.
boolean enabled
Whether this MapValidator is enabled.
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.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void saveEnabled(@NotNull String key, boolean enabled)
Saves the "enabled" attribute.
boolean loadEnabled(@NotNull String key, boolean defaultEnabled)
Loads the stored "enabled" attribute.
static final String VALIDATOR_PREFIX
The prefix for preference keys.
GameObjects are the objects based on Archetypes found on maps.
Super-interface for validators.
Definition: Validator.java:31
Utility class for ActionBuilder related functions.
final ValidatorPreferences validatorPreferences
The ValidatorPreferences to use.
AbstractValidator(@NotNull final ValidatorPreferences validatorPreferences, @NotNull final String key)
Create an AbstractMapValidator.
static final ActionBuilder ACTION_BUILDER
Action Builder.