Gridarta Editor
AbstractServerActions.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.actions;
21 
22 import java.awt.Point;
23 import java.io.IOException;
24 import javax.swing.Action;
35 import net.sf.japi.swing.action.ActionMethod;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
44 public abstract class AbstractServerActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction, MapViewManagerListener<G, A, R> {
45 
49  @Nullable
51 
55  @NotNull
57 
62  @Nullable
63  private Action aOpenInClient;
64 
69  protected AbstractServerActions(@NotNull final FileControl<G, A, R> fileControl) {
70  this.fileControl = fileControl;
71  }
72 
76  @ActionMethod
77  public void openInClient() {
78  doOpenInClient(true);
79  }
80 
84  private void updateActions() {
85  if (aOpenInClient != null) {
86  //noinspection ConstantConditions
87  aOpenInClient.setEnabled(doOpenInClient(false));
88  }
89  }
90 
96  private boolean doOpenInClient(final boolean performAction) {
97  final MapView<G, A, R> mapView = currentMapView;
98  if (mapView == null) {
99  return false;
100  }
101 
102  final MapControl<G, A, R> mapControl = mapView.getMapControl();
103  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
104  final MapFile mapFile = mapModel.getMapFile();
105  if (mapFile == null) {
106  return false;
107  }
108 
109  if (performAction) {
110  if (mapModel.isModified() && !fileControl.save(mapControl)) {
111  return false;
112  }
113 
114  final Point cursor = mapView.getMapCursor().getLocation();
115  final int mapX = cursor.x;
116  final int mapY = cursor.y;
117  final String mapPath = mapFile.getMapPath().toString();
118  try {
119  resetMap(mapPath); // TODO: remove from here and add to map saving
120  teleportCharacterToMap(mapPath, mapX, mapY);
121  } catch (final IOException ex) {
122  fileControl.reportTeleportCharacterError(mapPath, ex.getMessage());
123  }
124  }
125 
126  return true;
127  }
128 
134  protected abstract void resetMap(@NotNull String mapPath) throws IOException;
135 
143  protected abstract void teleportCharacterToMap(@NotNull String mapPath, int mapX, int mapY) throws IOException;
144 
145  @Override
146  public void setAction(@NotNull final Action action, @NotNull final String name) {
147  aOpenInClient = action;
148  updateActions();
149  }
150 
151  @Override
152  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
153  currentMapView = mapView;
154  updateActions();
155  }
156 
160  @Override
161  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
162  // ignore
163  }
164 
168  @Override
169  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
170  // ignore
171  }
172 
173 }
AbstractServerActions(@NotNull final FileControl< G, A, R > fileControl)
Creates a new instance.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
boolean doOpenInClient(final boolean performAction)
Executes the "open in client" action.
Graphical User Interface of Gridarta.
final FileControl< G, A, R > fileControl
The file control for saving maps.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
void updateActions()
Update the enabled/disabled state of all actions.
abstract void teleportCharacterToMap(@NotNull String mapPath, int mapX, int mapY)
Teleports the character to the given map path.
boolean isModified()
Return whether the map has been modified from the on-disk state.
void openInClient()
Action method for "open in client".
Actions that require a connection to a game server.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
MapModel< G, A, R > getMapModel()
Returns the map model.
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
NoopMethodInAbstractClass
void reportTeleportCharacterError(@NotNull String mapPath, @NotNull String message)
Reports an error while teleporting a character to the current map.
Base package of all Gridarta classes.
void mapViewClosing(@NotNull final MapView< G, A, R > mapView)
NoopMethodInAbstractClass
Reflects a game object (object on a map).
Definition: GameObject.java:36
A global editor action.
abstract void resetMap(@NotNull String mapPath)
Resets a map identified by the given map path.
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
GameObjects are the objects based on Archetypes found on maps.
Interface for listeners interested in events related to MapViewManager instances. ...
Action aOpenInClient
The action for "open in client".
MapView< G, A, R > currentMapView
The currently active map or.
boolean save(@NotNull MapControl< G, A, R > mapControl)
Save one map.
Base classes for rendering maps.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
MapFile getMapFile()
Returns the map file.
The location of a map file with a map directory.
Definition: MapFile.java:31