Gridarta Editor
ValidateMapAction.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.action;
21 
22 import javax.swing.Action;
31 import net.sf.japi.swing.action.ActionMethod;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
39 public class ValidateMapAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
40 
44  @NotNull
46 
51  @Nullable
52  private Action action;
53 
57  @Nullable
59 
65  @NotNull
67  return new MapManagerListener<G, A, R>() {
68 
69  @Override
70  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
71  currentMapControl = mapControl;
72  updateActions();
73  }
74 
75  @Override
76  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
77  // ignore: a current map changed event will be generated
78  }
79 
80  @Override
81  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
82  // ignore: a current map changed event will be generated
83  }
84 
85  @Override
86  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
87  // ignore: a current map changed event will be generated
88  }
89 
90  };
91  }
92 
98  public ValidateMapAction(@NotNull final DelegatingMapValidator<G, A, R> validators, @NotNull final MapManager<G, A, R> mapManager) {
99  this.validators = validators;
100  mapManager.addMapManagerListener(newMapManagerListener());
101  currentMapControl = mapManager.getCurrentMap();
102  }
103 
107  @ActionMethod
108  public void validateMap() {
109  doValidateMap(true);
110  }
111 
112  @Override
113  public void setAction(@NotNull final Action action, @NotNull final String name) {
114  this.action = action;
115  }
116 
122  private boolean doValidateMap(final boolean performAction) {
123  final MapControl<G, A, R> mapControl = currentMapControl;
124  if (mapControl == null) {
125  return false;
126  }
127 
128  if (performAction) {
129  validators.validateAll(mapControl.getMapModel());
130  }
131 
132  return true;
133  }
134 
138  private void updateActions() {
139  if (action != null) {
140  //noinspection ConstantConditions
141  action.setEnabled(doValidateMap(false));
142  }
143  }
144 
145 }
void validateMap()
Runs the map validator on the current map.
A MapManager manages all opened maps.
Definition: MapManager.java:37
An EditorAction that runs the map validator on the current map.
This package contains the framework for validating maps.
MapModel< G, A, R > getMapModel()
Returns the map model.
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
A global editor action.
Interface for listeners listening to MapManager changes.
GameObjects are the objects based on Archetypes found on maps.
MapControl< G, A, R > currentMapControl
The last known active map, or.
void updateActions()
Updates the state of the action.
boolean doValidateMap(final boolean performAction)
Executes the "validate map" action.
final DelegatingMapValidator< G, A, R > validators
The map validators.
A Map Validator that delegates to other MapValidators.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
MapManagerListener< G, A, R > newMapManagerListener()
Creates a new MapManagerListener that refreshes the actions when the current map changes.
Action action
The Action associated with this editor action.
ValidateMapAction(@NotNull final DelegatingMapValidator< G, A, R > validators, @NotNull final MapManager< G, A, R > mapManager)
Creates a new instance.