Gridarta Editor
MapFileActions.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.gui.map;
21 
22 import java.awt.Component;
23 import java.io.IOException;
24 import java.util.Set;
25 import javax.swing.Action;
44 import net.sf.gridarta.utils.Size2D;
45 import net.sf.japi.swing.action.ActionMethod;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
48 
53 public class MapFileActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
54 
58  @NotNull
60 
64  @NotNull
66 
70  @NotNull
71  private final Component mainView;
72 
76  @NotNull
78 
82  @NotNull
84 
88  @Nullable
89  private Action aSaveMap;
90 
94  @Nullable
95  private Action aSaveMapAs;
96 
100  @Nullable
101  private Action aCreateImage;
102 
106  @Nullable
107  private Action aReloadMap;
108 
112  @Nullable
113  private Action aCloseMap;
114 
119  @Nullable
121 
125  @Nullable
127 
132  @NotNull
134 
135  @Override
136  public void mapSizeChanged(@NotNull final Size2D newSize) {
137  // ignore
138  }
139 
140  @Override
141  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
142  // ignore
143  }
144 
145  @Override
146  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
147  // ignore
148  }
149 
150  @Override
151  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
152  // ignore
153  }
154 
155  @Override
156  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
157  // ignore
158  }
159 
160  @Override
161  public void modifiedChanged() {
162  updateActions();
163  }
164 
165  };
166 
171  @NotNull
173 
174  @Override
175  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
176  if (currentMapControl != null) {
178  }
179  currentMapControl = mapControl;
180  if (currentMapControl != null) {
182  }
183  updateActions();
184  }
185 
186  @Override
187  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
188  // ignore
189  }
190 
191  @Override
192  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
193  // ignore
194  }
195 
196  @Override
197  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
198  // ignore
199  }
200 
201  };
202 
212  public MapFileActions(@NotNull final ImageCreator2<G, A, R> imageCreator2, @NotNull final MapManager<G, A, R> mapManager, @NotNull final MapViewsManager<G, A, R> mapViewsManager, @NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final FileControl<G, A, R> fileControl, @NotNull final Component mainView) {
213  this.imageCreator2 = imageCreator2;
214  this.mapManager = mapManager;
215  this.mapViewsManager = mapViewsManager;
216  this.fileControl = fileControl;
217  this.mainView = mainView;
220  if (currentMapControl != null) {
222  }
223  final MapViewManagerListener<G, A, R> mapViewManagerListener = new MapViewManagerListener<G, A, R>() {
224 
225  @Override
226  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
227  currentMapView = mapView;
228  updateActions();
229  }
230 
231  @Override
232  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
233  // ignore
234  }
235 
236  @Override
237  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
238  // ignore
239  }
240 
241  };
242  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
243  currentMapView = mapViewManager.getActiveMapView();
244  }
245 
250  public void closeNotify() {
252  if (currentMapControl != null) {
254  }
255  }
256 
260  private void updateActions() {
261  if (aSaveMap != null) {
262  aSaveMap.setEnabled(doSaveMap(false));
263  }
264  if (aSaveMapAs != null) {
265  aSaveMapAs.setEnabled(doSaveMapAs(false));
266  }
267  if (aCreateImage != null) {
268  aCreateImage.setEnabled(doCreateImage(false));
269  }
270  if (aReloadMap != null) {
271  aReloadMap.setEnabled(doReloadMap(false));
272  }
273  if (aCloseMap != null) {
274  aCloseMap.setEnabled(doCloseMap(false));
275  }
276  }
277 
281  @ActionMethod
282  public void saveMap() {
283  doSaveMap(true);
284  }
285 
289  @ActionMethod
290  public void saveMapAs() {
291  doSaveMapAs(true);
292  }
293 
297  @ActionMethod
298  public void createImage() {
299  doCreateImage(true);
300  }
301 
306  @ActionMethod
307  public void reloadMap() {
308  doReloadMap(true);
309  }
310 
314  @ActionMethod
315  public void closeMap() {
316  doCloseMap(true);
317  }
318 
324  private boolean doSaveMap(final boolean performAction) {
325  final MapControl<G, A, R> mapControl = currentMapControl;
326  if (mapControl == null || !mapControl.getMapModel().isModified()) {
327  return false;
328  }
329 
330  if (performAction) {
331  if (!fileControl.save(mapControl)) {
332  return false;
333  }
334  }
335 
336  return true;
337  }
338 
344  private boolean doSaveMapAs(final boolean performAction) {
345  final MapControl<G, A, R> mapControl = currentMapControl;
346  if (mapControl == null) {
347  return false;
348  }
349 
350  if (performAction) {
351  if (!fileControl.saveAs(mapControl)) {
352  return false;
353  }
354  }
355 
356  return true;
357  }
358 
364  private boolean doCreateImage(final boolean performAction) {
365  final MapView<G, A, R> mapView = currentMapView;
366  if (mapView == null) {
367  return false;
368  }
369 
370  if (performAction) {
371  final MapModel<G, A, R> mapModel = mapView.getMapControl().getMapModel();
373  }
374 
375  return true;
376  }
377 
383  private boolean doReloadMap(final boolean performAction) {
384  final MapControl<G, A, R> mapControl = currentMapControl;
385  if (mapControl == null) {
386  return false;
387  }
388 
389  final MapFile mapFile = mapControl.getMapModel().getMapFile();
390  if (mapFile == null) {
391  return false;
392  }
393 
394  if (performAction) {
395  try {
396  mapManager.revert(mapControl);
397  } catch (final IOException ex) {
398  fileControl.reportLoadError(mapFile.getFile(), ex.getMessage());
399  return false;
400  }
401  }
402 
403  return true;
404  }
405 
411  private boolean doCloseMap(final boolean performAction) {
412  final MapView<G, A, R> mapView = currentMapView;
413  if (mapView == null) {
414  return false;
415  }
416 
417  if (performAction) {
418  mapViewsManager.closeMapView(mapView);
419  }
420 
421  return true;
422  }
423 
424  @Override
425  public void setAction(@NotNull final Action action, @NotNull final String name) {
426  switch (name) {
427  case "saveMap":
428  aSaveMap = action;
429  break;
430 
431  case "saveMapAs":
432  aSaveMapAs = action;
433  break;
434 
435  case "createImage":
436  aCreateImage = action;
437  break;
438 
439  case "reloadMap":
440  aReloadMap = action;
441  break;
442 
443  case "closeMap":
444  aCloseMap = action;
445  break;
446 
447  default:
448  throw new IllegalArgumentException("unsupported action name: " + name);
449  }
450  updateActions();
451  }
452 
453 }
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.gui.map.MapFileActions.closeMap
void closeMap()
Invoked when the user wants to close the map.
Definition: MapFileActions.java:315
net.sf.gridarta.gui.map.MapFileActions.MapFileActions
MapFileActions(@NotNull final ImageCreator2< G, A, R > imageCreator2, @NotNull final MapManager< G, A, R > mapManager, @NotNull final MapViewsManager< G, A, R > mapViewsManager, @NotNull final MapViewManager< G, A, R > mapViewManager, @NotNull final FileControl< G, A, R > fileControl, @NotNull final Component mainView)
Creates a new instance that tracks the map state.
Definition: MapFileActions.java:212
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta.model.mapmanager.MapManager.removeMapManagerListener
void removeMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Removes a MapManagerListener to be notified.
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.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.gui.map.mapview.MapViewManager
Maintains all map views.
Definition: MapViewManager.java:38
net.sf
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.model.mapmanager.MapManagerListener
Interface for listeners listening to MapManager changes.
Definition: MapManagerListener.java:42
net.sf.gridarta.gui.map.MapFileActions.mapManagerListener
final MapManagerListener< G, A, R > mapManagerListener
The map manager listener which is attached to the current map if the current map is tracked.
Definition: MapFileActions.java:172
net.sf.gridarta.gui.map.MapFileActions.mapManager
final MapManager< G, A, R > mapManager
The map manager.
Definition: MapFileActions.java:77
net.sf.gridarta.gui.map.MapFileActions.saveMapAs
void saveMapAs()
Invoked when the user wants to save the map to a file.
Definition: MapFileActions.java:290
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.MapFileActions.aSaveMapAs
Action aSaveMapAs
The action for "save map as".
Definition: MapFileActions.java:95
net.sf.gridarta.gui.map.mapview.MapViewManagerListener
Interface for listeners interested in events related to {} instances.
Definition: MapViewManagerListener.java:33
net.sf.gridarta.model.mapmanager.FileControl.saveAs
boolean saveAs(@NotNull MapControl< G, A, R > mapControl)
Asks the user for a filename, then saves the map.
net.sf.gridarta.gui.map.MapFileActions.mainView
final Component mainView
The main view Component.
Definition: MapFileActions.java:71
net.sf.gridarta.gui.map.MapFileActions.setAction
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Definition: MapFileActions.java:425
net.sf.gridarta.gui.map.MapFileActions.mapViewsManager
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: MapFileActions.java:83
net.sf.gridarta.gui.map.renderer
Definition: AbstractIsoMapRenderer.java:20
net.sf.gridarta.model.mapmanager.MapManager.addMapManagerListener
void addMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Adds a MapManagerListener to be notified.
net.sf.gridarta.gui.map.renderer.ImageCreator2
Creates images from map instances.
Definition: ImageCreator2.java:43
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.mapmanager.FileControl.reportLoadError
void reportLoadError(@Nullable File file, @NotNull String message)
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.addMapModelListener
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
net.sf.gridarta.model.mapmodel.MapModel.isModified
boolean isModified()
Return whether the map has been modified from the on-disk state.
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.map.renderer.ImageCreator2.createImage
void createImage(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Component component)
Creates an image of a map and save it as a file.
Definition: ImageCreator2.java:80
net
net.sf.gridarta.gui.map.MapFileActions.fileControl
final FileControl< G, A, R > fileControl
The file control to forward actions to.
Definition: MapFileActions.java:65
net.sf.gridarta.gui.map.MapFileActions.reloadMap
void reloadMap()
Invoked when the user wants to reload the map to the previously saved state.
Definition: MapFileActions.java:307
net.sf.gridarta.gui.map.mapview.MapViewsManager.closeMapView
void closeMapView(@NotNull final MapView< G, A, R > mapView)
Invoked when the user wants to close a map view.
Definition: MapViewsManager.java:308
net.sf.gridarta.gui.map.MapFileActions.doSaveMap
boolean doSaveMap(final boolean performAction)
Executes the "save map" action.
Definition: MapFileActions.java:324
net.sf.gridarta.gui.map.MapFileActions.doCloseMap
boolean doCloseMap(final boolean performAction)
Executes the "close map" action.
Definition: MapFileActions.java:411
net.sf.gridarta.gui.map.MapFileActions.closeNotify
void closeNotify()
Unregisters all registered listeners.
Definition: MapFileActions.java:250
net.sf.gridarta.gui.map.MapFileActions.imageCreator2
final ImageCreator2< G, A, R > imageCreator2
The ImageCreator2 to forwards actions to.
Definition: MapFileActions.java:59
errors
errors
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:1
net.sf.gridarta.model.mapmodel.MapModel.removeMapModelListener
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
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.model.mapmodel.MapFile.getFile
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
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.gui.map.MapFileActions.aReloadMap
Action aReloadMap
The action for "reload map".
Definition: MapFileActions.java:107
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.mapmanager.MapManager.getCurrentMap
MapControl< G, A, R > getCurrentMap()
Returns the current map.
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.gui.map.MapFileActions.mapModelListener
final MapModelListener< G, A, R > mapModelListener
The MapModelListener which is attached to {}'s map model.
Definition: MapFileActions.java:133
net.sf.gridarta.model.mapmodel.MapModelListener
Interface for listeners listening on MapModel events.
Definition: MapModelListener.java:36
net.sf.gridarta.gui.map.MapFileActions.doReloadMap
boolean doReloadMap(final boolean performAction)
Executes the "reload map" action.
Definition: MapFileActions.java:383
net.sf.gridarta.gui.map.MapFileActions.createImage
void createImage()
Invoked when the user wants to create an image file of the map.
Definition: MapFileActions.java:298
net.sf.gridarta.gui.map.MapFileActions.doCreateImage
boolean doCreateImage(final boolean performAction)
Executes the "create image" action.
Definition: MapFileActions.java:364
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.map.MapFileActions.currentMapControl
MapControl< G, A, R > currentMapControl
The currently tracked map, or.
Definition: MapFileActions.java:120
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.model.mapmanager.MapManager.revert
void revert(@NotNull MapControl< G, A, R > mapControl)
Reverts one map.
net.sf.gridarta.gui.map.MapFileActions.currentMapView
MapView< G, A, R > currentMapView
The current map view, or.
Definition: MapFileActions.java:126
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
net.sf.gridarta.gui.map.MapFileActions.saveMap
void saveMap()
Invoked when the user wants to save the map.
Definition: MapFileActions.java:282
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.map.MapFileActions.aCreateImage
Action aCreateImage
The action for "create image".
Definition: MapFileActions.java:101
net.sf.gridarta.gui.map.MapFileActions.updateActions
void updateActions()
Updates the enabled/disabled state of all actions.
Definition: MapFileActions.java:260
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.gui.map.mapview.MapViewsManager
Stores all existing MapViews.
Definition: MapViewsManager.java:47
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.map.MapFileActions.aCloseMap
Action aCloseMap
The action for "close map".
Definition: MapFileActions.java:113
net.sf.gridarta.gui.map.MapFileActions.doSaveMapAs
boolean doSaveMapAs(final boolean performAction)
Executes the "save map as" action.
Definition: MapFileActions.java:344
net.sf.gridarta.gui.map.MapFileActions.aSaveMap
Action aSaveMap
The action for "save map".
Definition: MapFileActions.java:89
net.sf.gridarta.gui.map.MapFileActions
Implements actions for the "file" menu attached to maps.
Definition: MapFileActions.java:53