Gridarta Editor
WindowAction.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.mapdesktop;
21 
22 import java.awt.event.ActionEvent;
23 import java.util.Set;
24 import javax.swing.AbstractAction;
25 import javax.swing.KeyStroke;
37 import net.sf.gridarta.utils.Size2D;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
45 public class WindowAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractAction {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
57 
61  @NotNull
62  private final MapView<G, A, R> mapView;
63 
67  @NotNull
69 
73  private int index = -1;
74 
79 
80  @Override
81  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
82  // ignore
83  }
84 
85  @Override
86  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
87  // ignore
88  }
89 
90  @Override
91  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
92  // ignore
93  }
94 
95  @Override
96  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
97  if (mapControl == mapView.getMapControl()) {
99  mapManager.removeMapManagerListener(mapManagerListener);
100  }
101  }
102 
103  };
104 
109 
110  @Override
111  public void mapSizeChanged(@NotNull final Size2D newSize) {
112  // ignore
113  }
114 
115  @Override
116  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
117  // ignore
118  }
119 
120  @Override
121  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
122  // ignore
123  }
124 
125  @Override
126  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
127  // ignore
128  }
129 
130  @Override
131  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
132  // ignore
133  }
134 
135  @Override
136  public void modifiedChanged() {
137  updateName();
138  }
139 
140  };
141 
148  public WindowAction(@NotNull final MapDesktop<G, A, R> mapDesktop, @NotNull final MapView<G, A, R> mapView, @NotNull final MapManager<G, A, R> mapManager) {
149  this.mapDesktop = mapDesktop;
150  this.mapView = mapView;
151  this.mapManager = mapManager;
152  putValue(SHORT_DESCRIPTION, "Switches to map " + mapView.getInternalFrame().getTitle());
153  mapView.getMapControl().getMapModel().addMapModelListener(mapModelListener);
154  mapManager.addMapManagerListener(mapManagerListener);
155  setIndex(-1);
156  }
157 
163  public final void setIndex(final int index) {
164  this.index = index;
165  if (0 <= index && index <= 10) {
166  putValue(MNEMONIC_KEY, KeyStroke.getKeyStroke(Integer.toString(index % 10)).getKeyCode());
167  putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl pressed " + index % 10));
168  } else {
169  putValue(MNEMONIC_KEY, null);
170  putValue(ACCELERATOR_KEY, null);
171  }
172  updateName();
173  }
174 
178  private void updateName() {
179  final String windowTitle = mapView.getWindowTitle();
180  if (0 <= index && index <= 10) {
181  putValue(NAME, index + ": " + windowTitle);
182  } else {
183  putValue(NAME, windowTitle);
184  }
185  }
186 
187  @Override
188  public void actionPerformed(@NotNull final ActionEvent e) {
189  mapDesktop.setCurrentMapView(mapView);
190  }
191 
192  @NotNull
193  @Override
194  public Object clone() {
195  try {
196  return super.clone();
197  } catch (final CloneNotSupportedException ex) {
198  throw new AssertionError(ex);
199  }
200  }
201 
202 }
void updateName()
Updates the action&#39;s name to the current map state.
final MapManagerListener< G, A, R > mapManagerListener
The MapManagerListener to detect closed maps.
void removeMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Removes a MapManagerListener to be notified.
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
This package contains the framework for validating maps.
final MapView< G, A, R > mapView
The associated map view.
Interface for listeners listening on MapModel events.
void actionPerformed(@NotNull final ActionEvent e)
MapControl< G, A, R > getMapControl()
Return the controller of this view.
MapModel< G, A, R > getMapModel()
Returns the map model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Action class for selecting this window.
String getWindowTitle()
Returns the title for the map window.
static final long serialVersionUID
The serial version UID.
Interface for listeners listening to MapManager changes.
GameObjects are the objects based on Archetypes found on maps.
WindowAction(@NotNull final MapDesktop< G, A, R > mapDesktop, @NotNull final MapView< G, A, R > mapView, @NotNull final MapManager< G, A, R > mapManager)
Create a new instance.
Base classes for rendering maps.
final void setIndex(final int index)
Set the index of this action so this Action knows what Mnemonic and Accelerator to use...
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
void addMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Adds a MapManagerListener to be notified.
void setCurrentMapView(@NotNull final MapView< G, A, R > mapView)
Sets the given level view as the current one.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
final MapManager< G, A, R > mapManager
The MapManager to use.
An interface for classes that collect errors.
final MapModelListener< G, A, R > mapModelListener
The MapModelListener to detect modified maps.
int index
The index of this action.
The location of a map file with a map directory.
Definition: MapFile.java:31
final MapDesktop< G, A, R > mapDesktop
The map desktop.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30