Gridarta Editor
MapViews.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.Dimension;
23 import java.awt.Point;
24 import java.awt.Rectangle;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import javax.swing.JScrollPane;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class MapViews<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
43 
47  @NotNull
49 
56  @NotNull
57  private final List<MapView<G, A, R>> mapViews = new ArrayList<>();
58 
62  private int nextMapViewCounter;
63 
68  public void addMapViewsListener(@NotNull final MapViewsListener<G, A, R> listener) {
69  listeners.add(listener);
70  }
71 
76  public void removeMapViewsListener(@NotNull final MapViewsListener<G, A, R> listener) {
77  listeners.remove(listener);
78  }
79 
84  @Nullable
86  return mapViews.isEmpty() ? null : mapViews.get(0);
87  }
88 
93  public int getMapViews() {
94  return mapViews.size();
95  }
96 
107  @NotNull
108  public MapView<G, A, R> newMapView(@NotNull final MapControl<G, A, R> mapControl, @Nullable final Point viewPosition, @Nullable final Point centerSquare, @NotNull final MapViewFactory<G, A, R> mapViewFactory) {
109  mapControl.acquire();
110  try {
111  final MapView<G, A, R> mapView = mapViewFactory.newMapView(mapControl, viewPosition, ++nextMapViewCounter);
112  mapViews.add(mapView);
113  mapControl.acquire();
114  for (final MapViewsListener<G, A, R> listener : listeners.getListeners()) {
115  listener.mapViewCreated(mapView);
116  }
117  if (viewPosition == null && centerSquare != null) {
118  final Rectangle squareBounds = mapView.getRenderer().getSquareBounds(centerSquare);
119  final JScrollPane scrollPane = mapView.getScrollPane();
120  final Dimension extentSize = scrollPane.getViewport().getExtentSize();
121  final Point centerPoint = new Point(Math.max(0, squareBounds.x + squareBounds.width / 2 - extentSize.width / 2), Math.max(0, squareBounds.y + squareBounds.height / 2 - extentSize.height / 2));
122  scrollPane.getViewport().setViewPosition(centerPoint);
123  }
124  return mapView;
125  } finally {
126  mapControl.release();
127  }
128  }
129 
134  @NotNull
135  public Point[] getViewPositions() {
136  final Point[] result = new Point[mapViews.size()];
137  for (int i = 0; i < result.length; i++) {
138  result[i] = mapViews.get(i).getScrollPane().getViewport().getViewPosition();
139  }
140  return result;
141  }
142 
147  public void closeView(@NotNull final MapView<G, A, R> mapView) {
148  for (final MapViewsListener<G, A, R> listener : listeners.getListeners()) {
149  listener.mapViewClosing(mapView);
150  }
151  mapView.closeNotify();
152  mapViews.remove(mapView);
153  mapView.getMapControl().release();
154  }
155 
159  public void closeAllViews() {
160  while (!mapViews.isEmpty()) {
161  closeView(mapViews.get(0));
162  }
163  }
164 
169  public void setFocus(@NotNull final MapView<G, A, R> mapView) {
170  assert mapViews.contains(mapView);
171  mapViews.remove(mapView);
172  mapViews.add(0, mapView);
173  }
174 
180  @NotNull
181  public Iterator<MapView<G, A, R>> getMapViewIterator() {
182  return Collections.unmodifiableList(mapViews).iterator();
183  }
184 
188  public void repaintAllViews() {
189  for (final MapView<G, A, R> mapView : mapViews) {
190  mapView.getRenderer().forceRepaint();
191  }
192  }
193 
194 }
JScrollPane getScrollPane()
Returns the JScrollPane of this map view.
Point [] getViewPositions()
Returns the current view positions of all views of a MapControl.
Definition: MapViews.java:135
Rectangle getSquareBounds(@NotNull Point p)
Returns coordinates, length and width of map square.
T [] getListeners()
Returns an array of all the listeners.
int nextMapViewCounter
The map view counter for the next created MapView.
Definition: MapViews.java:62
int getMapViews()
Returns the number of views of a MapControl.
Definition: MapViews.java:93
void repaintAllViews()
Repaints all MapViews.
Definition: MapViews.java:188
A list of MapViews for one MapControl instance.
Definition: MapViews.java:42
void closeView(@NotNull final MapView< G, A, R > mapView)
Closes a view of a MapControl.
Definition: MapViews.java:147
void removeMapViewsListener(@NotNull final MapViewsListener< G, A, R > listener)
Removes a MapViewsListener to be notified of events.
Definition: MapViews.java:76
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
MapView< G, A, R > newMapView(@NotNull final MapControl< G, A, R > mapControl, @Nullable final Point viewPosition, @Nullable final Point centerSquare, @NotNull final MapViewFactory< G, A, R > mapViewFactory)
Creates a new MapView.
Definition: MapViews.java:108
void remove(@NotNull final T listener)
Removes a listener.
void closeAllViews()
Closes all views of a MapControl.
Definition: MapViews.java:159
GameObjects are the objects based on Archetypes found on maps.
void add(@NotNull final T listener)
Adds a listener.
MapRenderer getRenderer()
Returns the MapRenderer for this view.
MapView< G, A, R > getMapViewFrame()
Returns the last used view of a MapControl.
Definition: MapViews.java:85
final List< MapView< G, A, R > > mapViews
The list contains all views of the map.
Definition: MapViews.java:57
void addMapViewsListener(@NotNull final MapViewsListener< G, A, R > listener)
Adds a MapViewsListener to be notified of events.
Definition: MapViews.java:68
Type-safe version of EventListenerList.
final EventListenerList2< MapViewsListener< G, A, R > > listeners
The registered MapViewsListeners.
Definition: MapViews.java:48
Interface for listeners interested in MapViewsManager related events.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
Iterator< MapView< G, A, R > > getMapViewIterator()
Returns an Iterator returning all MapViews.
Definition: MapViews.java:181
void setFocus(@NotNull final MapView< G, A, R > mapView)
Sets a MapView as the main view.
Definition: MapViews.java:169