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-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.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 }
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.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapMenuEntry that represents a directory.
MapMenuLoader(@NotNull final String key, @NotNull final PathManager pathManager)
Creates a new instance.
Reading and writing of maps, handling of paths.
final String directory
The entry's directory.
int loadNumEntries()
Returns the number of entries present in the preferences.
final String key
The preferences key prefix.
static final Category LOG
The Logger for printing log messages.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
MAP
Preferences value for entries representing map files.
Result loadEntry(final int index)
Loads an entry from preferences.
Result value consisting of a MapMenuEntry and its location (directory).
Base package of all Gridarta classes.
Saves or restores MapMenu contents to Preferences.
final Preferences preferences
The Preferences.
void saveNumEntries(final int num)
Sets the number of entries present in the preferences.
Interface used as preferences location.
MapMenuEntry getMapMenuEntry()
Returns the entry.
Abstract base class for recent and bookmark menu entries.
DIR
Preferences value for entries representing directories.
final PathManager pathManager
The global path manager instance.
A MapMenuEntry that represents a map.
void removeEntry(final int index)
Removes an entry.
String getDirectory()
Returns the entry's directory.
Result(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry)
Creates a new instance.