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-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.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  updateActions();
116  }
117 
123  private boolean doValidateMap(final boolean performAction) {
124  final MapControl<G, A, R> mapControl = currentMapControl;
125  if (mapControl == null) {
126  return false;
127  }
128 
129  if (performAction) {
130  validators.validateAll(mapControl.getMapModel());
131  }
132 
133  return true;
134  }
135 
139  private void updateActions() {
140  if (action != null) {
141  action.setEnabled(doValidateMap(false));
142  }
143  }
144 
145 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta.action.ValidateMapAction.currentMapControl
MapControl< G, A, R > currentMapControl
The last known active map, or.
Definition: ValidateMapAction.java:58
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.action.ValidateMapAction.newMapManagerListener
MapManagerListener< G, A, R > newMapManagerListener()
Creates a new MapManagerListener that refreshes the actions when the current map changes.
Definition: ValidateMapAction.java:66
net.sf
net.sf.gridarta.action.ValidateMapAction.setAction
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Definition: ValidateMapAction.java:113
net.sf.gridarta.model.mapmanager.MapManagerListener
Interface for listeners listening to MapManager changes.
Definition: MapManagerListener.java:42
net.sf.gridarta.action.ValidateMapAction.action
Action action
The Action associated with this editor action.
Definition: ValidateMapAction.java:52
net.sf.gridarta.action.ValidateMapAction.doValidateMap
boolean doValidateMap(final boolean performAction)
Executes the "validate map" action.
Definition: ValidateMapAction.java:123
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.action.ValidateMapAction.ValidateMapAction
ValidateMapAction(@NotNull final DelegatingMapValidator< G, A, R > validators, @NotNull final MapManager< G, A, R > mapManager)
Creates a new instance.
Definition: ValidateMapAction.java:98
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.action.ValidateMapAction.validateMap
void validateMap()
Runs the map validator on the current map.
Definition: ValidateMapAction.java:108
net.sf.gridarta.model.validation.DelegatingMapValidator.validateAll
void validateAll(@NotNull final MapModel< G, A, R > mapModel)
Performs all validations on a map.
Definition: DelegatingMapValidator.java:71
net.sf.gridarta.action.ValidateMapAction
An EditorAction that runs the map validator on the current map.
Definition: ValidateMapAction.java:39
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.validation.DelegatingMapValidator
A Map Validator that delegates to other MapValidators.
Definition: DelegatingMapValidator.java:36
net.sf.gridarta.utils.EditorAction
A global editor action.
Definition: EditorAction.java:29
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.action.ValidateMapAction.updateActions
void updateActions()
Updates the state of the action.
Definition: ValidateMapAction.java:139
net.sf.gridarta.action.ValidateMapAction.validators
final DelegatingMapValidator< G, A, R > validators
The map validators.
Definition: ValidateMapAction.java:45