Gridarta Editor
AbstractDefaultProjectSettings.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.settings;
21 
22 import java.io.File;
23 import java.util.prefs.PreferenceChangeEvent;
24 import java.util.prefs.PreferenceChangeListener;
25 import java.util.prefs.Preferences;
26 import net.sf.gridarta.MainControl;
27 import net.sf.gridarta.utils.IOUtils;
28 import org.jetbrains.annotations.NotNull;
29 
35 
39  @NotNull
40  private static final String MAP_DIRECTORY_KEY = "mapDirectory";
41 
45  @NotNull
46  private static final String ARCH_DIRECTORY_KEY = "archDirectory";
47 
51  @NotNull
52  private static final String MEDIA_DIRECTORY_KEY = "mediaDirectory";
53 
57  @NotNull
58  private static final String IMAGE_SET_KEY = "useImageSet";
59 
63  @NotNull
64  private static final String PREFERENCES_USER_NAME = "username";
65 
69  @NotNull
70  private static final String CONFIG_SOURCE_KEY = "configSource";
71 
75  @NotNull
76  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
77 
81  @NotNull
83 
87  @NotNull
88  private File archDirectory;
89 
93  @NotNull
94  private File mapsDirectory;
95 
99  @NotNull
100  private File mediaDirectory;
101 
105  @NotNull
106  private String imageSet;
107 
111  @NotNull
112  private String configSourceName;
113 
118  protected AbstractDefaultProjectSettings(@NotNull final EditorSettings editorSettings) {
119  this.editorSettings = editorSettings;
120 
121  final PreferenceChangeListener preferenceChangeListener = new PreferenceChangeListener() {
122 
123  @Override
124  public void preferenceChange(final PreferenceChangeEvent evt) {
125  if (evt.getKey().equals(ARCH_DIRECTORY_KEY)) {
126  archDirectory = IOUtils.getCanonicalFile(new File(PREFERENCES.get(ARCH_DIRECTORY_KEY, editorSettings.getArchDirectoryDefault().toString())));
127  } else if (evt.getKey().equals(MAP_DIRECTORY_KEY)) {
128  setMapsDirectoryInt(new File(PREFERENCES.get(MAP_DIRECTORY_KEY, editorSettings.getMapsDirectoryDefault().toString())), false);
129  } else if (evt.getKey().equals(MEDIA_DIRECTORY_KEY)) {
130  mediaDirectory = IOUtils.getCanonicalFile(new File(PREFERENCES.get(MEDIA_DIRECTORY_KEY, editorSettings.getMediaDirectoryDefault().toString())));
131  } else if (evt.getKey().equals(IMAGE_SET_KEY)) {
132  imageSet = PREFERENCES.get(IMAGE_SET_KEY, editorSettings.getImageSetDefault());
133  } else if (evt.getKey().equals(CONFIG_SOURCE_KEY)) {
134  configSourceName = PREFERENCES.get(CONFIG_SOURCE_KEY, "");
135  }
136  }
137 
138  };
139  PREFERENCES.addPreferenceChangeListener(preferenceChangeListener);
140 
141  archDirectory = IOUtils.getCanonicalFile(new File(PREFERENCES.get(ARCH_DIRECTORY_KEY, editorSettings.getArchDirectoryDefault().toString())));
142  mapsDirectory = IOUtils.getCanonicalFile(new File(PREFERENCES.get(MAP_DIRECTORY_KEY, editorSettings.getMapsDirectoryDefault().toString())));
143  mediaDirectory = IOUtils.getCanonicalFile(new File(PREFERENCES.get(MEDIA_DIRECTORY_KEY, editorSettings.getMediaDirectoryDefault().toString())));
144  imageSet = PREFERENCES.get(IMAGE_SET_KEY, editorSettings.getImageSetDefault());
145  configSourceName = PREFERENCES.get(CONFIG_SOURCE_KEY, "");
146  }
147 
148  @NotNull
149  @Override
150  public File getArchDirectory() {
151  return archDirectory;
152  }
153 
154  @Override
155  public void setArchDirectory(@NotNull final File archDirectory) {
156  final File effectiveArchDirectory = IOUtils.getCanonicalFile(archDirectory);
157  if (this.archDirectory.equals(effectiveArchDirectory)) {
158  return;
159  }
160 
161  this.archDirectory = effectiveArchDirectory;
162  PREFERENCES.put(ARCH_DIRECTORY_KEY, effectiveArchDirectory.toString());
163  }
164 
165  @NotNull
166  @Override
167  public File getMapsDirectory() {
168  return mapsDirectory;
169  }
170 
171  @Override
172  public void setMapsDirectory(@NotNull final File mapsDirectory) {
173  setMapsDirectoryInt(mapsDirectory, true);
174  }
175 
181  private void setMapsDirectoryInt(@NotNull final File mapsDirectory, final boolean updatePreferences) {
182  final File effectiveMapsDirectory = IOUtils.getCanonicalFile(mapsDirectory);
183  if (this.mapsDirectory.equals(effectiveMapsDirectory)) {
184  return;
185  }
186 
187  this.mapsDirectory = effectiveMapsDirectory;
188  if (updatePreferences) {
189  PREFERENCES.put(MAP_DIRECTORY_KEY, effectiveMapsDirectory.toString());
190  }
192  }
193 
194  @NotNull
195  @Override
196  public File getMediaDirectory() {
197  return mediaDirectory;
198  }
199 
200  @Override
201  public void setMediaDirectory(@NotNull final File mediaDirectory) {
202  final File effectiveMediaDirectory = IOUtils.getCanonicalFile(mediaDirectory);
203  if (!editorSettings.hasMediaDirectory()) {
204  return;
205  }
206  if (this.mediaDirectory.equals(effectiveMediaDirectory)) {
207  return;
208  }
209 
210  this.mediaDirectory = effectiveMediaDirectory;
211  PREFERENCES.put(MEDIA_DIRECTORY_KEY, effectiveMediaDirectory.getPath());
212  }
213 
214  @NotNull
215  @Override
216  public String getImageSet() {
217  return imageSet;
218  }
219 
220  @Override
221  public void setImageSet(@NotNull final String imageSet) {
222  if (!editorSettings.hasImageSet()) {
223  return;
224  }
225  if (this.imageSet.equals(imageSet)) {
226  return;
227  }
228 
229  this.imageSet = imageSet;
230  PREFERENCES.put(IMAGE_SET_KEY, imageSet);
231  }
232 
233  @NotNull
234  @Override
235  public String getConfigSourceName() {
236  return configSourceName;
237  }
238 
239  @Override
240  public void setConfigSourceName(@NotNull final String configSourceName) {
241  if (this.configSourceName.equals(configSourceName)) {
242  return;
243  }
244 
245  this.configSourceName = configSourceName;
246  PREFERENCES.put(CONFIG_SOURCE_KEY, configSourceName);
247  }
248 
249  @NotNull
250  @Override
251  public String getUserName() {
252  return PREFERENCES.get(PREFERENCES_USER_NAME, editorSettings.getUserNameDefault());
253  }
254 
255  @Override
256  public void setUserName(@NotNull final String userName) {
257  PREFERENCES.put(PREFERENCES_USER_NAME, userName);
258  }
259 
260  @Override
261  public boolean saveIndices() {
262  return true;
263  }
264 
265 }
static final String MEDIA_DIRECTORY_KEY
The preferences key for the media directory.
void setUserName(@NotNull final String userName)
Sets the user name.
final EditorSettings editorSettings
The EditorSettings for retrieving default values.
static final String ARCH_DIRECTORY_KEY
The preferences key for the archetype directory.
String configSourceName
Do we load arches from the collected archives.
String getConfigSourceName()
Returns the name of the configuration source.
static final String PREFERENCES_USER_NAME
Preferences key for user name.
void setConfigSourceName(@NotNull final String configSourceName)
Sets the name of the configuration source.
static File getCanonicalFile(@NotNull final File file)
Calls File#getCanonicalFile().
Definition: IOUtils.java:208
Base package of all Gridarta classes.
void setMediaDirectory(@NotNull final File mediaDirectory)
Sets the media directory.
void fireMapsDirectoryChanged()
Notifies all listeners about a changed maps directory.
boolean saveIndices()
Returns whether indices should be saved to disk.
static final String CONFIG_SOURCE_KEY
The preferences key for configuration source.
AbstractDefaultProjectSettings(@NotNull final EditorSettings editorSettings)
Creates a new instance.
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
boolean hasMediaDirectory()
Returns whether a media directory is used.
static final String IMAGE_SET_KEY
The preferences key for the selected image set.
Interface used as preferences location.
boolean hasImageSet()
Returns whether an image set is used.
void setImageSet(@NotNull final String imageSet)
Sets the image set.
String getUserNameDefault()
Returns the default user name.
void setMapsDirectory(@NotNull final File mapsDirectory)
Sets the default maps directory.
void setMapsDirectoryInt(@NotNull final File mapsDirectory, final boolean updatePreferences)
Sets the mapsDirectory.
void setArchDirectory(@NotNull final File archDirectory)
Sets the archetype directory.
Settings that apply to the editor.
static final String MAP_DIRECTORY_KEY
The preferences key for the archetype directory.
Abstract base class for ProjectSettings implementations.