Gridarta Editor
ExportMapAsImageAction.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.action.exportmap;
21 
22 import java.awt.image.BufferedImage;
23 import java.io.File;
24 import java.io.IOException;
25 import javax.imageio.ImageIO;
26 import javax.swing.Action;
27 import javax.swing.JFrame;
39 import net.sf.japi.swing.action.ActionMethod;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
47 public class ExportMapAsImageAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
48 
52  @NotNull
54 
58  @NotNull
60 
64  @NotNull
65  private final JFrame parent;
66 
71  @Nullable
72  private Action action;
73 
77  @Nullable
79 
85  @NotNull
87  return new MapManagerListener<G, A, R>() {
88 
89  @Override
90  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
91  currentMapControl = mapControl;
92  updateActions();
93  }
94 
95  @Override
96  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
97  // ignore: a current map changed event will be generated
98  }
99 
100  @Override
101  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
102  // ignore: a current map changed event will be generated
103  }
104 
105  @Override
106  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
107  // ignore: a current map changed event will be generated
108  }
109 
110  };
111  }
112 
120  public ExportMapAsImageAction(@NotNull final MapManager<G, A, R> mapManager, @NotNull final RendererFactory<G, A, R> rendererFactory, @NotNull final JFrame parent) {
121  this.mapManager = mapManager;
122  this.rendererFactory = rendererFactory;
123  this.parent = parent;
126  }
127 
131  @ActionMethod
132  public void exportMapAsImage() {
133  doExportMap(true);
134  }
135 
136  @Override
137  public void setAction(@NotNull final Action action, @NotNull final String name) {
138  this.action = action;
139  updateActions();
140  }
141 
147  private boolean doExportMap(final boolean performAction) {
148  final MapControl<G, A, R> mapControl = currentMapControl;
149  if (mapControl == null) {
150  return false;
151  }
152 
153  if (mapControl.getMapModel().getMapFile() == null) {
154  return false;
155  }
156 
157  if (performAction) {
158  new ExportMapAsImageDialog<>(parent, this, mapControl);
159  }
160 
161  return true;
162  }
163 
167  private void updateActions() {
168  if (action != null) {
169  action.setEnabled(doExportMap(false));
170  }
171  }
172 
181  public boolean exportMap(@NotNull final MapControl<G, A, R> mapControl, final boolean includeTiledMaps, @NotNull final File file, @NotNull final ExportMapProgress progress) {
182  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
183  final MapFile mapFile = mapModel.getMapFile();
184  if (mapFile == null) {
185  return false;
186  }
187 
189  final BufferedImage image = collector.createImage(mapFile, includeTiledMaps, progress);
190  if (image == null) {
191  return false;
192  }
193 
194  try {
195  ImageIO.write(image, "png", file);
196  } catch (final IOException ex) {
197  System.err.println(file + ": " + ex);
198  return false;
199  }
200 
201  return true;
202  }
203 
204 }
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.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta.gui.dialog.exportmap.ExportMapAsImageDialog
Dialog to export a new map file into an image file.
Definition: ExportMapAsImageDialog.java:66
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.action.exportmap.MapImageCreator.createImage
BufferedImage createImage(@NotNull final MapFile mapFile, final boolean includeTiledMaps, @NotNull final ExportMapProgress progress)
Creates an image from a map file.
Definition: MapImageCreator.java:137
net.sf
net.sf.gridarta.model.mapmanager.MapManagerListener
Interface for listeners listening to MapManager changes.
Definition: MapManagerListener.java:42
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
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.action.exportmap.MapImageCreator
Creates images from maps.
Definition: MapImageCreator.java:56
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.action.exportmap.ExportMapAsImageAction.ExportMapAsImageAction
ExportMapAsImageAction(@NotNull final MapManager< G, A, R > mapManager, @NotNull final RendererFactory< G, A, R > rendererFactory, @NotNull final JFrame parent)
Creates a new instance.
Definition: ExportMapAsImageAction.java:120
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.rendererFactory
final RendererFactory< G, A, R > rendererFactory
The RendererFactory for creating images from maps.
Definition: ExportMapAsImageAction.java:59
net.sf.gridarta.gui.dialog.exportmap
The dialog for exporting maps as image files.
Definition: ExportMapAsImageDialog.java:20
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.setAction
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Definition: ExportMapAsImageAction.java:137
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.doExportMap
boolean doExportMap(final boolean performAction)
Executes the "validate map" action.
Definition: ExportMapAsImageAction.java:147
net.sf.gridarta.action.exportmap.ExportMapProgress
Interface for receiving progress information while the map is exported.
Definition: ExportMapProgress.java:28
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.action
Action action
The Action associated with this editor action.
Definition: ExportMapAsImageAction.java:72
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.updateActions
void updateActions()
Updates the state of the action.
Definition: ExportMapAsImageAction.java:167
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.dialog
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.action.exportmap.ExportMapAsImageAction.exportMap
boolean exportMap(@NotNull final MapControl< G, A, R > mapControl, final boolean includeTiledMaps, @NotNull final File file, @NotNull final ExportMapProgress progress)
Exports a map as an image file.
Definition: ExportMapAsImageAction.java:181
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.map.renderer.RendererFactory
Factory for creating MapRenderer instances.
Definition: RendererFactory.java:33
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.parent
final JFrame parent
The parent frame for export map dialogs.
Definition: ExportMapAsImageAction.java:65
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.action.exportmap.ExportMapAsImageAction.exportMapAsImage
void exportMapAsImage()
Exports the current map as an image file.
Definition: ExportMapAsImageAction.java:132
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.currentMapControl
MapControl< G, A, R > currentMapControl
The last known active map, or.
Definition: ExportMapAsImageAction.java:78
net.sf.gridarta.utils.EditorAction
A global editor action.
Definition: EditorAction.java:29
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.newMapManagerListener
MapManagerListener< G, A, R > newMapManagerListener()
Creates a new MapManagerListener that refreshes the actions when the current map changes.
Definition: ExportMapAsImageAction.java:86
net.sf.gridarta.action.exportmap.ExportMapAsImageAction.mapManager
final MapManager< G, A, R > mapManager
The MapManager for loading maps.
Definition: ExportMapAsImageAction.java:53
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.action.exportmap.ExportMapAsImageAction
An EditorAction that runs the map export on the current map.
Definition: ExportMapAsImageAction.java:47