Source code: org/argouml/configuration/ConfigurationProperties.java
1 // Copyright (c) 1996-2001 The Regents of the University of California. All
2 // Rights Reserved. Permission to use, copy, modify, and distribute this
3 // software and its documentation without fee, and without a written
4 // agreement is hereby granted, provided that the above copyright notice
5 // and this paragraph appear in all copies. This software program and
6 // documentation are copyrighted by The Regents of the University of
7 // California. The software program and documentation are supplied "AS
8 // IS", without any accompanying services from The Regents. The Regents
9 // does not warrant that the operation of the program will be
10 // uninterrupted or error-free. The end-user understands that the program
11 // was developed for research purposes and is advised not to rely
12 // exclusively on the program for any reason. IN NO EVENT SHALL THE
13 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
14 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
15 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
16 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
17 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
18 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
20 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
21 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
22 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
24 //
25 // This code is originally from the open source UML editor argouml.
26 // Information on argouml can be found at: http://argouml.tigris.org/
27 //
28 // The original author is Thierry Lach
29 //
30 // Adopted by Matthieu Cormier on Fri June 27 2003.
31
32 package org.argouml.configuration;
33
34 import java.io.*;
35 import java.awt.event.*;
36 import java.util.*;
37 import java.net.*;
38 import javax.swing.*;
39
40 import simple.logging.Log;
41
42 /**
43 * This class provides a user configuration based upon properties files.
44 * Eventually this configuration file will be
45 * available to users via a GUI interface to
46 * set keyboards
47 * memory allocations
48 * which modules to load
49 * user preferences
50 * font sizes
51 * user names and data
52 * etc.*
53 */
54 public class ConfigurationProperties extends ConfigurationHandler {
55
56 /** The location of Argo's default properties resource.
57 */
58 private static String PROPERTIES = "/org/argouml/resource/default.properties";
59 /** The primary property bundle.
60 */
61 protected Properties _properties = null;
62
63 /** Flag to ensure that only the first load failure is reported
64 * even though we keep trying because the file or URL may only
65 * be temporarily unavailable.
66 */
67 private boolean _canComplain = true;
68
69 /** Anonymous constructor.
70 */
71 public ConfigurationProperties() {
72 super(true);
73 Properties defaults = new Properties();
74 try {
75 defaults.load(getClass().getResourceAsStream(PROPERTIES));
76
77 }
78 catch (Exception ioe)
79 {
80 // needs-more-work: What should we do here?
81 }
82 _properties = new Properties(defaults);
83 }
84
85 /** Returns the default path for user properties.
86 *
87 * @return a generic path string.
88 */
89 public String getDefaultPath() {
90 return System.getProperty("user.home") + "/allusions.user.properties";
91 }
92
93
94 /** Load the configuration from a specified location.
95 *
96 * @param file the path to load the configuration from.
97 *
98 * @return true if the load was successful, false if not.
99 */
100 public boolean loadFile(File file) {
101 try {
102 _properties.load(new FileInputStream(file));
103 Log.out ("Configuration loaded from " + file + "\n");
104 return true;
105 }
106 catch (Exception e) {
107 if (_canComplain)
108 Log.out ("Unable to load configuration " + file + "\n");
109 _canComplain = false;
110 }
111
112 return false;
113 }
114
115 /** Save the configuration to a specified location.
116 *
117 * @param file the path to save the configuration at.
118 *
119 * @return true if the save was successful, false if not.
120 */
121 boolean saveFile(File file) {
122 try {
123 _properties.store(new FileOutputStream(file), "Allusions properties");
124 Log.out ("Configuration saved to " + file);
125 return true;
126 }
127 catch (Exception e) {
128 if (_canComplain)
129 Log.out ("Unable to save configuration " + file + "\n");
130 _canComplain = false;
131 }
132
133 return false;
134 }
135 /** Load the configuration from a specified location.
136 *
137 * @param url the path to load the configuration from.
138 *
139 * @return true if the load was successful, false if not.
140 */
141 public boolean loadURL(URL url) {
142 try {
143 _properties.load(url.openStream());
144 Log.out("Configuration loaded from " + url + "\n");
145 return true;
146 }
147 catch (Exception e) {
148 if (_canComplain)
149 Log.out("Unable to load configuration " + url + "\n");
150 _canComplain = false;
151 return false;
152 }
153 }
154
155 /** Save the configuration to a specified location.
156 *
157 * @param url the path to save the configuration at.
158 *
159 * @return true if the save was successful, false if not.
160 */
161 boolean saveURL(URL url) {
162 // System.out.println ("Configuration saved to " + url + "\n");
163 return false;
164 }
165
166 /** Returns the string value of a configuration property.
167 *
168 * @param key the key to return the value of.
169 * @param defaultValue the value to return if the key was not found.
170 *
171 * @return the string value of the key if found, otherwise null;
172 */
173 public String getValue(String key, String defaultValue) {
174 String result = "";
175 try {
176 result = _properties.getProperty(key, defaultValue);
177 }
178 catch (Exception e) {
179 result = defaultValue;
180 }
181 Log.out("key '" + key + "' returns '" + result + "'");
182 return result;
183 }
184
185 /** Sets the string value of a configuration property.
186 *
187 * @param key the key to set.
188 * @param value the value to set the key to.
189 */
190 public void setValue(String key, String value)
191 {
192 Log.out("key '" + key + "' set to '" + value + "'");
193 _properties.setProperty(key, value);
194 }
195 }
196