Gridarta Editor
ExitConnectorController.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.exitconnector;
21 
22 import java.awt.Point;
23 import javax.swing.Action;
34 import net.sf.japi.swing.action.ActionMethod;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class ExitConnectorController<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction, MapViewManagerListener<G, A, R> {
43 
47  @NotNull
49 
53  @Nullable
54  private Action aExitCopy;
55 
59  @Nullable
60  private Action aExitPaste;
61 
65  @Nullable
66  private Action aExitConnect;
67 
71  @Nullable
73 
77  @NotNull
79 
80  @Override
81  public void exitLocationChanged(@Nullable final ExitLocation exitLocation) {
83  }
84 
85  @Override
86  public void pasteExitNameChanged(final boolean pasteExitName) {
87  // ignore
88  }
89 
90  @Override
91  public void autoCreateExitChanged(final boolean pasteExitName) {
93  }
94 
95  @Override
96  public void exitArchetypeNameChanged(@NotNull final String exitArchetypeName) {
97  // ignore
98  }
99 
100  };
101 
107  public ExitConnectorController(@NotNull final ExitConnectorActions<G, A, R> exitConnectorActions, @NotNull final ExitConnectorModel exitConnectorModel) {
108  this.exitConnectorActions = exitConnectorActions;
109  exitConnectorModel.addExitConnectorModelListener(exitConnectorModelListener);
110  }
111 
115  @ActionMethod
116  public void exitCopy() {
117  doExitCopy(true);
118  }
119 
123  @ActionMethod
124  public void exitPaste() {
125  doExitPaste(true);
126  }
127 
131  @ActionMethod
132  public void exitConnect() {
133  doExitConnect(true);
134  }
135 
139  private void refreshActions() {
140  if (aExitCopy != null) {
141  //noinspection ConstantConditions
142  aExitCopy.setEnabled(doExitCopy(false));
143  }
144  if (aExitPaste != null) {
145  //noinspection ConstantConditions
146  aExitPaste.setEnabled(doExitPaste(false));
147  }
148  if (aExitConnect != null) {
149  //noinspection ConstantConditions
150  aExitConnect.setEnabled(doExitConnect(false));
151  }
152  }
153 
159  private boolean doExitCopy(final boolean performAction) {
160  final MapView<G, A, R> mapView = currentMapView;
161  if (mapView == null) {
162  // no active map view ==> no location to remember
163  return false;
164  }
165 
166  final Point location = mapView.getMapCursor().getLocation();
167  return exitConnectorActions.doExitCopy(performAction, mapView.getMapControl(), location);
168  }
169 
175  private boolean doExitPaste(final boolean performAction) {
176  final MapView<G, A, R> mapView = currentMapView;
177  if (mapView == null) {
178  return false;
179  }
180 
181  final Point targetLocation = mapView.getMapCursor().getLocation();
182  return exitConnectorActions.doExitPaste(performAction, mapView.getMapControl(), targetLocation);
183  }
184 
190  private boolean doExitConnect(final boolean performAction) {
191  final MapView<G, A, R> mapView = currentMapView;
192  if (mapView == null) {
193  return false;
194  }
195 
196  final Point targetLocation = mapView.getMapCursor().getLocation();
197  return exitConnectorActions.doExitConnect(performAction, mapView.getMapControl(), targetLocation);
198  }
199 
200  @Override
201  public void setAction(@NotNull final Action action, @NotNull final String name) {
202  if (name.equals("exitCopy")) {
203  aExitCopy = action;
204  } else if (name.equals("exitPaste")) {
205  aExitPaste = action;
206  } else if (name.equals("exitConnect")) {
207  aExitConnect = action;
208  } else {
209  throw new IllegalArgumentException();
210  }
211  refreshActions();
212  }
213 
214  @Override
215  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
216  currentMapView = mapView;
217  refreshActions();
218  }
219 
220  @Override
221  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
222  // ignore
223  }
224 
225  @Override
226  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
227  // ignore
228  }
229 
230 }
Graphical User Interface of Gridarta.
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view was created.
boolean doExitPaste(final boolean performAction)
Performs or checks availability of the "exit paste" action.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
final ExitConnectorModelListener exitConnectorModelListener
The ExitConnectorModelListener registered to the model.
ExitConnectorController(@NotNull final ExitConnectorActions< G, A, R > exitConnectorActions, @NotNull final ExitConnectorModel exitConnectorModel)
Creates a new instance.
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
boolean doExitCopy(final boolean performAction)
Performs or checks availability of the "exit copy" action.
A global editor action.
void mapViewClosing(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view is to be closed.
boolean doExitConnect(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit connect" action.
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
boolean doExitConnect(final boolean performAction)
Performs or checks availability of the "exit connect" action.
GameObjects are the objects based on Archetypes found on maps.
Interface for listeners interested in events related to MapViewManager instances. ...
Base classes for rendering maps.
Utility class implementing actions that operate on ExitConnectorModels.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Stores information needed by the exit connector.
Stores information about a remembered exit location.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
boolean doExitCopy(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point location)
Executes the "exit copy" action.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
void refreshActions()
Updates the enabled state of all actions.
final ExitConnectorActions< G, A, R > exitConnectorActions
The actions for this controller.
Interface for listeners interested in ExitConnectorModel related events.
boolean doExitPaste(final boolean performAction, @NotNull final MapControl< G, A, R > mapControl, @NotNull final Point targetLocation)
Executes the "exit paste" action.