Gridarta Editor
MapMenuLoader.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.gui.mapmenu;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.prefs.Preferences;
25 import net.sf.gridarta.MainControl;
27 import org.apache.log4j.Category;
28 import org.apache.log4j.Logger;
29 import org.jetbrains.annotations.NotNull;
30 
35 public class MapMenuLoader {
36 
41  public enum Type {
42 
46  DIR,
47 
51  MAP
52 
53  }
54 
58  @NotNull
59  private static final Category LOG = Logger.getLogger(MapMenuLoader.class);
60 
64  @NotNull
65  private final Preferences preferences = Preferences.userNodeForPackage(MainControl.class);
66 
70  @NotNull
71  private final String key;
72 
76  @NotNull
77  private final PathManager pathManager;
78 
84  public MapMenuLoader(@NotNull final String key, @NotNull final PathManager pathManager) {
85  if (LOG.isDebugEnabled()) {
86  //noinspection ThisEscapedInObjectConstruction
87  LOG.debug(System.identityHashCode(this) + " new " + key);
88  }
89  this.key = key;
90  this.pathManager = pathManager;
91  }
92 
97  public int loadNumEntries() {
98  final int result = preferences.getInt(key + "Num", 0);
99  if (LOG.isDebugEnabled()) {
100  LOG.debug(System.identityHashCode(this) + " loadNumEntries=" + result);
101  }
102  return result;
103  }
104 
109  public void saveNumEntries(final int num) {
110  if (LOG.isDebugEnabled()) {
111  LOG.debug(System.identityHashCode(this) + " saveNumEntries(" + num + ")");
112  }
113  preferences.putInt(key + "Num", num);
114  }
115 
122  @NotNull
123  public Result loadEntry(final int index) throws IOException {
124  final String suffix = "[" + index + "]";
125  final String title = preferences.get(key + "Title" + suffix, "");
126  final String filename = preferences.get(key + "Filename" + suffix, "");
127  final String directory = preferences.get(key + "Directory" + suffix, "");
128  final String typeString = preferences.get(key + "Type" + suffix, Type.MAP.toString());
129  final Type type;
130  try {
131  type = Type.valueOf(typeString);
132  } catch (final IllegalArgumentException ex) {
133  if (LOG.isDebugEnabled()) {
134  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=invalid type: " + typeString + " [" + ex.getMessage() + "]");
135  }
136  throw new IOException("invalid type: " + typeString, ex);
137  }
138  switch (type) {
139  case DIR:
140  final MapMenuEntry mapMenuEntryDir;
141  try {
142  mapMenuEntryDir = new MapMenuEntryDir(title);
143  } catch (final IllegalArgumentException ex) {
144  if (LOG.isDebugEnabled()) {
145  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=invalid directory name: " + title + " [" + ex.getMessage() + "]");
146  }
147  throw new IOException("invalid directory name: " + title, ex);
148  }
149  final Result resultDir = new Result(directory, mapMenuEntryDir);
150  if (LOG.isDebugEnabled()) {
151  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=DIR[" + resultDir + "]");
152  }
153  return resultDir;
154 
155  case MAP:
156  if (filename.isEmpty()) {
157  if (LOG.isDebugEnabled()) {
158  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=empty filename: " + title);
159  }
160  throw new IOException("bookmark without file name: " + title);
161  }
162 
163  final MapMenuEntry mapMenuEntryMap = new MapMenuEntryMap(pathManager.getMapFile(new File(filename)), title);
164  final Result resultMap = new Result(directory, mapMenuEntryMap);
165  if (LOG.isDebugEnabled()) {
166  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=MAP[" + resultMap + "]");
167  }
168  return resultMap;
169 
170  default:
171  if (LOG.isDebugEnabled()) {
172  LOG.debug(System.identityHashCode(this) + " loadEntry(" + index + ")=invalid type: " + type);
173  }
174  throw new IOException("invalid type: " + type);
175  }
176  }
177 
186  public void saveEntry(final int index, @NotNull final String title, @NotNull final String filename, @NotNull final String directory, @NotNull final Type type) {
187  if (LOG.isDebugEnabled()) {
188  LOG.debug(System.identityHashCode(this) + " saveEntry(title=" + title + ", filename=" + filename + ", directory=" + directory + ", type=" + type + ")");
189  }
190  final String suffix = "[" + index + "]";
191  preferences.put(key + "Title" + suffix, title);
192  preferences.put(key + "Filename" + suffix, filename);
193  if (directory.isEmpty()) {
194  preferences.remove(key + "Directory" + suffix);
195  } else {
196  preferences.put(key + "Directory" + suffix, directory);
197  }
198  if (type == Type.MAP) {
199  preferences.remove(key + "Type" + suffix);
200  } else {
201  preferences.put(key + "Type" + suffix, type.toString());
202  }
203  }
204 
209  public void removeEntry(final int index) {
210  if (LOG.isDebugEnabled()) {
211  LOG.debug(System.identityHashCode(this) + " removeEntry(" + index + ")");
212  }
213  final String suffix = "[" + index + "]";
214  preferences.remove(key + "Title" + suffix);
215  preferences.remove(key + "Filename" + suffix);
216  preferences.remove(key + "Directory" + suffix);
217  preferences.remove(key + "Type" + suffix);
218  }
219 
225  public static class Result {
226 
230  @NotNull
231  private final String directory;
232 
236  @NotNull
237  private final MapMenuEntry mapMenuEntry;
238 
244  private Result(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry) {
245  this.directory = directory;
246  this.mapMenuEntry = mapMenuEntry;
247  }
248 
253  @NotNull
254  public String getDirectory() {
255  return directory;
256  }
257 
262  @NotNull
264  return mapMenuEntry;
265  }
266 
267  @NotNull
268  @Override
269  public String toString() {
270  return "directory=" + directory + ",mapMenuEntry=" + mapMenuEntry;
271  }
272 
273  }
274 
275 }
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result
Result value consisting of a MapMenuEntry and its location (directory).
Definition: MapMenuLoader.java:225
net.sf.gridarta.gui.mapmenu.MapMenuLoader.saveNumEntries
void saveNumEntries(final int num)
Sets the number of entries present in the preferences.
Definition: MapMenuLoader.java:109
net.sf.gridarta.gui.mapmenu.MapMenuLoader.pathManager
final PathManager pathManager
The global path manager instance.
Definition: MapMenuLoader.java:77
net.sf.gridarta.gui.mapmenu.MapMenuEntry
Abstract base class for recent and bookmark menu entries.
Definition: MapMenuEntry.java:29
net.sf.gridarta.gui.mapmenu.MapMenuLoader.key
final String key
The preferences key prefix.
Definition: MapMenuLoader.java:71
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.Result
Result(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry)
Creates a new instance.
Definition: MapMenuLoader.java:244
directory
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing before considering any fundamental changes About code DO NOT USE TABS No matter what Java development platform you are please configure insert indent Tabs are displayed totally different in every editor and there are millions of different editors out there The insertion of tabs in the source code is messing up the syntax formatting in a way that is UNREPAIRABLE Apart from please keep code indentation accurate This is not just good it helps to keep code readable and in that way dramatically decreases the chance for overlooked bugs Everyone is welcomed to correct indentation errors wherever they are spotted Before you start to do this please double check that your editor is really configured to insert spaces Line feeds may be checked in either in windows or in unix linux style All reasonable text and java editors can deal with both linefeed formats Converting line feeds is but in this case please make sure that only linefeed characters are changed and nothing else is affected Due to the platform independent nature of the editor has the potential to run on almost any given operating system the build process differs greatly between systems as well as java environments In the several people have attempted to add build scripts along with structural changes to optimize the setup on one particular system environment which has led to conflict Please do *not *attempt to change the structure or any directories for the mere purpose of improving a build process or performance in a java environment Build scripts may be placed in the root directory
Definition: Developer_README.txt:45
net.sf
net.sf.gridarta.gui.mapmenu.MapMenuLoader.loadNumEntries
int loadNumEntries()
Returns the number of entries present in the preferences.
Definition: MapMenuLoader.java:97
net.sf.gridarta.model.io.PathManager
This class contains methods for converting relative map paths to absolute map paths and vice versa.
Definition: PathManager.java:39
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.toString
String toString()
Definition: MapMenuLoader.java:269
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.getMapMenuEntry
MapMenuEntry getMapMenuEntry()
Returns the entry.
Definition: MapMenuLoader.java:263
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.directory
final String directory
The entry's directory.
Definition: MapMenuLoader.java:231
net
net.sf.gridarta.gui.mapmenu.MapMenuLoader.removeEntry
void removeEntry(final int index)
Removes an entry.
Definition: MapMenuLoader.java:209
net.sf.gridarta.gui.mapmenu.MapMenuLoader.loadEntry
Result loadEntry(final int index)
Loads an entry from preferences.
Definition: MapMenuLoader.java:123
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.mapMenuEntry
final MapMenuEntry mapMenuEntry
The entry.
Definition: MapMenuLoader.java:237
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.getDirectory
String getDirectory()
Returns the entry's directory.
Definition: MapMenuLoader.java:254
net.sf.gridarta.model.io.PathManager.getMapFile
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Definition: PathManager.java:72
net.sf.gridarta.gui.mapmenu.MapMenuLoader.saveEntry
void saveEntry(final int index, @NotNull final String title, @NotNull final String filename, @NotNull final String directory, @NotNull final Type type)
Saves an entry to preferences.
Definition: MapMenuLoader.java:186
net.sf.gridarta.gui.mapmenu.MapMenuLoader
Saves or restores MapMenu contents to Preferences.
Definition: MapMenuLoader.java:35
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type
Preferences values for entries.
Definition: MapMenuLoader.java:41
net.sf.gridarta.gui.mapmenu.MapMenuLoader.MapMenuLoader
MapMenuLoader(@NotNull final String key, @NotNull final PathManager pathManager)
Creates a new instance.
Definition: MapMenuLoader.java:84
net.sf.gridarta.gui.mapmenu.MapMenuLoader.preferences
final Preferences preferences
The Preferences.
Definition: MapMenuLoader.java:65
net.sf.gridarta.model
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type.DIR
DIR
Preferences value for entries representing directories.
Definition: MapMenuLoader.java:46
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type.MAP
MAP
Preferences value for entries representing map files.
Definition: MapMenuLoader.java:51
net.sf.gridarta.gui.mapmenu.MapMenuEntryMap
A MapMenuEntry that represents a map.
Definition: MapMenuEntryMap.java:29
net.sf.gridarta.gui.mapmenu.MapMenuEntryDir
A MapMenuEntry that represents a directory.
Definition: MapMenuEntryDir.java:28
net.sf.gridarta.gui.mapmenu.MapMenuLoader.LOG
static final Category LOG
The Logger for printing log messages.
Definition: MapMenuLoader.java:59
net.sf.gridarta.MainControl
Interface used as preferences location.
Definition: MainControl.java:27