Gridarta Editor
AutoValidator.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.autovalidator;
21 
22 import java.util.prefs.PreferenceChangeEvent;
23 import java.util.prefs.PreferenceChangeListener;
24 import java.util.prefs.Preferences;
25 import net.sf.gridarta.MainControl;
33 import org.jetbrains.annotations.NotNull;
34 
40 public class AutoValidator<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
41 
45  private static final String PREFERENCES_VALIDATOR_AUTO = "autoValidate";
46 
50  @NotNull
51  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
52 
56  @NotNull
58 
62  @NotNull
64 
68  private boolean wasEnabled;
69 
76  @NotNull
77  private final PreferenceChangeListener preferenceChangeListener = new PreferenceChangeListener() {
78 
79  @Override
80  public void preferenceChange(final PreferenceChangeEvent evt) {
81  final String key = evt.getKey();
82  if (key.equals(PREFERENCES_VALIDATOR_AUTO)) {
83  if (wasEnabled != isEnabled()) {
84  wasEnabled = !wasEnabled;
86  }
87  } else if (key.startsWith("Validator.")) {
89  }
90  }
91 
92  };
93 
97  @NotNull
99 
100  @Override
101  public void mapModelChanged(@NotNull final MapModel<G, A, R> mapModel) {
102  if (wasEnabled) {
103  validators.validateAll(mapModel);
104  }
105  }
106 
107  };
108 
116  public AutoValidator(@NotNull final DelegatingMapValidator<G, A, R> validators, final boolean autoDefault, @NotNull final DelayedMapModelListenerManager<G, A, R> delayedMapModelListenerManager) {
117  this.validators = validators;
118  this.delayedMapModelListenerManager = delayedMapModelListenerManager;
119  setDefaultPreferencesValue(autoDefault);
120  wasEnabled = isEnabled(); // call to isEnabled() must not happen before setDefaultPreferencesValue()
121  PREFERENCES.addPreferenceChangeListener(preferenceChangeListener);
122  delayedMapModelListenerManager.addDelayedMapModelListener(delayedMapModelListener);
123  }
124 
128  private void validateAllMaps() {
129  if (wasEnabled) {
130  delayedMapModelListenerManager.scheduleAllMapModels();
131  }
132  }
133 
138  private static void setDefaultPreferencesValue(final boolean autoDefault) {
139  final boolean enabled = PREFERENCES.getBoolean(PREFERENCES_VALIDATOR_AUTO, autoDefault);
140  if (enabled != isEnabled()) {
141  PREFERENCES.putBoolean(PREFERENCES_VALIDATOR_AUTO, autoDefault);
142  }
143  }
144 
149  public static boolean isEnabled() {
150  return PREFERENCES.getBoolean(PREFERENCES_VALIDATOR_AUTO, false);
151  }
152 
157  public static void setEnabled(final boolean enabled) {
158  PREFERENCES.putBoolean(PREFERENCES_VALIDATOR_AUTO, enabled);
159  }
160 
161 }
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
This package contains the framework for validating maps.
final DelegatingMapValidator< G, A, R > validators
The map validators to run.
static final String PREFERENCES_VALIDATOR_AUTO
Preferences key for auto validation.
static final Preferences PREFERENCES
Preferences.
void validateAll(@NotNull final MapModel< G, A, R > mapModel)
Perform all validations on a map.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final DelayedMapModelListener< G, A, R > delayedMapModelListener
The DelayedMapModelListener used to detect changed maps.
GameObjects are the objects based on Archetypes found on maps.
Interface used as preferences location.
final PreferenceChangeListener preferenceChangeListener
The preference change listener to detect an changed options.
static boolean isEnabled()
Return whether the auto-validator is enabled.
void validateAllMaps()
Schedules all maps for validation.
static void setDefaultPreferencesValue(final boolean autoDefault)
Set the default preferences value for PREFERENCES_VALIDATOR_AUTO.
boolean wasEnabled
The last known enabled state.
static void setEnabled(final boolean enabled)
Set whether the auto-validator is enabled.
A Map Validator that delegates to other MapValidators.
Implements the auto-validator for map validation.
Interface for listeners interested in delayed map model change events.
Provides support for delayed notification of MapModel changes.
final DelayedMapModelListenerManager< G, A, R > delayedMapModelListenerManager
The DelayedMapModelListenerManager.
AutoValidator(@NotNull final DelegatingMapValidator< G, A, R > validators, final boolean autoDefault, @NotNull final DelayedMapModelListenerManager< G, A, R > delayedMapModelListenerManager)
Create a new instance.