Gridarta Editor
GameObjectTextEditorTab.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.mainwindow;
21 
22 import java.awt.event.FocusEvent;
23 import java.awt.event.FocusListener;
24 import java.util.Set;
43 import net.sf.gridarta.utils.Size2D;
44 import org.jetbrains.annotations.NotNull;
45 import org.jetbrains.annotations.Nullable;
46 
51 public class GameObjectTextEditorTab<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends Tab {
52 
56  @NotNull
58 
62  @Nullable
64 
69 
73  @Nullable
74  private G selectedGameObject;
75 
80  private boolean isInMapTransaction;
81 
85  @NotNull
87 
88  @Override
89  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
90  if (currentMapControl != null) {
91  final MapModel<G, A, R> mapModel = currentMapControl.getMapModel();
94  }
95  currentMapControl = mapControl;
96  if (currentMapControl != null) {
97  final MapModel<G, A, R> mapModel = currentMapControl.getMapModel();
100  }
101  }
102 
103  @Override
104  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
105  // ignore
106  }
107 
108  @Override
109  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
110  // ignore
111  }
112 
113  @Override
114  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
115  // ignore
116  }
117 
118  };
119 
125  @NotNull
127 
128  @Override
129  public void preBeginTransaction() {
131  isInMapTransaction = true;
132  }
133 
134  @Override
135  public void beginTransaction(@NotNull final String name) {
136  // ignore
137  }
138 
139  @Override
140  public void endTransaction(@NotNull final SavedSquares<G, A, R> savedSquares) {
141  // ignore
142  }
143 
144  @Override
145  public void postEndTransaction() {
146  isInMapTransaction = false;
147  }
148 
149  };
150 
154  @NotNull
156 
157  @Override
158  public void mapSizeChanged(@NotNull final Size2D newSize) {
159  // ignore
160  }
161 
162  @Override
163  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
164  if (selectedGameObject == null) {
165  return;
166  }
167 
168  final G topGameObject = selectedGameObject.getTopContainer();
169  for (final Iterable<G> mapSquare : mapSquares) {
170  for (final G gameObject : mapSquare) {
171  if (gameObject == topGameObject) {
172  refreshDisplay();
173  }
174  }
175  }
176  }
177 
178  @Override
179  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
180  for (final GameObject<G, A, R> gameObject : gameObjects) {
181  if (selectedGameObject == gameObject.getHead()) {
182  refreshDisplay();
183  }
184  }
185  }
186 
187  @Override
188  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
189  // ignore
190  }
191 
192  @Override
193  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
194  // ignore
195  }
196 
197  @Override
198  public void modifiedChanged() {
199  // ignore
200  }
201 
202  };
203 
216  public GameObjectTextEditorTab(@NotNull final String ident, @NotNull final GameObjectTextEditor gameObjectTextEditor, @NotNull final Location defaultLocation, final boolean alternativeLocation, final int index, final boolean defaultOpen, @NotNull final SelectedSquareModel<G, A, R> selectedSquareModel, @NotNull final MapManager<G, A, R> mapManager) {
217  super(ident, gameObjectTextEditor, defaultLocation, alternativeLocation, index, defaultOpen);
218  this.gameObjectTextEditor = gameObjectTextEditor;
219  selectedSquareModel.addSelectedSquareListener(new SelectedSquareModelListener<G, A, R>() {
220 
221  @Override
222  public void selectionChanged(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
224  selectedGameObject = gameObject;
225  refreshDisplay();
226  }
227 
228  });
229  currentMapControl = mapManager.getCurrentMap();
230  if (currentMapControl != null) {
231  final MapModel<G, A, R> mapModel = currentMapControl.getMapModel();
232  mapModel.addMapModelListener(mapModelListener);
233  mapModel.addMapTransactionListener(mapTransactionListener);
234  }
235  mapManager.addMapManagerListener(mapManagerListener);
236  selectedGameObject = selectedSquareModel.getSelectedGameObject();
237  refreshDisplay();
238  gameObjectTextEditor.getTextPane().addFocusListener(new FocusListener() {
239 
240  @Override
241  public void focusGained(final FocusEvent e) {
242  // ignore
243  }
244 
245  @Override
246  public void focusLost(final FocusEvent e) {
248  }
249 
250  });
251  }
252 
256  private void autoApplyArchPanelChanges() {
257  final GameObject<G, A, R> gameObject = selectedGameObject;
258  if (gameObject == null || isInAutoApplyArchPanelChanges || isInMapTransaction) {
259  return;
260  }
261 
262  final MapSquare<G, A, R> mapSquare = gameObject.getMapSquare();
263  if (mapSquare == null) {
264  return;
265  }
266 
267  final MapModel<G, A, R> mapModel = mapSquare.getMapModel();
268 
269  isInAutoApplyArchPanelChanges = true;
270  try {
271  mapModel.beginTransaction("Change object attributes");
272  try {
273  gameObjectTextEditor.applyChanges(gameObject);
274  } finally {
275  mapModel.endTransaction();
276  }
277  } finally {
278  isInAutoApplyArchPanelChanges = false;
279  }
280  }
281 
285  private void refreshDisplay() {
286  setSeverity(gameObjectTextEditor.refreshDisplay(selectedGameObject));
287  }
288 
289 }
void removeMapTransactionListener(@NotNull MapTransactionListener< G, A, R > listener)
Unregisters a map transaction listener.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
This package contains the framework for validating maps.
void endTransaction()
End a transaction.
A tab in a TabbedPanel component.
Definition: Tab.java:53
void applyChanges(@NotNull final GameObject<?, ?, ?> gameObject)
Updates a GameObject&#39;s attributes from the input field.
Interface for listeners listening on MapModel events.
Interface for listeners interested in events of SelectedSquareModel instances.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
Severity refreshDisplay(@Nullable final GameObject<?, ?, ?> gameObject)
Refreshes the input field to show the attributes of a GameObject.
boolean isInAutoApplyArchPanelChanges
Whether autoApplyArchPanelChanges() is currently running.
MapModel< G, A, R > getMapModel()
Returns the map model.
Base package of all Gridarta classes.
Interface for listeners listening on map transactions of MapModels.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void addMapTransactionListener(@NotNull MapTransactionListener< G, A, R > listener)
Registers a map transaction listener.
final MapTransactionListener< G, A, R > mapTransactionListener
The map transaction listener which is attached to currentMapControl.
final GameObjectTextEditor gameObjectTextEditor
The displayed GameObjectTextEditor instance.
Interface for listeners listening to MapManager changes.
final String ident
The tab&#39;s identification string.
Definition: Tab.java:105
GameObjects are the objects based on Archetypes found on maps.
void refreshDisplay()
Refreshes the tab&#39;s state from a selectedGameObject.
Displays the contents of the currently selected map square.
boolean alternativeLocation
Whether the tab is shown in the alternative location (.
Definition: Tab.java:159
The Tab displaying the game object text editor.
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
Records a set of changed map squares.
GameObjectTextEditorTab(@NotNull final String ident, @NotNull final GameObjectTextEditor gameObjectTextEditor, @NotNull final Location defaultLocation, final boolean alternativeLocation, final int index, final boolean defaultOpen, @NotNull final SelectedSquareModel< G, A, R > selectedSquareModel, @NotNull final MapManager< G, A, R > mapManager)
Creates a new instance.
An interface for classes that collect errors.
final MapModelListener< G, A, R > mapModelListener
The map model listener which is attached to currentMapControl.
void beginTransaction(@NotNull String name)
Starts a new transaction.
The model component of the selected square control.
final MapManagerListener< G, A, R > mapManagerListener
The map manager listener.
boolean isInMapTransaction
Records whether a map transaction is active.
void setSeverity(@NotNull final Severity severity)
Sets the tab&#39;s Severity.
Definition: Tab.java:436
final int index
The tab&#39;s index for ordering.
Definition: Tab.java:164
The location of a map file with a map directory.
Definition: MapFile.java:31
MapControl< G, A, R > currentMapControl
Last known active map.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
MapSquare< G, A, R > getMapSquare()
Get the MapSquare of this GameObjectContainer.