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-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.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 
61  @Nullable
62  private Action aOpenInClient;
63 
69  this.fileControl = fileControl;
70  }
71 
75  @ActionMethod
76  public void openInClient() {
77  doOpenInClient(true);
78  }
79 
83  private void updateActions() {
84  if (aOpenInClient != null) {
85  aOpenInClient.setEnabled(doOpenInClient(false));
86  }
87  }
88 
94  private boolean doOpenInClient(final boolean performAction) {
95  final MapView<G, A, R> mapView = currentMapView;
96  if (mapView == null) {
97  return false;
98  }
99 
100  final MapControl<G, A, R> mapControl = mapView.getMapControl();
101  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
102  final MapFile mapFile = mapModel.getMapFile();
103  if (mapFile == null) {
104  return false;
105  }
106 
107  if (performAction) {
108  if (mapModel.isModified() && !fileControl.save(mapControl)) {
109  return false;
110  }
111 
112  final Point cursor = mapView.getMapCursor().getLocation();
113  final int mapX = cursor.x;
114  final int mapY = cursor.y;
115  final String mapPath = mapFile.getMapPath().toString();
116  try {
117  resetMap(mapPath); // TODO: remove from here and add to map saving
118  teleportCharacterToMap(mapPath, mapX, mapY);
119  } catch (final IOException ex) {
120  fileControl.reportTeleportCharacterError(mapPath, ex.getMessage());
121  }
122  }
123 
124  return true;
125  }
126 
132  protected abstract void resetMap(@NotNull String mapPath) throws IOException;
133 
141  protected abstract void teleportCharacterToMap(@NotNull String mapPath, int mapX, int mapY) throws IOException;
142 
143  @Override
144  public void setAction(@NotNull final Action action, @NotNull final String name) {
145  aOpenInClient = action;
146  updateActions();
147  }
148 
149  @Override
150  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
151  currentMapView = mapView;
152  updateActions();
153  }
154 
158  @Override
159  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
160  // ignore
161  }
162 
166  @Override
167  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
168  // ignore
169  }
170 
171 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.actions.AbstractServerActions.openInClient
void openInClient()
Action method for "open in client".
Definition: AbstractServerActions.java:76
net.sf.gridarta.actions.AbstractServerActions.teleportCharacterToMap
abstract void teleportCharacterToMap(@NotNull String mapPath, int mapX, int mapY)
Teleports the character to the given map path.
net.sf.gridarta.actions.AbstractServerActions
Actions that require a connection to a game server.
Definition: AbstractServerActions.java:44
net.sf.gridarta.gui.map.mapview.MapView.getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.actions.AbstractServerActions.mapViewClosing
void mapViewClosing(@NotNull final MapView< G, A, R > mapView)
@noinspection NoopMethodInAbstractClass
Definition: AbstractServerActions.java:167
net.sf
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.mapview.MapViewManagerListener
Interface for listeners interested in events related to {} instances.
Definition: MapViewManagerListener.java:33
net.sf.gridarta.actions.AbstractServerActions.updateActions
void updateActions()
Update the enabled/disabled state of all actions.
Definition: AbstractServerActions.java:83
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
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.mapmodel.MapModel.isModified
boolean isModified()
Return whether the map has been modified from the on-disk state.
net.sf.gridarta.model.mapmodel.MapFile.getMapPath
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.actions.AbstractServerActions.mapViewCreated
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
@noinspection NoopMethodInAbstractClass
Definition: AbstractServerActions.java:159
net.sf.gridarta.gui.map.mapview.MapView.getMapCursor
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.actions.AbstractServerActions.setAction
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Definition: AbstractServerActions.java:144
net.sf.gridarta.actions.AbstractServerActions.activeMapViewChanged
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
Definition: AbstractServerActions.java:150
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.actions.AbstractServerActions.doOpenInClient
boolean doOpenInClient(final boolean performAction)
Executes the "open in client" action.
Definition: AbstractServerActions.java:94
net.sf.gridarta.actions.AbstractServerActions.aOpenInClient
Action aOpenInClient
The action for "open in client".
Definition: AbstractServerActions.java:62
net.sf.gridarta.model.mapcursor.MapCursor.getLocation
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:227
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.toString
String toString()
Definition: AbsoluteMapPath.java:117
net.sf.gridarta.model.mapmanager.FileControl.reportTeleportCharacterError
void reportTeleportCharacterError(@NotNull String mapPath, @NotNull String message)
Reports an error while teleporting a character to the current map.
net.sf.gridarta.actions.AbstractServerActions.AbstractServerActions
AbstractServerActions(@NotNull final FileControl< G, A, R > fileControl)
Creates a new instance.
Definition: AbstractServerActions.java:68
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.actions.AbstractServerActions.resetMap
abstract void resetMap(@NotNull String mapPath)
Resets a map identified by the given map path.
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.actions.AbstractServerActions.currentMapView
MapView< G, A, R > currentMapView
The currently active map or.
Definition: AbstractServerActions.java:50
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.mapmanager.FileControl.save
boolean save(@NotNull MapControl< G, A, R > mapControl)
Save one map.
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.utils.EditorAction
A global editor action.
Definition: EditorAction.java:29
net.sf.gridarta.actions.AbstractServerActions.fileControl
final FileControl< G, A, R > fileControl
The file control for saving maps.
Definition: AbstractServerActions.java:56
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20