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

Last change on this file since 2277 was 2277, checked in by say1, 23 years ago

fixed properties problem. saving IORs and properties in the users home directory. ChangeLog

  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 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 String dir = System.getProperties().getProperty("user.home");
52 String sep = System.getProperties().getProperty("file.separator");
53
54 File file = new File(dir + sep + preferencesFileName);
55 InputStream stream = new FileInputStream(file);
56 _preferences.load(stream);
57 } catch (Exception exception) {
58 System.out.println("Unable to load file " + preferencesFileName+ "\n" + exception);
59 }
60 }
61
62 public void save() {
63 if (!saving)
64 return;
65 try {
66 String dir = System.getProperties().getProperty("user.home");
67 String sep = System.getProperties().getProperty("file.separator");
68
69 File file = new File(dir + sep + preferencesFileName);
70 OutputStream stream = new FileOutputStream(file);
71 _preferences.store(stream,"Preferences ...");
72 } catch (Exception exception) {
73 System.out.println("Unable to save file " + preferencesFileName + "\n" + exception);
74 System.out.println("preferences =" + _preferences);
75 }
76 }
77
78 /**
79 * Get the static Preferences object
80 */
81 public static NzdlPreferences getInstance() {
82 if (instance == null)
83 instance = new NzdlPreferences();
84 return instance;
85 }
86 /**
87 * Get a boolean preference
88 */
89 public static boolean getBoolean(String key){
90 String value = (String) getInstance()._preferences.get(key);
91 if (value == null)
92 {
93 setBoolean(key,false);
94 return false;
95 }
96 return Boolean.valueOf(value).booleanValue();
97 }
98 /**
99 * Set a boolean preference
100 */
101 public static void setBoolean(String key,boolean value) {
102 getInstance()._preferences.put(key,"" + value);
103 getInstance().save();
104 }
105
106
107 public static boolean isBoolean(String key) {
108 String value = (String) getInstance()._preferences.get(key);
109 if (value == null)
110 return false;
111 return true;
112 }
113
114 /**
115 * Get a String preference
116 */
117 public static String getString(String key){
118 String value = (String) getInstance()._preferences.get(key);
119 if (value == null)
120 {
121 setString(key,"");
122 return "";
123 }
124 return value;
125 }
126
127 public static boolean isString(String key){
128 //String value = (String) getInstance()._preferences.get(key);
129 if (getInstance()._preferences.get(key) == null )
130 return false;
131 else
132 return true;
133 }
134
135
136
137 /**
138 * Set a String preference
139 */
140 public static void setString(String key, String value) {
141 getInstance()._preferences.put(key, value);
142 getInstance().save();
143 }
144}
145
Note: See TracBrowser for help on using the repository browser.