source: trunk/java-client/org/nzdl/gsdl/util/NzdlPreferences.java@ 2273

Last change on this file since 2273 was 2273, checked in by daven, 23 years ago

retain state of non-explicit preferences (e.g. casfolding, stemming)

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/*
2 * NzdlPreferences.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20//the package we're in
21package org.nzdl.gsdl.util;
22
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileOutputStream;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.OutputStream;
29import java.util.Hashtable;
30import java.util.Properties;
31
32
33/**
34 * Stores our preferences...
35 */
36public final class NzdlPreferences {
37 /** The hashtable we write to disk ... */
38 protected Properties _preferences = null;
39 /**
40 * Should we save the preferences? Should normally be true for
41 * GUI work and false for commandline and/or server work.
42 */
43 public static boolean saving = true;
44 /** The singleton instance */
45 static NzdlPreferences instance = null;
46 static String preferencesFileName = "preferences";
47
48 protected NzdlPreferences() {
49 try {
50 _preferences = new Properties();
51 File file = new File(preferencesFileName);
52 InputStream stream = new FileInputStream(file);
53 _preferences.load(stream);
54 } catch (Exception exception) {
55 System.out.println("Unable to load file " + preferencesFileName+ "\n" + exception);
56 }
57 }
58
59 public void save() {
60 if (!saving)
61 return;
62 try {
63 File file = new File(preferencesFileName);
64 OutputStream stream = new FileOutputStream(file);
65 _preferences.store(stream,"Preferences ...");
66 } catch (Exception exception) {
67 System.out.println("Unable to save file " + preferencesFileName + "\n" + exception);
68 System.out.println("preferences =" + _preferences);
69 }
70 }
71
72 /**
73 * Get the static Preferences object
74 */
75 public static NzdlPreferences getInstance() {
76 if (instance == null)
77 instance = new NzdlPreferences();
78 return instance;
79 }
80 /**
81 * Get a boolean preference
82 */
83 public static boolean getBoolean(String key){
84 String value = (String) getInstance()._preferences.get(key);
85 if (value == null)
86 {
87 setBoolean(key,false);
88 return false;
89 }
90 return Boolean.valueOf(value).booleanValue();
91 }
92 /**
93 * Set a boolean preference
94 */
95 public static void setBoolean(String key,boolean value) {
96 getInstance()._preferences.put(key,"" + value);
97 getInstance().save();
98 }
99
100
101 public static boolean isBoolean(String key) {
102 String value = (String) getInstance()._preferences.get(key);
103 if (value == null)
104 return false;
105 return true;
106 }
107
108 /**
109 * Get a String preference
110 */
111 public static String getString(String key){
112 String value = (String) getInstance()._preferences.get(key);
113 if (value == null)
114 {
115 setString(key,"");
116 return "";
117 }
118 return value;
119 }
120
121 public static boolean isString(String key){
122 //String value = (String) getInstance()._preferences.get(key);
123 if (getInstance()._preferences.get(key) == null )
124 return false;
125 else
126 return true;
127 }
128
129
130
131 /**
132 * Set a String preference
133 */
134 public static void setString(String key, String value) {
135 getInstance()._preferences.put(key, value);
136 getInstance().save();
137 }
138}
139
Note: See TracBrowser for help on using the repository browser.