Gridarta Editor
MapViewManager.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.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
38 public class MapViewManager<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterable<MapView<G, A, R>> {
39 
44  @NotNull
45  private final List<MapView<G, A, R>> mapViewsList = Collections.synchronizedList(new ArrayList<>());
46 
50  @Nullable
52 
56  @NotNull
57  private final Collection<MapViewManagerListener<G, A, R>> listeners = new ArrayList<>();
58 
63  @SuppressWarnings("NullableProblems")
64  public void setActiveMapView(@NotNull final MapView<G, A, R> mapView) {
65  assert mapViewsList.contains(mapView);
66  mapViewsList.remove(mapView);
67  mapViewsList.add(0, mapView);
69  }
70 
76  public void removeMapView(@NotNull final MapView<G, A, R> mapView) {
77  assert mapViewsList.contains(mapView);
78  if (!mapViewsList.contains(mapView)) {
79  return;
80  }
81 
82  mapViewsList.remove(mapView);
83  for (final MapViewManagerListener<G, A, R> listener : listeners) {
84  listener.mapViewClosing(mapView);
85  }
87  }
88 
93  public void addMapView(@NotNull final MapView<G, A, R> mapView) {
94  assert !mapViewsList.contains(mapView);
95  mapViewsList.add(0, mapView);
96 
97  for (final MapViewManagerListener<G, A, R> listener : listeners) {
98  listener.mapViewCreated(mapView);
99  }
100  }
101 
106  public void deactivateMapView(@NotNull final MapView<G, A, R> mapView) {
107  assert mapViewsList.contains(mapView);
108  if (mapViewsList.size() <= 1) {
109  return;
110  }
111 
112  mapViewsList.remove(mapView);
113  mapViewsList.add(mapView);
115  }
116 
121  public void activateMapView(@NotNull final MapView<G, A, R> mapView) {
122  assert mapViewsList.contains(mapView);
123  mapViewsList.remove(mapView);
124  mapViewsList.add(0, mapView);
126  }
127 
132  @NotNull
133  @Override
134  public Iterator<MapView<G, A, R>> iterator() {
135  return Collections.unmodifiableList(mapViewsList).iterator();
136  }
137 
141  private void updateActiveMapView() {
142  final MapView<G, A, R> newActiveMapView = mapViewsList.isEmpty() ? null : mapViewsList.get(0);
143  if (activeMapView == newActiveMapView) {
144  return;
145  }
146 
147  activeMapView = newActiveMapView;
148  for (final MapViewManagerListener<G, A, R> listener : listeners) {
149  listener.activeMapViewChanged(activeMapView);
150  }
151  }
152 
157  public void addMapViewManagerListener(@NotNull final MapViewManagerListener<G, A, R> listener) {
158  listeners.add(listener);
159  }
160 
165  public void removeMapViewManagerListener(@NotNull final MapViewManagerListener<G, A, R> listener) {
166  listeners.remove(listener);
167  }
168 
174  @Nullable
176  return activeMapView;
177  }
178 
184  public boolean doPrevWindow(final boolean performAction) {
185  if (mapViewsList.size() <= 1) {
186  return false;
187  }
188 
189  if (performAction) {
190  // XXX I might work I might not work, use AWT/Swing focus traversal
191  final MapView<G, A, R> mapView = mapViewsList.remove(0);
192  mapViewsList.add(mapView);
194  }
195 
196  return true;
197  }
198 
204  public boolean doNextWindow(final boolean performAction) {
205  final int size = mapViewsList.size();
206  if (size <= 1) {
207  return false;
208  }
209 
210  if (performAction) {
211  // XXX I might work I might not work, use AWT/Swing focus traversal
212  final MapView<G, A, R> mapView = mapViewsList.remove(size - 1);
213  mapViewsList.add(0, mapView);
215  }
216 
217  return true;
218  }
219 
220 }
boolean doNextWindow(final boolean performAction)
Executes the "next window" action.
void updateActiveMapView()
Updates the active map view and notifies all listeners of changes.
void addMapViewManagerListener(@NotNull final MapViewManagerListener< G, A, R > listener)
Adds a listener to be notified.
void addMapView(@NotNull final MapView< G, A, R > mapView)
Adds a map view.
boolean doPrevWindow(final boolean performAction)
Executes the "prev window" action.
void removeMapView(@NotNull final MapView< G, A, R > mapView)
Removes a map view.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final Collection< MapViewManagerListener< G, A, R > > listeners
The registered MapViewManagerListeners.
Iterator< MapView< G, A, R > > iterator()
Returns all opened map views.
void deactivateMapView(@NotNull final MapView< G, A, R > mapView)
Deactivates a map view.
GameObjects are the objects based on Archetypes found on maps.
void setActiveMapView(@NotNull final MapView< G, A, R > mapView)
Sets the active map view.
MapView< G, A, R > getActiveMapView()
Returns the active top map view we are working with.
void removeMapViewManagerListener(@NotNull final MapViewManagerListener< G, A, R > listener)
Removes a listener to be notified.
MapView< G, A, R > activeMapView
The active map view.
final List< MapView< G, A, R > > mapViewsList
All open map views as a list.
void activateMapView(@NotNull final MapView< G, A, R > mapView)
Activates a map view.