Gridarta Editor
EditorActionManager.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.maincontrol;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import javax.swing.Action;
34 import net.sf.japi.swing.action.ActionBuilder;
35 import net.sf.japi.swing.action.ActionBuilderFactory;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
52 public class EditorActionManager<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
53 
57  @NotNull
58  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
59 
63  @NotNull
65 
70  @NotNull
71  private final Collection<MapCursorListener<G, A, R>> mapCursorListeners = new ArrayList<>();
72 
76  @Nullable
78 
83  @NotNull
85 
86  @Override
87  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
88  final MapView<G, A, R> tmpMapView = currentMapView;
89  if (tmpMapView != null) {
90  for (final MapCursorListener<G, A, R> mapCursorListener : mapCursorListeners) {
91  tmpMapView.getMapCursor().removeMapCursorListener(mapCursorListener);
92  }
93  }
94  currentMapView = mapView;
95  if (mapView != null) {
96  for (final MapCursorListener<G, A, R> mapCursorListener : mapCursorListeners) {
97  mapView.getMapCursor().addMapCursorListener(mapCursorListener);
98  }
99  }
100  }
101 
102  @Override
103  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
104  // ignore
105  }
106 
107  @Override
108  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
109  // ignore
110  }
111 
112  };
113 
118  public EditorActionManager(@NotNull final MapViewManager<G, A, R> mapViewManager) {
119  this.mapViewManager = mapViewManager;
120  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
121  currentMapView = mapViewManager.getActiveMapView();
122  }
123 
131  @NotNull
132  public Action createAction(@NotNull final String name, @NotNull final String category, @NotNull final EditorAction editorAction) {
133  addEditorAction(editorAction);
134  return ActionUtils.newAction(ACTION_BUILDER, category, editorAction, name);
135  }
136 
145  @Nullable
146  public Action createActionOptional(@NotNull final String name, @NotNull final String category, @Nullable final EditorAction editorAction) {
147  return editorAction == null ? null : createAction(name, category, editorAction);
148  }
149 
157  @NotNull
158  public Action createToggleAction(@NotNull final String name, @NotNull final String category, @NotNull final EditorAction editorAction) {
159  addEditorAction(editorAction);
160  return ActionUtils.newToggleAction(ACTION_BUILDER, category, editorAction, name);
161  }
162 
167  private void addEditorAction(@NotNull final EditorAction editorAction) {
168  if (currentMapView != null) {
169  // MapViewManagerListener instances would not know this
170  throw new IllegalStateException();
171  }
172 
173  if (editorAction instanceof MapCursorListener) {
174  final MapCursorListener<G, A, R> mapCursorListener = (MapCursorListener<G, A, R>) editorAction;
175  mapCursorListeners.add(mapCursorListener);
176  }
177  if (editorAction instanceof MapViewManagerListener) {
178  final MapViewManagerListener<G, A, R> mapViewManagerListener = (MapViewManagerListener<G, A, R>) editorAction;
179  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
180  }
181  }
182 
183 }
void addMapViewManagerListener(@NotNull final MapViewManagerListener< G, A, R > listener)
Adds a listener to be notified.
Graphical User Interface of Gridarta.
MapView< G, A, R > currentMapView
The active map view, or.
final Collection< MapCursorListener< G, A, R > > mapCursorListeners
The EditorActions that are interested in MapCursorListener events.
Utility class implementing Action related functions.
final MapViewManagerListener< G, A, R > mapViewManagerListener
The map view manager listener used to detect changed current maps.
Base package of all Gridarta classes.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
Definition: MapCursor.java:419
Action createToggleAction(@NotNull final String name, @NotNull final String category, @NotNull final EditorAction editorAction)
Initializes a new action.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Action createAction(@NotNull final String name, @NotNull final String category, @NotNull final EditorAction editorAction)
Initializes a new action.
A global editor action.
GameObjects are the objects based on Archetypes found on maps.
Action createActionOptional(@NotNull final String name, @NotNull final String category, @Nullable final EditorAction editorAction)
Initializes a new action.
Interface for listeners interested in events related to MapViewManager instances. ...
void addEditorAction(@NotNull final EditorAction editorAction)
Records the event types an EditorAction is interested in.
Maintains a set of EditorAction instances and calls their event listener callbacks.
EditorActionManager(@NotNull final MapViewManager< G, A, R > mapViewManager)
Creates a new instance.
Base classes for rendering maps.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
final MapViewManager< G, A, R > mapViewManager
The MapViewManager for tracking open maps.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Interface for listeners listening to MapCursor related events.
void removeMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Remove a MapCursorListener.
Definition: MapCursor.java:427
static Action newToggleAction(@NotNull final ActionBuilder actionBuilder, @NotNull final String category, @NotNull final EditorAction editorAction, @NotNull final String key)
Creates a new Action instance.
static Action newAction(@NotNull final ActionBuilder actionBuilder, @NotNull final String category, @NotNull final EditorAction editorAction, @NotNull final String key)
Creates a new Action instance.