Gridarta Editor
DefaultMapView.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.mapview;
21 
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Point;
25 import java.beans.PropertyVetoException;
26 import java.util.Set;
27 import javax.swing.Action;
28 import javax.swing.JComponent;
29 import javax.swing.JInternalFrame;
30 import javax.swing.JScrollPane;
31 import javax.swing.KeyStroke;
32 import javax.swing.WindowConstants;
52 import net.sf.gridarta.utils.Size2D;
53 import net.sf.japi.swing.action.ActionBuilder;
54 import net.sf.japi.swing.action.ActionBuilderFactory;
55 import org.apache.log4j.Category;
56 import org.apache.log4j.Logger;
57 import org.jetbrains.annotations.NotNull;
58 import org.jetbrains.annotations.Nullable;
59 
64 public class DefaultMapView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractMapView<G, A, R> {
65 
69  @NotNull
70  private static final Category LOG = Logger.getLogger(DefaultMapView.class);
71 
75  @NotNull
76  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
77 
81  @NotNull
83 
87  @NotNull
88  private final JInternalFrame internalFrame;
89 
93  private final int number;
94 
98  @NotNull
99  private final PathManager pathManager;
100 
104  @NotNull
106 
110  @NotNull
112 
116  @NotNull
118 
123  @NotNull
125 
126  @Override
127  public void mapSizeChanged(@NotNull final Size2D newSize) {
128  // ignore
129  }
130 
131  @Override
132  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
133  // ignore
134  }
135 
136  @Override
137  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
138  // ignore
139  }
140 
141  @Override
142  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
143  // ignore
144  }
145 
146  @Override
147  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
148  updateTitle();
149  }
150 
151  @Override
152  public void modifiedChanged() {
153  updateTitle();
154  }
155 
156  };
157 
162  @NotNull
164 
165  @Override
166  public void mapMetaChanged() {
167  updateTitle();
168  }
169 
170  @Override
171  public void mapSizeChanged(@NotNull final Size2D mapSize) {
172  // ignore
173  }
174 
175  };
176 
181  @NotNull
183 
195  public DefaultMapView(@NotNull final MapControl<G, A, R> mapControl, final int number, @NotNull final PathManager pathManager, @NotNull final MapGrid mapGrid, @NotNull final MapCursor<G, A, R> mapCursor, @NotNull final AbstractMapRenderer<G, A, R> renderer, final int xScrollDistance, final int yScrollDistance) {
197  internalFrame = new JInternalFrame(getWindowTitle(mapControl, number, pathManager), true, true, true, true);
198  this.mapControl = mapControl;
199  this.number = number;
200  this.pathManager = pathManager;
201  this.renderer = renderer;
204  mapCursorTracker = new MapCursorTracker<>(mapControl.isPickmap(), xScrollDistance, yScrollDistance, mapCursor, renderer);
206  mapModel.getMapArchObject().addMapArchObjectListener(mapArchObjectListener);
208  internalFrame.getContentPane().setLayout(new BorderLayout());
209  internalFrame.getContentPane().add(mapCursorTracker.getScrollPane(), BorderLayout.CENTER);
210  internalFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
211  internalFrame.setAutoscrolls(true);
212  internalFrame.setFocusable(true);
213  for (final Direction direction : Direction.values()) {
214  installAccelerator("moveCursor" + direction.getId());
215  }
216  installAccelerator("moveSquarePrev");
217  installAccelerator("moveSquareNext");
218  installAccelerator("moveSquareUp");
219  installAccelerator("moveSquareDown");
220  installAccelerator("moveSquareInv");
221  installAccelerator("moveSquareEnv");
222  }
223 
228  @Override
229  public void setViewPosition(@NotNull final Point viewPosition) {
230  mapCursorTracker.setViewPosition(viewPosition);
231  }
232 
237  private void installAccelerator(@NotNull final String key) {
238  final Action action = ACTION_BUILDER.getAction(key);
239  if (action != null) {
240  final KeyStroke keyStroke1 = ActionUtils.getShortcut(action);
241  if (keyStroke1 != null) {
242  internalFrame.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke1, key);
243  }
244  final KeyStroke keyStroke2 = ActionUtils.getAlternativeShortcut(action);
245  if (keyStroke2 != null) {
246  internalFrame.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke2, key);
247  }
248  internalFrame.getActionMap().put(key, action);
249  }
250  }
251 
252  @Override
253  public void closeNotify() {
254  super.closeNotify();
255  internalFrame.getContentPane().remove(mapCursorTracker.getScrollPane());
261  mapModel.getMapArchObject().removeMapArchObjectListener(mapArchObjectListener);
263  //mapFileActions.closeNotify();
265  }
266 
270  private void updateTitle() {
271  internalFrame.setTitle(getWindowTitle());
272  }
273 
274  @NotNull
275  @Override
276  public String getWindowTitle() {
278  }
279 
287  @NotNull
288  private static String getWindowTitle(@NotNull final MapControl<?, ?, ?> mapControl, final int number, @NotNull final PathManager pathManager) {
289  final MapFile mapFile = mapControl.getMapModel().getMapFile();
290  final String mapPath = mapFile == null ? "<unsaved>" : mapFile.getMapPath().toString();
291  return mapPath + " [ " + mapControl.getMapModel().getMapArchObject().getMapName() + " ] (" + number + ")" + (mapControl.getMapModel().isModified() ? " *" : "");
292  }
293 
294  @NotNull
295  @Override
296  public Component getComponent() {
297  return internalFrame;
298  }
299 
300  @NotNull
301  @Override
303  return mapControl;
304  }
305 
306  @Override
307  public void activate() {
308  try {
309  internalFrame.setSelected(true);
310  } catch (final PropertyVetoException e) {
311  LOG.warn("Unexpected exception", e);
312  }
313  internalFrame.setVisible(true);
314  internalFrame.requestFocus();
315  internalFrame.restoreSubcomponentFocus();
316  }
317 
318  @NotNull
319  @Override
320  public JInternalFrame getInternalFrame() {
321  return internalFrame;
322  }
323 
324  @NotNull
325  @Override
327  return renderer;
328  }
329 
330  @NotNull
331  @Override
332  public JScrollPane getScrollPane() {
334  }
335 
336 }
net.sf.gridarta.gui.map.mapview.AbstractMapView< G, A, R >::mapGrid
final MapGrid mapGrid
The MapGrid of this map view.
Definition: AbstractMapView.java:61
net.sf.gridarta.model.direction.Direction
A direction.
Definition: Direction.java:28
net.sf.gridarta.gui.map.renderer.AbstractMapRenderer.closeNotify
abstract void closeNotify()
Must be called when this renderer is not used anymore.
net.sf.gridarta.gui.map.mapview.DefaultMapView.mapArchObjectListener
final MapArchObjectListener mapArchObjectListener
The MapArchObjectListener used to detect changes in the map model that should be reflected in the win...
Definition: DefaultMapView.java:163
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.gui.map.mapview.DefaultMapView.closeNotify
void closeNotify()
Definition: DefaultMapView.java:253
net.sf.gridarta.gui.map.mapview.DefaultMapView.mapControlListener
final MapControlListener< G, A, R > mapControlListener
The MapControlListener used to detect changes in the map control that should be reflected in the wind...
Definition: DefaultMapView.java:182
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.gui.map.mapview.AbstractMapView< G, A, R >::mapModel
final MapModel< G, A, R > mapModel
The MapModel of this map view.
Definition: AbstractMapView.java:55
net.sf.gridarta.gui.map.mapview.DefaultMapView.pathManager
final PathManager pathManager
The PathManager for converting path names.
Definition: DefaultMapView.java:99
net.sf.gridarta.gui.map.mapview.ErroneousMapSquares< G, A, R >
net.sf.gridarta.gui.map.mapview.DefaultMapView.getMapControl
MapControl< G, A, R > getMapControl()
Definition: DefaultMapView.java:302
net.sf.gridarta.gui.map.mapview.DefaultMapView.mapModelListener
final MapModelListener< G, A, R > mapModelListener
The MapModelListener used to detect changes in the map model that should be reflected in the window t...
Definition: DefaultMapView.java:124
net.sf.gridarta.gui.map.mapview.DefaultMapView.internalFrame
final JInternalFrame internalFrame
The JInternalFrame instance associated with this map view.
Definition: DefaultMapView.java:88
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
net.sf.gridarta.model.maparchobject.MapArchObjectListener
Interface for listeners listening on map arch object changes.
Definition: MapArchObjectListener.java:30
net.sf.gridarta.model.io.PathManager
This class contains methods for converting relative map paths to absolute map paths and vice versa.
Definition: PathManager.java:39
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.mapview.DefaultMapView.setViewPosition
void setViewPosition(@NotNull final Point viewPosition)
Sets the view position.
Definition: DefaultMapView.java:229
net.sf.gridarta.gui.map.renderer
Definition: AbstractIsoMapRenderer.java:20
net.sf.gridarta.gui.map.mapview.DefaultMapView.getWindowTitle
static String getWindowTitle(@NotNull final MapControl<?, ?, ?> mapControl, final int number, @NotNull final PathManager pathManager)
Returns the title for the map window.
Definition: DefaultMapView.java:288
net.sf.gridarta.gui.map.mapview.DefaultMapView.getComponent
Component getComponent()
Definition: DefaultMapView.java:296
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.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.map.mapview.DefaultMapView.activate
void activate()
Definition: DefaultMapView.java:307
net.sf.gridarta.gui.map.mapview.DefaultMapView.installAccelerator
void installAccelerator(@NotNull final String key)
Installs accelerator keys for an action.
Definition: DefaultMapView.java:237
net.sf.gridarta.model.mapmodel.MapFile.getMapPath
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
net.sf.gridarta.utils.ActionUtils
Utility class implementing Action related functions.
Definition: ActionUtils.java:33
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.utils.MenuUtils.disposeMenuElement
static void disposeMenuElement(@NotNull final MenuElement menuElement)
Remove all actions attached to menu entries in a given menu element and its children.
Definition: MenuUtils.java:58
net.sf.gridarta.gui.map.renderer.AbstractMapRenderer
Abstract base class for classes implementing MapRenderer.
Definition: AbstractMapRenderer.java:45
net.sf.gridarta.gui.map.mapview.DefaultMapView.getWindowTitle
String getWindowTitle()
Definition: DefaultMapView.java:276
net.sf.gridarta.gui.map.mapview.DefaultMapView.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Definition: DefaultMapView.java:76
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.map.mapview.DefaultMapView.number
final int number
View number.
Definition: DefaultMapView.java:93
net
net.sf.gridarta.model.mapcontrol.MapControl.isPickmap
boolean isPickmap()
Return flag that indicates whether this is a pickmap or not.
errors
errors
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:1
net.sf.gridarta.utils.ActionUtils.getShortcut
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
Definition: ActionUtils.java:120
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.MapCursorTracker.closeNotify
void closeNotify()
Must be called when this instance is not used anymore.
Definition: MapCursorTracker.java:129
net.sf.gridarta.model.validation.ErrorCollector
An interface for classes that collect errors.
Definition: ErrorCollector.java:33
net.sf.gridarta.gui.map.mapview.DefaultMapView.updateTitle
void updateTitle()
Updates the Map-Window Title (according to name and changeFlag).
Definition: DefaultMapView.java:270
net.sf.gridarta.gui.map.mapview.DefaultMapView.getInternalFrame
JInternalFrame getInternalFrame()
Definition: DefaultMapView.java:320
net.sf.gridarta.gui.map.mapview.DefaultMapView.mapCursorTracker
final MapCursorTracker< G, A, R > mapCursorTracker
The erroneous MapSquares.
Definition: DefaultMapView.java:117
net.sf.gridarta.model.validation
This package contains the framework for validating maps.
Definition: AbstractValidator.java:20
net.sf.gridarta.model.mapmodel.AbsoluteMapPath.toString
String toString()
Definition: AbsoluteMapPath.java:117
net.sf.gridarta.model.mapcursor.MapCursor
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:58
net.sf.gridarta.gui.map.mapview.DefaultMapView.getScrollPane
JScrollPane getScrollPane()
Definition: DefaultMapView.java:332
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.utils.ActionUtils.getAlternativeShortcut
static KeyStroke getAlternativeShortcut(@NotNull final Action action)
Returns the alternative shortcut of an Action.
Definition: ActionUtils.java:130
net.sf.gridarta.model.mapmodel.MapModelListener
Interface for listeners listening on MapModel events.
Definition: MapModelListener.java:36
net.sf.gridarta.model.mapgrid.MapGrid
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:46
net.sf.gridarta.gui.map.mapview.DefaultMapView
Default MapView implementation.
Definition: DefaultMapView.java:64
net.sf.gridarta.model.mapcontrol.MapControl.addMapControlListener
void addMapControlListener(@NotNull MapControlListener< G, A, R > listener)
Registers a MapControlListener.
net.sf.gridarta.gui.map.mapview.DefaultMapView.LOG
static final Category LOG
The Logger for printing log messages.
Definition: DefaultMapView.java:70
net.sf.gridarta.model.mapgrid
Definition: MapGrid.java:20
net.sf.gridarta.gui.map.mapview.MapCursorTracker.getScrollPane
JScrollPane getScrollPane()
Returns the JScrollPane of the renderer.
Definition: MapCursorTracker.java:147
net.sf.gridarta.gui.map.mapview.DefaultMapView.erroneousMapSquares
final ErroneousMapSquares< G, A, R > erroneousMapSquares
The erroneous MapSquares.
Definition: DefaultMapView.java:111
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.gui.map.mapview.AbstractMapView
Abstract base class for MapView implementations.
Definition: AbstractMapView.java:49
net.sf.gridarta.gui.map.mapview.ErroneousMapSquares.closeNotify
void closeNotify()
Must be called when this instance is not used anymore.
Definition: ErroneousMapSquares.java:129
net.sf.gridarta.gui.map.mapview.DefaultMapView.DefaultMapView
DefaultMapView(@NotNull final MapControl< G, A, R > mapControl, final int number, @NotNull final PathManager pathManager, @NotNull final MapGrid mapGrid, @NotNull final MapCursor< G, A, R > mapCursor, @NotNull final AbstractMapRenderer< G, A, R > renderer, final int xScrollDistance, final int yScrollDistance)
Creates a new instance.
Definition: DefaultMapView.java:195
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.MapControlListener
Interface for listeners listening on changes in MapControl instances.
Definition: MapControlListener.java:33
net.sf.gridarta.model.mapcontrol.MapControl.removeMapControlListener
void removeMapControlListener(@NotNull MapControlListener< G, A, R > listener)
Unregisters a MapControlListener.
net.sf.gridarta.gui.map.mapview.DefaultMapView.renderer
final AbstractMapRenderer< G, A, R > renderer
The AbstractMapRenderer for rendering the map model.
Definition: DefaultMapView.java:105
net.sf.gridarta.gui.map.mapview.MapCursorTracker< G, A, R >
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.gui.utils
Definition: AnimationComponent.java:20
net.sf.gridarta.gui.map.renderer.MapRenderer
Common interface for renderers of map control instances.
Definition: MapRenderer.java:36
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.gui.map.mapview.DefaultMapView.getRenderer
MapRenderer getRenderer()
Definition: DefaultMapView.java:326
net.sf.gridarta.model.mapcursor
Definition: MapCursor.java:20
net.sf.gridarta.gui.utils.MenuUtils
Utility class implementing menu related functions.
Definition: MenuUtils.java:39
net.sf.gridarta.gui.map.mapview.MapCursorTracker.setViewPosition
void setViewPosition(@NotNull final Point viewPosition)
Sets the view position.
Definition: MapCursorTracker.java:121
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.gui.map.mapview.DefaultMapView.mapControl
final MapControl< G, A, R > mapControl
The controller of this view.
Definition: DefaultMapView.java:82
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.direction
Definition: Direction.java:20
net.sf.gridarta.gui.map.mapview.AbstractMapView< G, A, R >::mapCursor
final MapCursor< G, A, R > mapCursor
The MapCursor of this map view.
Definition: AbstractMapView.java:67