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-2023 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;
28 import java.util.Collection;
29 import java.util.concurrent.CopyOnWriteArrayList;
37 import net.sf.gridarta.utils.IOUtils;
38 import org.jetbrains.annotations.NotNull;
39 
44 public class DefaultMapControl<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements MapControl<G, A, R> {
45 
49  @NotNull
50  private final Collection<MapControlListener<G, A, R>> listenerList = new CopyOnWriteArrayList<>();
51 
56  private int useCounter = 1;
57 
61  private final boolean isPickmap;
62 
66  @NotNull
67  private final MapModel<G, A, R> mapModel;
68 
72  @NotNull
74 
78  @NotNull
80 
88  public DefaultMapControl(@NotNull final MapModel<G, A, R> mapModel, final boolean isPickmap, @NotNull final MapWriter<G, A, R> mapWriter, @NotNull final ProjectSettings projectSettings) {
89  this.mapModel = mapModel;
90  this.isPickmap = isPickmap;
91  this.mapWriter = mapWriter;
92  this.projectSettings = projectSettings;
93  }
94 
95  @Override
96  public void addMapControlListener(@NotNull final MapControlListener<G, A, R> listener) {
97  listenerList.add(listener);
98  }
99 
100  @Override
101  public void removeMapControlListener(@NotNull final MapControlListener<G, A, R> listener) {
102  listenerList.remove(listener);
103  }
104 
105  @Override
106  public void acquire() {
107  useCounter++;
108  }
109 
110  @Override
111  public void release() {
112  assert useCounter > 0;
113  useCounter--;
114  }
115 
116  @Override
117  public int getUseCounter() {
118  return useCounter;
119  }
120 
121  @Override
122  public boolean isPickmap() {
123  return isPickmap;
124  }
125 
131  private void encodeMapFile(@NotNull final File file) throws IOException {
132  final File dir = file.getParentFile();
133  if (dir != null && !dir.exists() && !dir.mkdirs()) {
134  throw new IOException("cannot create directory: " + dir);
135  }
136  try (OutputStream outputStream = new FileOutputStream(file)) {
137  try (Writer writer = new OutputStreamWriter(outputStream, IOUtils.MAP_ENCODING)) {
139  }
140  }
141  }
142 
143  @Override
144  public void save() throws IOException {
145  final MapFile mapFile = mapModel.getMapFile();
146  if (mapFile == null) {
147  throw new IllegalStateException("map file is unset");
148  }
149  save(mapFile);
150  }
151 
152  @Override
153  public void saveAs(@NotNull final MapFile mapFile) throws IOException {
154  mapModel.setMapFile(mapFile);
155  save(mapFile);
156  }
157 
164  private void save(@NotNull final MapFile mapFile) throws IOException {
165  if (mapModel.isModified()) {
166  mapModel.getMapArchObject().updateModifiedAttribute(projectSettings.getUserName());
167  }
168  encodeMapFile(mapFile.getFile());
170  for (final MapControlListener<G, A, R> listener : listenerList) {
171  listener.saved(this);
172  }
173  }
174 
175  @NotNull
176  @Override
178  return mapModel;
179  }
180 
181 }
net.sf.gridarta.model.mapcontrol.DefaultMapControl.mapWriter
final MapWriter< G, A, R > mapWriter
The MapWriter for saving this map.
Definition: DefaultMapControl.java:73
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.model.mapmodel.MapModel.resetModified
void resetModified()
Resets the modified flag to false.
net.sf.gridarta.model.mapcontrol.DefaultMapControl.addMapControlListener
void addMapControlListener(@NotNull final MapControlListener< G, A, R > listener)
Definition: DefaultMapControl.java:96
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.mapcontrol.DefaultMapControl.DefaultMapControl
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.
Definition: DefaultMapControl.java:88
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.mapcontrol.DefaultMapControl.saveAs
void saveAs(@NotNull final MapFile mapFile)
Definition: DefaultMapControl.java:153
net.sf.gridarta.model.mapcontrol.DefaultMapControl.getMapModel
MapModel< G, A, R > getMapModel()
Definition: DefaultMapControl.java:177
net.sf.gridarta.model.mapcontrol.DefaultMapControl.acquire
void acquire()
Definition: DefaultMapControl.java:106
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapcontrol.DefaultMapControl.mapModel
final MapModel< G, A, R > mapModel
The MapModel.
Definition: DefaultMapControl.java:67
net.sf.gridarta.model.mapmodel.MapModel.isModified
boolean isModified()
Return whether the map has been modified from the on-disk state.
net.sf.gridarta.model.mapcontrol.DefaultMapControl.release
void release()
Definition: DefaultMapControl.java:111
net.sf.gridarta.model.mapcontrol.DefaultMapControl.encodeMapFile
void encodeMapFile(@NotNull final File file)
Saves the map.
Definition: DefaultMapControl.java:131
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.settings.ProjectSettings.getUserName
String getUserName()
Returns the user name.
net.sf.gridarta.model.io.MapWriter
Interface for classes that write map files.
Definition: MapWriter.java:34
net.sf.gridarta.model.mapcontrol.DefaultMapControl.isPickmap
boolean isPickmap()
Definition: DefaultMapControl.java:122
net.sf.gridarta.model.mapcontrol.DefaultMapControl.removeMapControlListener
void removeMapControlListener(@NotNull final MapControlListener< G, A, R > listener)
Definition: DefaultMapControl.java:101
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapcontrol.DefaultMapControl.save
void save()
Definition: DefaultMapControl.java:144
net.sf.gridarta.model.mapcontrol.DefaultMapControl.useCounter
int useCounter
The use counter.
Definition: DefaultMapControl.java:56
net.sf.gridarta.model.mapcontrol.DefaultMapControl.projectSettings
final ProjectSettings projectSettings
The ProjectSettings to use.
Definition: DefaultMapControl.java:79
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.model.settings.ProjectSettings
Settings that apply to a project.
Definition: ProjectSettings.java:29
net.sf.gridarta.model.mapcontrol.DefaultMapControl.getUseCounter
int getUseCounter()
Definition: DefaultMapControl.java:117
net.sf.gridarta.model.mapcontrol.DefaultMapControl.save
void save(@NotNull final MapFile mapFile)
Updates the maps "Updated:" flag, saves it into the given {}, resets its modified and notifies all li...
Definition: DefaultMapControl.java:164
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.io.MapWriter.encodeMapFile
void encodeMapFile(@NotNull MapModel< G, A, R > mapModel, @NotNull Writer writer)
Write the whole map-data into a file.
net.sf.gridarta.model.mapmodel.MapModel.setMapFile
void setMapFile(@Nullable MapFile mapFile)
Sets the map file.
net.sf.gridarta.utils.IOUtils
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.model.mapcontrol.MapControlListener< G, A, R >
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapcontrol.DefaultMapControl
Implements map models.
Definition: DefaultMapControl.java:44
net.sf.gridarta.model.mapmodel.MapModel.getMapFile
MapFile getMapFile()
Returns the map file.
net.sf.gridarta.model.mapcontrol.DefaultMapControl.listenerList
final Collection< MapControlListener< G, A, R > > listenerList
The registered event listeners.
Definition: DefaultMapControl.java:50
net.sf.gridarta.model.mapcontrol.DefaultMapControl.isPickmap
final boolean isPickmap
Flag that indicates whether this is a pickmap or not.
Definition: DefaultMapControl.java:61
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.settings
Definition: AbstractDefaultProjectSettings.java:20
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Encoding to use for maps and other data.
Definition: IOUtils.java:52