Gridarta Editor
PickmapState.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.mapfiles;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.regex.Pattern;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
45 public class PickmapState<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
46 
50  @NotNull
51  private static final Pattern PATTERN_VALID_PICKMAP_NAME = Pattern.compile("[-a-zA-Z_+ 0-9,]+");
52 
56  @NotNull
58 
62  @NotNull
63  private final String name;
64 
68  @NotNull
70 
74  @NotNull
76 
81  @Nullable
83 
88  @Nullable
90 
94  @NotNull
96 
101  @NotNull
102  private final Object sync = new Object();
103 
113  public PickmapState(@NotNull final MapFolder<G, A, R> mapFolder, @NotNull final String name, @NotNull final MapReaderFactory<G, A> mapReaderFactory, @NotNull final MapManager<G, A, R> pickmapManager, @NotNull final MapViewsManager<G, A, R> mapViewsManager) throws InvalidNameException {
114  if (!isValidPickmapName(name)) {
115  throw new InvalidNameException(name);
116  }
117 
118  this.mapFolder = mapFolder;
119  this.name = name;
120  this.pickmapManager = pickmapManager;
121  this.mapReaderFactory = mapReaderFactory;
122  this.mapViewsManager = mapViewsManager;
123  }
124 
130  public static boolean isValidPickmapName(@NotNull final CharSequence name) {
131  return PATTERN_VALID_PICKMAP_NAME.matcher(name).matches();
132  }
133 
138  @NotNull
139  public File getFile() {
140  return new File(mapFolder.getDir(), name);
141  }
142 
149  @Nullable
151  synchronized (sync) {
152  return pickmap;
153  }
154  }
155 
162  @Nullable
164  synchronized (sync) {
165  return pickmapView;
166  }
167  }
168 
173  public void loadPickmap() throws IOException {
174  synchronized (sync) {
175  if (pickmap != null) {
176  return;
177  }
178 
179  final File file = getFile();
180  final MapReader<G, A> decoder = mapReaderFactory.newMapReader(file);
181  pickmap = pickmapManager.newMap(decoder.getGameObjects(), decoder.getMapArchObject(), new MapFile(file), true);
182  pickmapView = mapViewsManager.newMapView(pickmap, null, null);
183  }
184  }
185 
189  public void freePickmap() {
190  synchronized (sync) {
191  if (pickmap != null) {
192  assert pickmapView != null;
193  mapViewsManager.closeView(pickmapView);
194  assert pickmap != null;
195  pickmapManager.release(pickmap);
196  pickmap = null;
197  }
198  }
199  }
200 
205  public void getUnsavedPickmaps(@NotNull final Collection<MapControl<G, A, R>> unsavedPickmaps) {
206  synchronized (sync) {
207  if (pickmap != null && pickmap.getMapModel().isModified()) {
208  unsavedPickmaps.add(pickmap);
209  }
210  }
211  }
212 
217  public void remove(final boolean deleteFile) {
218  mapFolder.removePickmap(this, deleteFile);
219  }
220 
226  public void save() throws IOException {
227  final MapControl<G, A, R> oldPickmap = getPickmap();
228  if (oldPickmap != null && oldPickmap.getMapModel().isModified()) {
229  oldPickmap.save();
230  }
231  }
232 
238  public void revert() throws IOException {
239  final MapControl<G, A, R> oldPickmap = getPickmap();
240  if (oldPickmap != null) {
241  freePickmap();
242  loadPickmap();
243  mapFolder.firePickmapReverted(this, oldPickmap);
244  }
245  }
246 
247 }
void save()
Saves the map to a file.
static final Pattern PATTERN_VALID_PICKMAP_NAME
The Pattern that matches valid pickmap names.
MapView< G, A, R > pickmapView
The MapView or pickmap, or.
A MapManager manages all opened maps.
Definition: MapManager.java:37
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
void firePickmapReverted(@NotNull final PickmapState< G, A, R > pickmapState, @NotNull final MapControl< G, A, R > oldPickmap)
Notifies all listeners about a reverted pickmap.
Definition: MapFolder.java:256
MapControl< G, A, R > newMap(@Nullable List< G > objects, @NotNull A mapArchObject, @Nullable MapFile mapFile, boolean interactive)
Creates a new map control without view.
Maintains the state of a pickmap file.
final MapFolder< G, A, R > mapFolder
The folder this pickmap is part of.
MapReader< G, A > newMapReader(@NotNull File file)
Create a new MapReader instance.
A factory for creating MapReader instances.
MapView< G, A, R > newMapView(@NotNull final MapControl< G, A, R > mapControl, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Creates a new map view.
File getDir()
Returns the base directory.
Definition: MapFolder.java:143
void revert()
Reverts this pickmap to its underlying map file.
boolean isModified()
Return whether the map has been modified from the on-disk state.
static boolean isValidPickmapName(@NotNull final CharSequence name)
Returns whether a pickmap name is valid.
List< G > getGameObjects()
Return the decoded game objects.
MapModel< G, A, R > getMapModel()
Returns the map model.
void loadPickmap()
Loads the pickmap from the underlying map file.
final MapManager< G, A, R > pickmapManager
The MapManager for creating new pickmaps.
Base package of all Gridarta classes.
Interface for classes that read map files.
Definition: MapReader.java:31
Reflects a game object (object on a map).
Definition: GameObject.java:36
void removePickmap(@NotNull final PickmapState< G, A, R > pickmapState, final boolean deleteFile)
Removes a PickmapState from this folder.
Definition: MapFolder.java:172
final String name
The underlying map file name.
MapControl< G, A, R > getPickmap()
Returns the MapControl representing this pickmap.
GameObjects are the objects based on Archetypes found on maps.
PickmapState(@NotNull final MapFolder< G, A, R > mapFolder, @NotNull final String name, @NotNull final MapReaderFactory< G, A > mapReaderFactory, @NotNull final MapManager< G, A, R > pickmapManager, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a new instance.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Base classes for rendering maps.
MapView< G, A, R > getMapView()
Returns the MapView instance for this pickmap.
File getFile()
Returns the underlying map file.
MapControl< G, A, R > pickmap
The MapControl instance representing the map file, or.
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 Object sync
The synchronization object for accessed to pickmap and pickmapView.
final MapReaderFactory< G, A > mapReaderFactory
The MapReaderFactory to use.
void freePickmap()
Unloads the map file.
A getMapArchObject()
Returns the MapArchObject read from this MapFileDecode.
void closeView(@NotNull final MapView< G, A, R > mapView)
Closes a view of a MapControl.
void getUnsavedPickmaps(@NotNull final Collection< MapControl< G, A, R >> unsavedPickmaps)
Returns this pickmap if it has been loaded and is modified.
The location of a map file with a map directory.
Definition: MapFile.java:31
Indicates that a folder or pickmap name is invalid.