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-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.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;
53 import net.sf.gridarta.utils.Size2D;
54 import net.sf.japi.swing.action.ActionBuilder;
55 import net.sf.japi.swing.action.ActionBuilderFactory;
56 import org.apache.log4j.Category;
57 import org.apache.log4j.Logger;
58 import org.jetbrains.annotations.NotNull;
59 import org.jetbrains.annotations.Nullable;
60 
65 public class DefaultMapView<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractMapView<G, A, R> {
66 
70  @NotNull
71  private static final Category LOG = Logger.getLogger(DefaultMapView.class);
72 
76  @NotNull
77  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
78 
82  @NotNull
84 
88  @NotNull
89  private final JInternalFrame internalFrame;
90 
94  private final int number;
95 
99  @NotNull
100  private final PathManager pathManager;
101 
105  @NotNull
107 
111  @NotNull
113 
117  @NotNull
119 
124  @NotNull
126 
127  @Override
128  public void mapSizeChanged(@NotNull final Size2D newSize) {
129  // ignore
130  }
131 
132  @Override
133  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
134  // ignore
135  }
136 
137  @Override
138  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
139  // ignore
140  }
141 
142  @Override
143  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
144  // ignore
145  }
146 
147  @Override
148  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
149  updateTitle();
150  }
151 
152  @Override
153  public void modifiedChanged() {
154  updateTitle();
155  }
156 
157  };
158 
163  @NotNull
165 
166  @Override
167  public void mapMetaChanged() {
168  updateTitle();
169  }
170 
171  @Override
172  public void mapSizeChanged(@NotNull final Size2D mapSize) {
173  // ignore
174  }
175 
176  };
177 
182  @NotNull
184 
185  @Override
186  public void saved(@NotNull final DefaultMapControl<G, A, R> mapControl) {
187  updateTitle();
188  }
189 
190  };
191 
205  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, @Nullable final Point viewPosition, final int xScrollDistance, final int yScrollDistance) {
206  super(mapControl.getMapModel(), mapGrid, mapCursor);
207  internalFrame = new JInternalFrame(getWindowTitle(mapControl, number, pathManager), true, true, true, true);
208  this.mapControl = mapControl;
209  this.number = number;
210  this.pathManager = pathManager;
211  this.renderer = renderer;
212  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
213  erroneousMapSquares = new ErroneousMapSquares<>(mapModel, mapGrid, renderer);
214  mapCursorTracker = new MapCursorTracker<>(mapControl.isPickmap(), viewPosition, xScrollDistance, yScrollDistance, mapCursor, renderer);
215  mapModel.addMapModelListener(mapModelListener);
216  mapModel.getMapArchObject().addMapArchObjectListener(mapArchObjectListener);
217  mapControl.addMapControlListener(mapControlListener);
218  internalFrame.getContentPane().setLayout(new BorderLayout());
219  internalFrame.getContentPane().add(mapCursorTracker.getScrollPane(), BorderLayout.CENTER);
220  internalFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
221  internalFrame.setAutoscrolls(true);
222  internalFrame.setFocusable(true);
223  for (final Direction direction : Direction.values()) {
224  installAccelerator("moveCursor" + direction.getId());
225  }
226  installAccelerator("moveSquarePrev");
227  installAccelerator("moveSquareNext");
228  installAccelerator("moveSquareUp");
229  installAccelerator("moveSquareDown");
230  installAccelerator("moveSquareInv");
231  installAccelerator("moveSquareEnv");
232  }
233 
238  private void installAccelerator(@NotNull final String key) {
239  final Action action = ACTION_BUILDER.getAction(key);
240  if (action != null) {
241  final KeyStroke keyStroke1 = ActionUtils.getShortcut(action);
242  if (keyStroke1 != null) {
243  internalFrame.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke1, key);
244  }
245  final KeyStroke keyStroke2 = ActionUtils.getAlternativeShortcut(action);
246  if (keyStroke2 != null) {
247  internalFrame.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(keyStroke2, key);
248  }
249  internalFrame.getActionMap().put(key, action);
250  }
251  }
252 
253  @Override
254  public void closeNotify() {
255  internalFrame.getContentPane().remove(mapCursorTracker.getScrollPane());
256  mapCursorTracker.closeNotify();
257  erroneousMapSquares.closeNotify();
258  renderer.closeNotify();
259  mapControl.removeMapControlListener(mapControlListener);
260  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
261  mapModel.getMapArchObject().removeMapArchObjectListener(mapArchObjectListener);
262  mapModel.removeMapModelListener(mapModelListener);
263  //mapFileActions.closeNotify();
264  MenuUtils.disposeMenuElement(internalFrame.getJMenuBar());
265  }
266 
270  private void updateTitle() {
271  internalFrame.setTitle(getWindowTitle());
272  }
273 
274  @NotNull
275  @Override
276  public String getWindowTitle() {
277  return getWindowTitle(mapControl, number, pathManager);
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;
291  if (mapFile == null) {
292  mapPath = "<unsaved>";
293  } else {
294  mapPath = mapFile.getMapPath().toString();
295  }
296  return mapPath + " [ " + mapControl.getMapModel().getMapArchObject().getMapName() + " ] (" + number + ")" + (mapControl.getMapModel().isModified() ? " *" : "");
297  }
298 
299  @NotNull
300  @Override
301  public Component getComponent() {
302  return internalFrame;
303  }
304 
305  @NotNull
306  @Override
308  return mapControl;
309  }
310 
311  @Override
312  public void activate() {
313  try {
314  internalFrame.setSelected(true);
315  } catch (final PropertyVetoException e) {
316  LOG.warn("Unexpected exception", e);
317  }
318  internalFrame.setVisible(true);
319  internalFrame.requestFocus();
320  internalFrame.restoreSubcomponentFocus();
321  }
322 
323  @NotNull
324  @Override
325  public JInternalFrame getInternalFrame() {
326  return internalFrame;
327  }
328 
329  @NotNull
330  @Override
332  return renderer;
333  }
334 
335  @NotNull
336  @Override
337  public JScrollPane getScrollPane() {
338  return mapCursorTracker.getScrollPane();
339  }
340 
341 }
final PathManager pathManager
The PathManager for converting path names.
Interface for listeners listening on map arch object changes.
static KeyStroke getAlternativeShortcut(@NotNull final Action action)
Returns the alternative shortcut of an Action.
final JInternalFrame internalFrame
The JInternalFrame instance associated with this map view.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
void updateTitle()
Update the Map-Window Title (according to name and changeFlag).
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
This package contains the framework for validating maps.
AbsoluteMapPath getMapPath()
Returns the map path within getMapsDir().
Definition: MapFile.java:93
final MapArchObjectListener mapArchObjectListener
The MapArchObjectListener used to detect changes in the map model that should be reflected in the win...
Interface for listeners listening on MapModel events.
Utility class implementing Action related functions.
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
final MapControl< G, A, R > mapControl
The controller of this view.
final MapCursor< G, A, R > mapCursor
The MapCursor of this map view.
MapModel< G, A, R > getMapModel()
Returns the map model.
void closeNotify()
Must be called when this instance is not used anymore.
Base package of all Gridarta classes.
Abstract base class for classes implementing MapRenderer.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final ErroneousMapSquares< G, A, R > erroneousMapSquares
The erroneous MapSquares.
final AbstractMapRenderer< G, A, R > renderer
The AbstractMapRenderer for rendering the map model.
Utility class implementing menu related functions.
Definition: MenuUtils.java:39
final MapCursorTracker< G, A, R > mapCursorTracker
The erroneous MapSquares.
Interface for listeners listening on changes in MapControl instances.
abstract void closeNotify()
Must be called when this renderer is not used anymore.
GameObjects are the objects based on Archetypes found on maps.
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
final MapModel< G, A, R > mapModel
The MapModel of this map view.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
void installAccelerator(@NotNull final String key)
Installs accelerator keys for an action.
JScrollPane getScrollPane()
Returns the JScrollPane of the renderer.
Base classes for rendering maps.
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
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
static KeyStroke getShortcut(@NotNull final Action action)
Returns the shortcut of an Action.
static final Category LOG
The Logger for printing log messages.
void closeNotify()
Must be called when this instance is not used anymore.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
final MapGrid mapGrid
The MapGrid of this map view.
Abstract base class for MapView implementations.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
final MapControlListener< G, A, R > mapControlListener
The MapControlListener used to detect changes in the map control that should be reflected in the wind...
static String getWindowTitle(@NotNull final MapControl<?, ?, ?> mapControl, final int number, @NotNull final PathManager pathManager)
Returns the title for the map window.
An interface for classes that collect errors.
Common interface for renderers of map control instances.
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, @Nullable final Point viewPosition, final int xScrollDistance, final int yScrollDistance)
Create a new instance.
final MapModelListener< G, A, R > mapModelListener
The MapModelListener used to detect changes in the map model that should be reflected in the window t...
The location of a map file with a map directory.
Definition: MapFile.java:31
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
void removeMapControlListener(@NotNull MapControlListener< G, A, R > listener)
Unregisters a MapControlListener.