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 }
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result
Definition: MapMenuLoader.java:225
net.sf.gridarta.gui.mapmenu.MapMenuLoader.saveNumEntries
void saveNumEntries(final int num)
Definition: MapMenuLoader.java:109
net.sf.gridarta.gui.mapmenu.MapMenuLoader.pathManager
final PathManager pathManager
Definition: MapMenuLoader.java:77
net.sf.gridarta.gui.mapmenu.MapMenuEntry
Definition: MapMenuEntry.java:29
net.sf.gridarta.gui.mapmenu.MapMenuLoader.key
final String key
Definition: MapMenuLoader.java:71
net.sf.gridarta
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.Result
Result(@NotNull final String directory, @NotNull final MapMenuEntry mapMenuEntry)
Definition: MapMenuLoader.java:244
net.sf
net.sf.gridarta.gui.mapmenu.MapMenuLoader.loadNumEntries
int loadNumEntries()
Definition: MapMenuLoader.java:97
net.sf.gridarta.model.io.PathManager
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()
Definition: MapMenuLoader.java:263
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.directory
final String directory
Definition: MapMenuLoader.java:231
net
net.sf.gridarta.gui.mapmenu.MapMenuLoader.removeEntry
void removeEntry(final int index)
Definition: MapMenuLoader.java:209
net.sf.gridarta.gui.mapmenu.MapMenuLoader.loadEntry
Result loadEntry(final int index)
Definition: MapMenuLoader.java:123
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.mapMenuEntry
final MapMenuEntry mapMenuEntry
Definition: MapMenuLoader.java:237
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Result.getDirectory
String getDirectory()
Definition: MapMenuLoader.java:254
net.sf.gridarta.model.io.PathManager.getMapFile
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
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)
Definition: MapMenuLoader.java:186
net.sf.gridarta.gui.mapmenu.MapMenuLoader
Definition: MapMenuLoader.java:35
net.sf.gridarta.model.io
Definition: AbstractArchetypeParser.java:20
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type
Definition: MapMenuLoader.java:41
net.sf.gridarta.gui.mapmenu.MapMenuLoader.MapMenuLoader
MapMenuLoader(@NotNull final String key, @NotNull final PathManager pathManager)
Definition: MapMenuLoader.java:84
net.sf.gridarta.gui.mapmenu.MapMenuLoader.preferences
final Preferences preferences
Definition: MapMenuLoader.java:65
net.sf.gridarta.model
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type.DIR
DIR
Definition: MapMenuLoader.java:46
net.sf.gridarta.gui.mapmenu.MapMenuLoader.Type.MAP
MAP
Definition: MapMenuLoader.java:51
net.sf.gridarta.gui.mapmenu.MapMenuEntryMap
Definition: MapMenuEntryMap.java:29
net.sf.gridarta.gui.mapmenu.MapMenuEntryDir
Definition: MapMenuEntryDir.java:28
net.sf.gridarta.gui.mapmenu.MapMenuLoader.LOG
static final Category LOG
Definition: MapMenuLoader.java:59
net.sf.gridarta.MainControl
Definition: MainControl.java:27