Gridarta Editor
DefaultMapControl.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.model.mapcontrol;
21 
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
36 import net.sf.gridarta.utils.IOUtils;
37 import org.jetbrains.annotations.NotNull;
38 
43 public class DefaultMapControl<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements MapControl<G, A, R> {
44 
48  @NotNull
50 
55  private int useCounter = 1;
56 
60  private final boolean isPickmap;
61 
65  @NotNull
66  private final MapModel<G, A, R> mapModel;
67 
71  @NotNull
73 
77  @NotNull
79 
87  public DefaultMapControl(@NotNull final MapModel<G, A, R> mapModel, final boolean isPickmap, @NotNull final MapWriter<G, A, R> mapWriter, @NotNull final ProjectSettings projectSettings) {
88  this.mapModel = mapModel;
89  this.isPickmap = isPickmap;
90  this.mapWriter = mapWriter;
91  this.projectSettings = projectSettings;
92  }
93 
94  @Override
95  public void addMapControlListener(@NotNull final MapControlListener<G, A, R> listener) {
96  listenerList.add(listener);
97  }
98 
99  @Override
100  public void removeMapControlListener(@NotNull final MapControlListener<G, A, R> listener) {
101  listenerList.remove(listener);
102  }
103 
104  @Override
105  public void acquire() {
106  useCounter++;
107  }
108 
109  @Override
110  public void release() {
111  assert useCounter > 0;
112  useCounter--;
113  }
114 
115  @Override
116  public int getUseCounter() {
117  return useCounter;
118  }
119 
120  @Override
121  public boolean isPickmap() {
122  return isPickmap;
123  }
124 
130  private void encodeMapFile(@NotNull final File file) throws IOException {
131  final File dir = file.getParentFile();
132  if (dir != null && !dir.exists() && !dir.mkdirs()) {
133  throw new IOException("cannot create directory: " + dir);
134  }
135  try (OutputStream outputStream = new FileOutputStream(file)) {
136  try (Writer writer = new OutputStreamWriter(outputStream, IOUtils.MAP_ENCODING)) {
137  mapWriter.encodeMapFile(mapModel, writer);
138  }
139  }
140  }
141 
142  @Override
143  public void save() throws IOException {
144  final MapFile mapFile = mapModel.getMapFile();
145  if (mapFile == null) {
146  throw new IllegalStateException();
147  }
148  save(mapFile);
149  }
150 
151  @Override
152  public void saveAs(@NotNull final MapFile mapFile) throws IOException {
153  mapModel.setMapFile(mapFile);
154  save(mapFile);
155  }
156 
163  private void save(@NotNull final MapFile mapFile) throws IOException {
164  if (mapModel.isModified()) {
165  mapModel.getMapArchObject().updateModifiedAttribute(projectSettings.getUserName());
166  }
167  encodeMapFile(mapFile.getFile());
168  mapModel.resetModified();
169  for (final MapControlListener<G, A, R> listener : listenerList.getListeners()) {
170  listener.saved(this);
171  }
172  }
173 
174  @NotNull
175  @Override
177  return mapModel;
178  }
179 
180 }
A MapModel reflects the data of a map.
Definition: MapModel.java:75
T [] getListeners()
Returns an array of all the listeners.
Reading and writing of maps, handling of paths.
final EventListenerList2< MapControlListener< G, A, R > > listenerList
The registered event listeners.
Settings that apply to a project.
Interface for classes that write map files.
Definition: MapWriter.java:34
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:51
final MapWriter< G, A, R > mapWriter
The MapWriter for saving this map.
boolean isModified()
Return whether the map has been modified from the on-disk state.
void save(@NotNull final MapFile mapFile)
Updates the maps "Updated:" flag, saves it into the given MapFile, resets its modified and notifies a...
final ProjectSettings projectSettings
The ProjectSettings to use.
Base package of all Gridarta classes.
final boolean isPickmap
Flag that indicates whether this is a pickmap or not.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Utility-class for Gridarta&#39;s I/O.
Definition: IOUtils.java:40
void remove(@NotNull final T listener)
Removes a listener.
Interface for listeners listening on changes in MapControl instances.
String getUserName()
Returns the user name.
GameObjects are the objects based on Archetypes found on maps.
final MapModel< G, A, R > mapModel
The MapModel.
void add(@NotNull final T listener)
Adds a listener.
void addMapControlListener(@NotNull final MapControlListener< G, A, R > listener)
Type-safe version of EventListenerList.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
void resetModified()
Resets the modified flag to false.
MapFile getMapFile()
Returns the map file.
void encodeMapFile(@NotNull MapModel< G, A, R > mapModel, @NotNull Writer writer)
Write the whole map-data into a file.
void setMapFile(@Nullable MapFile mapFile)
Sets the map file.
DefaultMapControl(@NotNull final MapModel< G, A, R > mapModel, final boolean isPickmap, @NotNull final MapWriter< G, A, R > mapWriter, @NotNull final ProjectSettings projectSettings)
Creates a new instance.
The location of a map file with a map directory.
Definition: MapFile.java:31
void removeMapControlListener(@NotNull final MapControlListener< G, A, R > listener)
void encodeMapFile(@NotNull final File file)
Saves the map.