001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.settings;
021
022import java.io.File;
023import java.util.prefs.PreferenceChangeEvent;
024import java.util.prefs.PreferenceChangeListener;
025import java.util.prefs.Preferences;
026import net.sf.gridarta.MainControl;
027import net.sf.gridarta.utils.ActionBuilderUtils;
028import net.sf.japi.swing.action.ActionBuilder;
029import net.sf.japi.swing.action.ActionBuilderFactory;
030import org.jetbrains.annotations.NotNull;
031
032/**
033 * Default implementation of {@link GlobalSettings}.
034 * @author Andreas Kirschbaum
035 */
036public abstract class DefaultGlobalSettings extends AbstractGlobalSettings {
037
038    /**
039     * The {@link ActionBuilder}.
040     */
041    @NotNull
042    private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
043
044    /**
045     * The preferences key for the archetype directory.
046     */
047    @NotNull
048    private static final String MAP_DIRECTORY_KEY = "mapDirectory";
049
050    /**
051     * The preferences key for the archetype directory.
052     */
053    @NotNull
054    private static final String ARCH_DIRECTORY_KEY = "archDirectory";
055
056    /**
057     * The preferences key for the media directory.
058     */
059    @NotNull
060    private static final String MEDIA_DIRECTORY_KEY = "mediaDirectory";
061
062    /**
063     * The preferences key for the selected image set.
064     */
065    @NotNull
066    private static final String IMAGE_SET_KEY = "useImageSet";
067
068    /**
069     * The preferences key for storing the last known documentation version.
070     */
071    @NotNull
072    private static final String DOCUMENTATION_VERSION_KEY = "docuVersion";
073
074    /**
075     * Key for info whether the main window's toolbar is shown.
076     */
077    @NotNull
078    private static final String SHOW_MAIN_TOOLBAR_KEY = "ShowMainToolbar";
079
080    /**
081     * Preferences key for user name.
082     */
083    @NotNull
084    private static final String PREFERENCES_USER_NAME = "username";
085
086    /**
087     * Preferences default value for user name.
088     */
089    @NotNull
090    private static final String PREFERENCES_USER_NAME_DEFAULT = System.getProperty("user.name");
091
092    /**
093     * The preferences key for configuration source.
094     */
095    @NotNull
096    private static final String CONFIG_SOURCE_KEY = "configSource";
097
098    /**
099     * Preferences.
100     */
101    @NotNull
102    private static final Preferences preferences = Preferences.userNodeForPackage(MainControl.class);
103
104    /**
105     * The default value for the maps directory.
106     */
107    @NotNull
108    private final String mapsDirectoryDefault;
109
110    /**
111     * Whether a media directory is used.
112     */
113    private final boolean hasMediaDefaultDirectory;
114
115    /**
116     * The default value for the media directory.
117     */
118    @NotNull
119    private final String mediaDirectoryDefault;
120
121    /**
122     * Whether an image set is used.
123     */
124    private final boolean hasImageSet;
125
126    /**
127     * The default value for the image set.
128     */
129    @NotNull
130    private final String imageSetDefault;
131
132    /**
133     * The default value for the archetype directory.
134     */
135    @NotNull
136    private final String archDirectoryDefault;
137
138    /**
139     * The archetype directory.
140     */
141    @NotNull
142    private File archDirectory;
143
144    /**
145     * The default maps directory.
146     */
147    @NotNull
148    private File mapsDirectory;
149
150    /**
151     * The media directory.
152     */
153    @NotNull
154    private File mediaDirectory;
155
156    /**
157     * The image set.
158     */
159    @NotNull
160    private String imageSet;
161
162    /**
163     * The image directory. By default, created images are stored in this
164     * directory.
165     */
166    @NotNull
167    private File imageDirectory;
168
169    /**
170     * Do we load arches from the collected archives.
171     */
172    @NotNull
173    private String configSourceName;
174
175    /**
176     * Time for an automated documentation popup.
177     */
178    private boolean autoPopupDocumentation;
179
180    /**
181     * The the default directory for saving maps.
182     */
183    @NotNull
184    private File currentSaveMapDirectory;
185
186    /**
187     * Creates a new instance.
188     */
189    protected DefaultGlobalSettings() {
190        archDirectoryDefault = ActionBuilderUtils.getString(ACTION_BUILDER, "archDirectoryDefault", "");
191        mapsDirectoryDefault = ActionBuilderUtils.getString(ACTION_BUILDER, "mapsDirectoryDefault", "");
192        hasMediaDefaultDirectory = ActionBuilderUtils.getBoolean(ACTION_BUILDER, "mediaDirectory");
193        mediaDirectoryDefault = ActionBuilderUtils.getString(ACTION_BUILDER, "mediaDirectoryDefault", "");
194        hasImageSet = ActionBuilderUtils.getBoolean(ACTION_BUILDER, "imageSet");
195        imageSetDefault = ActionBuilderUtils.getString(ACTION_BUILDER, "imageSetDefault", "none");
196
197        final PreferenceChangeListener preferenceChangeListener = new PreferenceChangeListener() {
198
199            @Override
200            public void preferenceChange(final PreferenceChangeEvent evt) {
201                if (evt.getKey().equals(SHOW_MAIN_TOOLBAR_KEY)) {
202                    fireShowMainToolbarChanged();
203                } else if (evt.getKey().equals(ARCH_DIRECTORY_KEY)) {
204                    archDirectory = new File(preferences.get(ARCH_DIRECTORY_KEY, archDirectoryDefault));
205                } else if (evt.getKey().equals(MAP_DIRECTORY_KEY)) {
206                    setMapsDirectoryInt(new File(preferences.get(MAP_DIRECTORY_KEY, mapsDirectoryDefault)), false);
207                } else if (evt.getKey().equals(MEDIA_DIRECTORY_KEY)) {
208                    mediaDirectory = new File(preferences.get(MEDIA_DIRECTORY_KEY, mediaDirectoryDefault));
209                } else if (evt.getKey().equals(IMAGE_SET_KEY)) {
210                    imageSet = preferences.get(IMAGE_SET_KEY, imageSetDefault);
211                } else if (evt.getKey().equals(CONFIG_SOURCE_KEY)) {
212                    configSourceName = preferences.get(CONFIG_SOURCE_KEY, "");
213                }
214            }
215
216        };
217        preferences.addPreferenceChangeListener(preferenceChangeListener);
218
219        archDirectory = new File(preferences.get(ARCH_DIRECTORY_KEY, archDirectoryDefault));
220        mapsDirectory = new File(preferences.get(MAP_DIRECTORY_KEY, mapsDirectoryDefault));
221        mediaDirectory = new File(preferences.get(MEDIA_DIRECTORY_KEY, mediaDirectoryDefault));
222        imageSet = preferences.get(IMAGE_SET_KEY, imageSetDefault);
223        imageDirectory = mapsDirectory;
224        configSourceName = preferences.get(CONFIG_SOURCE_KEY, "");
225        final int documentationVersion = ActionBuilderUtils.getInt(ACTION_BUILDER, "docuVersion", 0);
226        if (documentationVersion > 0 && preferences.getInt(DOCUMENTATION_VERSION_KEY, 0) < documentationVersion) {
227            // remember to open documentation
228            autoPopupDocumentation = true;
229            // update documentation version right now, because we want the help popup only one time
230            preferences.putInt(DOCUMENTATION_VERSION_KEY, documentationVersion);
231        }
232
233        currentSaveMapDirectory = mapsDirectory.exists() ? mapsDirectory : new File(System.getProperty("user.dir"));
234    }
235
236    /**
237     * {@inheritDoc}
238     */
239    @NotNull
240    @Override
241    public File getArchDirectoryDefault() {
242        return new File(archDirectoryDefault);
243    }
244
245    /**
246     * {@inheritDoc}
247     */
248    @NotNull
249    @Override
250    public File getArchDirectory() {
251        return archDirectory;
252    }
253
254    /**
255     * {@inheritDoc}
256     */
257    @Override
258    public void setArchDirectory(@NotNull final File archDirectory) {
259        if (this.archDirectory.equals(archDirectory)) {
260            return;
261        }
262
263        this.archDirectory = archDirectory;
264        preferences.put(ARCH_DIRECTORY_KEY, archDirectory.toString());
265    }
266
267    /**
268     * {@inheritDoc}
269     */
270    @NotNull
271    @Override
272    public File getMapsDirectoryDefault() {
273        return new File(mapsDirectoryDefault);
274    }
275
276    /**
277     * {@inheritDoc}
278     */
279    @NotNull
280    @Override
281    public File getMapsDirectory() {
282        return mapsDirectory;
283    }
284
285    /**
286     * {@inheritDoc}
287     */
288    @Override
289    public void setMapsDirectory(@NotNull final File mapsDirectory) {
290        setMapsDirectoryInt(mapsDirectory, true);
291    }
292
293    /**
294     * Sets the {@link #mapsDirectory}.
295     * @param mapsDirectory the new maps directory
296     * @param updatePreferences whether the preferences should be updated
297     */
298    private void setMapsDirectoryInt(@NotNull final File mapsDirectory, final boolean updatePreferences) {
299        if (this.mapsDirectory.equals(mapsDirectory)) {
300            return;
301        }
302
303        this.mapsDirectory = mapsDirectory;
304        if (updatePreferences) {
305            preferences.put(MAP_DIRECTORY_KEY, mapsDirectory.toString());
306        }
307        fireMapsDirectoryChanged();
308    }
309
310    /**
311     * {@inheritDoc}
312     */
313    @Override
314    public boolean hasMediaDirectory() {
315        return hasMediaDefaultDirectory;
316    }
317
318    /**
319     * {@inheritDoc}
320     */
321    @NotNull
322    @Override
323    public File getMediaDirectoryDefault() {
324        return new File(mediaDirectoryDefault);
325    }
326
327    /**
328     * {@inheritDoc}
329     */
330    @NotNull
331    @Override
332    public File getMediaDirectory() {
333        return mediaDirectory;
334    }
335
336    /**
337     * {@inheritDoc}
338     */
339    @Override
340    public void setMediaDirectory(@NotNull final File mediaDirectory) {
341        if (!hasMediaDefaultDirectory) {
342            return;
343        }
344        if (this.mediaDirectory.equals(mediaDirectory)) {
345            return;
346        }
347
348        this.mediaDirectory = mediaDirectory;
349        preferences.put(MEDIA_DIRECTORY_KEY, mediaDirectory.getPath());
350    }
351
352    /**
353     * {@inheritDoc}
354     */
355    @Override
356    public boolean hasImageSet() {
357        return hasImageSet;
358    }
359
360    /**
361     * {@inheritDoc}
362     */
363    @NotNull
364    @Override
365    public String getImageSetDefault() {
366        return imageSetDefault;
367    }
368
369    /**
370     * {@inheritDoc}
371     */
372    @NotNull
373    @Override
374    public String getImageSet() {
375        return imageSet;
376    }
377
378    /**
379     * {@inheritDoc}
380     */
381    @Override
382    public void setImageSet(@NotNull final String imageSet) {
383        if (!hasImageSet) {
384            return;
385        }
386        if (this.imageSet.equals(imageSet)) {
387            return;
388        }
389
390        this.imageSet = imageSet;
391        preferences.put(IMAGE_SET_KEY, imageSet);
392    }
393
394    /**
395     * {@inheritDoc}
396     */
397    @NotNull
398    @Override
399    public File getImageDirectory() {
400        return imageDirectory;
401    }
402
403    /**
404     * {@inheritDoc}
405     */
406    @Override
407    public void setImageDirectory(@NotNull final File imageDirectory) {
408        this.imageDirectory = imageDirectory;
409    }
410
411    /**
412     * {@inheritDoc}
413     */
414    @NotNull
415    @Override
416    public String getConfigSourceName() {
417        return configSourceName;
418    }
419
420    /**
421     * {@inheritDoc}
422     */
423    @Override
424    public void setConfigSourceName(@NotNull final String configSourceName) {
425        if (this.configSourceName.equals(configSourceName)) {
426            return;
427        }
428
429        this.configSourceName = configSourceName;
430        preferences.put(CONFIG_SOURCE_KEY, configSourceName);
431    }
432
433    /**
434     * {@inheritDoc}
435     */
436    @NotNull
437    @Override
438    public File getCurrentSaveMapDirectory() {
439        return currentSaveMapDirectory;
440    }
441
442    /**
443     * {@inheritDoc}
444     */
445    @Override
446    public void setCurrentSaveMapDirectory(@NotNull final File currentSaveMapDirectory) {
447        this.currentSaveMapDirectory = currentSaveMapDirectory;
448    }
449
450    /**
451     * {@inheritDoc}
452     */
453    @Override
454    public boolean isAutoPopupDocumentation() {
455        return autoPopupDocumentation;
456    }
457
458    /**
459     * {@inheritDoc}
460     */
461    @Override
462    public void setAutoPopupDocumentation(final boolean autoPopupDocumentation) {
463        this.autoPopupDocumentation = autoPopupDocumentation;
464    }
465
466    /**
467     * Returns whether the main toolbar should be shown.
468     * @return whether the main toolbar should be shown
469     */
470    @Override
471    public boolean isShowMainToolbar() {
472        return preferences.getBoolean(SHOW_MAIN_TOOLBAR_KEY, SHOW_MAIN_TOOLBAR_DEFAULT);
473    }
474
475    /**
476     * {@inheritDoc}
477     */
478    @Override
479    public void setShowMainToolbar(final boolean selected) {
480        preferences.putBoolean(SHOW_MAIN_TOOLBAR_KEY, selected);
481    }
482
483    /**
484     * {@inheritDoc}
485     */
486    @NotNull
487    @Override
488    public String getUserName() {
489        return preferences.get(PREFERENCES_USER_NAME, PREFERENCES_USER_NAME_DEFAULT);
490    }
491
492    /**
493     * {@inheritDoc}
494     */
495    @NotNull
496    @Override
497    public String getUserNameDefault() {
498        return PREFERENCES_USER_NAME_DEFAULT;
499    }
500
501    /**
502     * {@inheritDoc}
503     */
504    @Override
505    public void setUserName(@NotNull final String userName) {
506        preferences.put(PREFERENCES_USER_NAME, userName);
507    }
508
509    /**
510     * {@inheritDoc}
511     */
512    @Override
513    public boolean saveIndices() {
514        return true;
515    }
516
517}