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-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.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) {
177  currentMapControl.getMapModel().removeMapModelListener(mapModelListener);
178  }
179  currentMapControl = mapControl;
180  if (currentMapControl != null) {
181  currentMapControl.getMapModel().addMapModelListener(mapModelListener);
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;
218  mapManager.addMapManagerListener(mapManagerListener);
219  currentMapControl = mapManager.getCurrentMap();
220  if (currentMapControl != null) {
221  currentMapControl.getMapModel().addMapModelListener(mapModelListener);
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() {
251  mapManager.removeMapManagerListener(mapManagerListener);
252  if (currentMapControl != null) {
253  currentMapControl.getMapModel().removeMapModelListener(mapModelListener);
254  }
255  }
256 
260  private void updateActions() {
261  if (aSaveMap != null) {
262  //noinspection ConstantConditions
263  aSaveMap.setEnabled(doSaveMap(false));
264  }
265  if (aSaveMapAs != null) {
266  //noinspection ConstantConditions
267  aSaveMapAs.setEnabled(doSaveMapAs(false));
268  }
269  if (aCreateImage != null) {
270  //noinspection ConstantConditions
271  aCreateImage.setEnabled(doCreateImage(false));
272  }
273  if (aReloadMap != null) {
274  //noinspection ConstantConditions
275  aReloadMap.setEnabled(doReloadMap(false));
276  }
277  if (aCloseMap != null) {
278  //noinspection ConstantConditions
279  aCloseMap.setEnabled(doCloseMap(false));
280  }
281  }
282 
286  @ActionMethod
287  public void saveMap() {
288  doSaveMap(true);
289  }
290 
294  @ActionMethod
295  public void saveMapAs() {
296  doSaveMapAs(true);
297  }
298 
302  @ActionMethod
303  public void createImage() {
304  doCreateImage(true);
305  }
306 
311  @ActionMethod
312  public void reloadMap() {
313  doReloadMap(true);
314  }
315 
319  @ActionMethod
320  public void closeMap() {
321  doCloseMap(true);
322  }
323 
329  private boolean doSaveMap(final boolean performAction) {
330  final MapControl<G, A, R> mapControl = currentMapControl;
331  if (mapControl == null || !mapControl.getMapModel().isModified()) {
332  return false;
333  }
334 
335  if (performAction) {
336  if (!fileControl.save(mapControl)) {
337  return false;
338  }
339  }
340 
341  return true;
342  }
343 
349  private boolean doSaveMapAs(final boolean performAction) {
350  final MapControl<G, A, R> mapControl = currentMapControl;
351  if (mapControl == null) {
352  return false;
353  }
354 
355  if (performAction) {
356  if (!fileControl.saveAs(mapControl)) {
357  return false;
358  }
359  }
360 
361  return true;
362  }
363 
369  private boolean doCreateImage(final boolean performAction) {
370  final MapView<G, A, R> mapView = currentMapView;
371  if (mapView == null) {
372  return false;
373  }
374 
375  if (performAction) {
376  final MapModel<G, A, R> mapModel = mapView.getMapControl().getMapModel();
377  imageCreator2.createImage(mapModel, mainView);
378  }
379 
380  return true;
381  }
382 
388  private boolean doReloadMap(final boolean performAction) {
389  final MapControl<G, A, R> mapControl = currentMapControl;
390  if (mapControl == null) {
391  return false;
392  }
393 
394  final MapFile mapFile = mapControl.getMapModel().getMapFile();
395  if (mapFile == null) {
396  return false;
397  }
398 
399  if (performAction) {
400  try {
401  mapManager.revert(mapControl);
402  } catch (final IOException ex) {
403  fileControl.reportLoadError(mapFile.getFile(), ex.getMessage());
404  return false;
405  }
406  }
407 
408  return true;
409  }
410 
416  private boolean doCloseMap(final boolean performAction) {
417  final MapView<G, A, R> mapView = currentMapView;
418  if (mapView == null) {
419  return false;
420  }
421 
422  if (performAction) {
423  mapViewsManager.closeMapView(mapView);
424  }
425 
426  return true;
427  }
428 
429  @Override
430  public void setAction(@NotNull final Action action, @NotNull final String name) {
431  if (name.equals("saveMap")) {
432  aSaveMap = action;
433  } else if (name.equals("saveMapAs")) {
434  aSaveMapAs = action;
435  } else if (name.equals("createImage")) {
436  aCreateImage = action;
437  } else if (name.equals("reloadMap")) {
438  aReloadMap = action;
439  } else if (name.equals("closeMap")) {
440  aCloseMap = action;
441  } else {
442  throw new IllegalArgumentException();
443  }
444  updateActions();
445  }
446 
447 }
void removeMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Removes a MapManagerListener to be notified.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Action aSaveMap
The action for "save map".
A MapManager manages all opened maps.
Definition: MapManager.java:37
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
void closeMapView(@NotNull final MapView< G, A, R > mapView)
Invoked when the user wants to close a map view.
Graphical User Interface of Gridarta.
boolean saveAs(@NotNull MapControl< G, A, R > mapControl)
Asks the user for a filename, then saves the map.
final ImageCreator2< G, A, R > imageCreator2
The ImageCreator2 to forwards actions to.
Action aCloseMap
The action for "close map".
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)
Create a new instance that tracks the map state.
This package contains the framework for validating maps.
final MapModelListener< G, A, R > mapModelListener
The MapModelListener which is attached to currentMapControl&#39;s map model.
MapControl< G, A, R > currentMapControl
The currently tracked map, or.
Interface for listeners listening on MapModel events.
void updateActions()
Update the enabled/disabled state of all actions.
Implements actions for the "file" menu attached to maps.
boolean doCloseMap(final boolean performAction)
Executes the "close map" action.
boolean isModified()
Return whether the map has been modified from the on-disk state.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
void createImage()
Invoked when the user wants to create an image file of the map.
MapModel< G, A, R > getMapModel()
Returns the map model.
void revert(@NotNull MapControl< G, A, R > mapControl)
Reverts one map.
Base package of all Gridarta classes.
final FileControl< G, A, R > fileControl
The file control to forward actions to.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void reportLoadError(@Nullable File file, @NotNull String message)
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
A global editor action.
void closeMap()
Invoked when the user wants to close the map.
Interface for listeners listening to MapManager changes.
GameObjects are the objects based on Archetypes found on maps.
final Component mainView
The main view Component.
Creates images from map instances.
Interface for listeners interested in events related to MapViewManager instances. ...
Action aSaveMapAs
The action for "save map as".
MapView< G, A, R > currentMapView
The current map view, or.
boolean save(@NotNull MapControl< G, A, R > mapControl)
Save one map.
boolean doSaveMapAs(final boolean performAction)
Executes the "save map as" action.
Base classes for rendering maps.
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
Action aCreateImage
The action for "create image".
void closeNotify()
Unregister all registered listeners.
void createImage(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Component component)
Create an image of a map and save it as a file.
boolean doCreateImage(final boolean performAction)
Executes the "create image" action.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
final MapManagerListener< G, A, R > mapManagerListener
The map manager listener which is attached to the current map if the current map is tracked...
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.
File getFile()
Returns a File for this map file.
Definition: MapFile.java:102
final MapManager< G, A, R > mapManager
The map manager.
An interface for classes that collect errors.
void reloadMap()
Invoked when the user wants to reload the map to the previously saved state.
boolean doSaveMap(final boolean performAction)
Executes the "save map" action.
boolean doReloadMap(final boolean performAction)
Executes the "reload map" action.
void saveMap()
Invoked when the user wants to save the map.
void saveMapAs()
Invoked when the user wants to save the map to a file.
The location of a map file with a map directory.
Definition: MapFile.java:31
Action aReloadMap
The action for "reload map".
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30