Gridarta Editor
AbstractMoveSquareAction.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 java.util.Set;
23 import javax.swing.Action;
38 import net.sf.gridarta.utils.Size2D;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
47 public abstract class AbstractMoveSquareAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
48 
52  @NotNull
54 
58  @Nullable
60 
64  @Nullable
65  private Action action;
66 
70  @NotNull
72 
73  @Override
74  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
75  setMapModel(mapControl == null ? null : mapControl.getMapModel());
76  }
77 
78  @Override
79  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
80  }
81 
82  @Override
83  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
84  }
85 
86  @Override
87  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
88  }
89 
90  };
91 
95  @NotNull
97 
98  @Override
99  public void mapSizeChanged(@NotNull final Size2D newSize) {
100  updateAction();
101  }
102 
103  @Override
104  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
105  if (selectedSquareModel.isSelectedMapSquares(mapSquares)) {
106  updateAction();
107  }
108  }
109 
110  @Override
111  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
112  if (selectedSquareModel.isSelectedGameObjects(gameObjects) || selectedSquareModel.isSelectedGameObjects(transientGameObjects)) {
113  updateAction();
114  }
115  }
116 
117  @Override
118  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
119  }
120 
121  @Override
122  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
123  }
124 
125  @Override
126  public void modifiedChanged() {
127  }
128 
129  };
130 
135  @NotNull
137 
138  @Override
139  public void selectionChanged(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
140  updateAction();
141  }
142 
143  };
144 
150  protected AbstractMoveSquareAction(@NotNull final SelectedSquareModel<G, A, R> selectedSquareModel, @Nullable final MapManager<G, A, R> mapManager) {
151  this.selectedSquareModel = selectedSquareModel;
152  selectedSquareModel.addSelectedSquareListener(selectedSquareModelListener);
153  if (mapManager != null) {
154  mapManager.addMapManagerListener(mapManagerListener);
155  }
156  }
157 
163  public boolean doAction(final boolean performAction) {
164  return doAction(performAction, selectedSquareModel.getSelectedGameObject());
165  }
166 
174  protected abstract boolean doAction(boolean performAction, @Nullable G gameObject);
175 
176  @Override
177  public void setAction(@NotNull final Action action, @NotNull final String name) {
178  this.action = action;
179  updateAction();
180  }
181 
185  private void updateAction() {
186  if (action != null) {
187  //noinspection ConstantConditions
188  action.setEnabled(doAction(false));
189  }
190  }
191 
197  private void setMapModel(@Nullable final MapModel<G, A, R> mapModel) {
198  if (this.mapModel != null) {
199  this.mapModel.removeMapModelListener(mapModelListener);
200  }
201  this.mapModel = mapModel;
202  if (this.mapModel != null) {
203  this.mapModel.addMapModelListener(mapModelListener);
204  }
205 
206  updateAction();
207  }
208 
209 }
G getSelectedGameObject()
Returns the currently selected GameObject within this list (currently selected MapSquare).
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
boolean isSelectedMapSquares(@NotNull final Iterable< MapSquare< G, A, R >> mapSquares)
A MapModel reflects the data of a map.
Definition: MapModel.java:75
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
This package contains the framework for validating maps.
Interface for listeners listening on MapModel events.
Interface for listeners interested in events of SelectedSquareModel instances.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
Base package of all Gridarta classes.
MapModel< G, A, R > mapModel
The currently active MapModel.
Abstract base class for actions that move the selected game object in the SelectedSquareModel.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void setMapModel(@Nullable final MapModel< G, A, R > mapModel)
Called whether the current map has changed.
A global editor action.
Interface for listeners listening to MapManager changes.
final SelectedSquareModel< G, A, R > selectedSquareModel
The SelectedSquareModel that is updated.
GameObjects are the objects based on Archetypes found on maps.
void updateAction()
Updates the enabled state of action.
Displays the contents of the currently selected map square.
final SelectedSquareModelListener< G, A, R > selectedSquareModelListener
The SelectedSquareModelListener attached to selectedSquareModel.
void addSelectedSquareListener(@NotNull final SelectedSquareModelListener< G, A, R > listener)
Adds a SelectedSquareModelListener to be notified.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
final MapManagerListener< G, A, R > mapManagerListener
The map view manager listener.
AbstractMoveSquareAction(@NotNull final SelectedSquareModel< G, A, R > selectedSquareModel, @Nullable final MapManager< G, A, R > mapManager)
Creates a new instance.
An interface for classes that collect errors.
The model component of the selected square control.
boolean doAction(final boolean performAction)
Executes the action.
boolean isSelectedGameObjects(@NotNull final Iterable< G > gameObjects)
The location of a map file with a map directory.
Definition: MapFile.java:31
final MapModelListener< G, A, R > mapModelListener
The MapModelListener attached to mapModel.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30