Gridarta Editor
PickmapChooserModel.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.panel.pickmapchooser;
21 
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 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 PickmapChooserModel<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterable<PickmapState<G, A, R>> {
52 
56  @NotNull
57  private final List<PickmapState<G, A, R>> pickmapStates = new ArrayList<>();
58 
62  @NotNull
63  private final Collection<PickmapChooserModelListener<G, A, R>> listeners = new LinkedList<>();
64 
68  @NotNull
69  private final Map<MapControl<G, A, R>, MapModelListener<G, A, R>> mapModelListeners = new HashMap<>();
70 
76  public int addMapFile(@NotNull final PickmapState<G, A, R> pickmapState) {
77  final int tmp = Collections.binarySearch(pickmapStates, pickmapState, MapFileNameComparator.INSTANCE);
78  final int index = tmp >= 0 ? tmp : -tmp - 1;
79  pickmapStates.add(index, pickmapState);
80  addMapModelListener(pickmapState.getPickmap());
81  return index;
82  }
83 
89  public int removeMapFile(@NotNull final PickmapState<G, A, R> pickmapState) {
90  final int index = pickmapStates.indexOf(pickmapState);
91  if (index < 0) {
92  return -1; // might have been a pickmap that could not be loaded
93  }
94  removeMapModelListener(pickmapState.getPickmap());
95  pickmapStates.remove(index);
96  return index;
97  }
98 
105  public int revertMapFile(@NotNull final MapControl<G, A, R> oldPickmap, @NotNull final PickmapState<G, A, R> pickmapState) {
106  final int index = pickmapStates.indexOf(pickmapState);
107  if (index < 0) {
108  return -1; // might have been a pickmap that could not be loaded
109  }
110  removeMapModelListener(oldPickmap);
111  addMapModelListener(pickmapState.getPickmap());
112  firePickmapReverted(pickmapState);
113  return index;
114  }
115 
121  @Nullable
122  public PickmapState<G, A, R> getPickmap(@NotNull final File file) {
123  for (final PickmapState<G, A, R> pickmapState : pickmapStates) {
124  if (pickmapState.getFile().equals(file)) {
125  return pickmapState;
126  }
127  }
128  return null;
129  }
130 
135  public boolean isEmpty() {
136  return pickmapStates.isEmpty();
137  }
138 
144  public int indexOf(@NotNull final PickmapState<G, A, R> pickmapState) {
145  return pickmapStates.indexOf(pickmapState);
146  }
147 
148  @NotNull
149  @Override
150  public Iterator<PickmapState<G, A, R>> iterator() {
151  return Collections.unmodifiableList(pickmapStates).iterator();
152  }
153 
159  @NotNull
160  public PickmapState<G, A, R> get(final int index) {
161  return pickmapStates.get(index);
162  }
163 
167  public void clear() {
168  for (final PickmapState<G, A, R> pickmapState : pickmapStates) {
169  removeMapModelListener(pickmapState.getPickmap());
170  pickmapState.freePickmap();
171  }
172  pickmapStates.clear();
173  }
174 
179  public void addPickmapChooserListener(@NotNull final PickmapChooserModelListener<G, A, R> pickmapChooserModelListener) {
180  listeners.add(pickmapChooserModelListener);
181  }
182 
187  private void firePickmapReverted(@NotNull final PickmapState<G, A, R> pickmapState) {
188  for (final PickmapChooserModelListener<G, A, R> listener : listeners) {
189  listener.pickmapReverted(pickmapState);
190  }
191  }
192 
197  public void fireActivePickmapChanged(@Nullable final PickmapState<G, A, R> pickmapState) {
198  for (final PickmapChooserModelListener<G, A, R> listener : listeners) {
199  listener.activePickmapChanged(pickmapState);
200  }
201  }
202 
208  private void firePickmapModifiedChanged(final int index, @NotNull final PickmapState<G, A, R> pickmapState) {
209  for (final PickmapChooserModelListener<G, A, R> listener : listeners) {
210  listener.pickmapModifiedChanged(index, pickmapState);
211  }
212  }
213 
219  private void addMapModelListener(@NotNull final MapControl<G, A, R> mapControl) {
220  final MapModel<G, A, R> mapModel = mapControl.getMapModel();
221  final MapModelListener<G, A, R> mapModelListener = new PickmapChooserMapModelListener(mapModel);
222  mapModel.addMapModelListener(mapModelListener);
223  mapModelListeners.put(mapControl, mapModelListener);
224  }
225 
231  private void removeMapModelListener(@NotNull final MapControl<G, A, R> mapControl) {
232  final MapModelListener<G, A, R> mapModelListener = mapModelListeners.remove(mapControl);
233  assert mapModelListener != null;
234  mapControl.getMapModel().removeMapModelListener(mapModelListener);
235  }
236 
240  private class PickmapChooserMapModelListener implements MapModelListener<G, A, R> {
241 
245  @NotNull
247 
252  private PickmapChooserMapModelListener(@NotNull final MapModel<G, A, R> mapModel) {
253  this.mapModel = mapModel;
254  }
255 
256  @Override
257  public void mapSizeChanged(@NotNull final Size2D newSize) {
258  // ignore
259  }
260 
261  @Override
262  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
263  // ignore
264  }
265 
266  @Override
267  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
268  // ignore
269  }
270 
271  @Override
272  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
273  // ignore
274  }
275 
276  @Override
277  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
278  // ignore
279  }
280 
281  @Override
282  public void modifiedChanged() {
283  int index = 0;
284  for (final PickmapState<G, A, R> pickmapState : pickmapStates) {
285  if (pickmapState.getPickmap().getMapModel() == mapModel) {
286  firePickmapModifiedChanged(index, pickmapState);
287  return;
288  }
289  index++;
290  }
291  }
292 
293  }
294 
295 }
int removeMapFile(@NotNull final PickmapState< G, A, R > pickmapState)
Removes a map file.
static final Comparator< PickmapState<?, ?, ?> > INSTANCE
A Comparator that compares PickmapState instances by name.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
PickmapState< G, A, R > getPickmap(@NotNull final File file)
Returns the pickmap by file name.
void errorsChanged(@NotNull final ErrorCollector< G, A, R > errors)
The errors of a map model have changed.
void mapFileChanged(@Nullable final MapFile oldMapFile)
The map file has changed.
Graphical User Interface of Gridarta.
final Collection< PickmapChooserModelListener< G, A, R > > listeners
The listeners to notify.
Maintains the state of a pickmap file.
void firePickmapModifiedChanged(final int index, @NotNull final PickmapState< G, A, R > pickmapState)
Notifies all listeners that a pickmap has been modified.
This package contains the framework for validating maps.
void removeMapModelListener(@NotNull final MapControl< G, A, R > mapControl)
Stops tracking a MapControl instance for changes.
A Comparator for comparing MapFiles by pickmap name.
void addMapModelListener(@NotNull final MapControl< G, A, R > mapControl)
Tracks a MapControl instance for changes.
boolean isEmpty()
Returns whether no pickmaps exist in the current folder.
Interface for listeners listening on MapModel events.
int indexOf(@NotNull final PickmapState< G, A, R > pickmapState)
Returns the index of a map file.
PickmapChooserMapModelListener(@NotNull final MapModel< G, A, R > mapModel)
Creates a new instance.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
final List< PickmapState< G, A, R > > pickmapStates
All open pickmaps.
void mapObjectsChanged(@NotNull final Set< G > gameObjects, @NotNull final Set< G > transientGameObjects)
One or more GameObjects on a map have changed.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void addPickmapChooserListener(@NotNull final PickmapChooserModelListener< G, A, R > pickmapChooserModelListener)
Adds a PickmapChooserModelListener to be notified.
GameObjects are the objects based on Archetypes found on maps.
final Map< MapControl< G, A, R >, MapModelListener< G, A, R > > mapModelListeners
Maps MapControl instance to attached MapModelListener.
void mapSquaresChanged(@NotNull final Set< MapSquare< G, A, R >> mapSquares)
Squares of a map have changed.
void fireActivePickmapChanged(@Nullable final PickmapState< G, A, R > pickmapState)
Notifies all listeners that the active pickmap has changes.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
An interface for classes that collect errors.
int addMapFile(@NotNull final PickmapState< G, A, R > pickmapState)
Adds a map file.
void mapSizeChanged(@NotNull final Size2D newSize)
The size of a map has changed.
void firePickmapReverted(@NotNull final PickmapState< G, A, R > pickmapState)
Notifies all listeners that the a pickmap has been reverted.
The location of a map file with a map directory.
Definition: MapFile.java:31
int revertMapFile(@NotNull final MapControl< G, A, R > oldPickmap, @NotNull final PickmapState< G, A, R > pickmapState)
Reverts a map file.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30