001: /*
002: ** Copyright (c) 1998 by Timothy Gerard Endres
003: ** <mailto:time@ice.com> <http://www.ice.com>
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.util;
024:
025: import java.io.*;
026: import java.awt.*;
027: import java.awt.Dimension;
028: import java.util.TimeZone;
029: import java.util.Properties;
030: import java.util.Vector;
031:
032: import com.ice.util.UserProperties;
033: import com.ice.util.ResourceUtilities;
034:
035: public abstract class DynamicConfig {
036: protected String name = null;
037: protected File homeDir = null;
038: protected Properties workingProps = null;
039:
040: public DynamicConfig(String name) {
041: this .name = name;
042: this .workingProps = new Properties();
043: this .determineHomeDirectory();
044: }
045:
046: public String getName() {
047: return this .name;
048: }
049:
050: public void saveProperties() throws IOException {
051: UserProperties.saveDynamicProperties(this .name);
052: }
053:
054: public void setProperty(String propName, boolean value) {
055: UserProperties.setDynamicProperty(this .name, propName,
056: (value ? "true" : "false"));
057: }
058:
059: public void setProperty(String propName, int value) {
060: UserProperties.setDynamicProperty(this .name, propName, ""
061: + value);
062: }
063:
064: public void setProperty(String propName, String value) {
065: UserProperties.setDynamicProperty(this .name, propName, value);
066: }
067:
068: public void removeProperty(String propName) {
069: UserProperties.removeDynamicProperty(this .name, propName);
070: }
071:
072: public void setStringArray(String propName, String[] strArray) {
073: StringBuffer buf = new StringBuffer();
074: for (int idx = 0; idx < strArray.length; ++idx) {
075: buf.append(strArray[idx]);
076: if (idx < (strArray.length - 1))
077: buf.append(":");
078: }
079:
080: UserProperties.setDynamicProperty(this .name, propName, buf
081: .toString());
082: }
083:
084: public void setStringArray(String propName, Vector strArray) {
085: int size = strArray.size();
086: StringBuffer buf = new StringBuffer();
087: for (int idx = 0; idx < size; ++idx) {
088: buf.append((String) strArray.elementAt(idx));
089: if (idx < (size - 1))
090: buf.append(":");
091: }
092:
093: UserProperties.setDynamicProperty(this .name, propName, buf
094: .toString());
095: }
096:
097: public File getHomeDirectory() {
098: return this .homeDir;
099: }
100:
101: public Rectangle getBounds(String propName, Rectangle defBounds) {
102: this .workingProps.clear();
103:
104: Rectangle result = new Rectangle();
105: defBounds.x = UserProperties.getProperty(propName + ".x",
106: defBounds.x);
107: defBounds.y = UserProperties.getProperty(propName + ".y",
108: defBounds.y);
109: defBounds.width = UserProperties.getProperty(propName
110: + ".width", defBounds.width);
111: defBounds.height = UserProperties.getProperty(propName
112: + ".height", defBounds.height);
113:
114: return defBounds;
115: }
116:
117: public void saveBounds(String propName, Rectangle bounds) {
118: this .saveBounds(propName, bounds.x, bounds.y, bounds.width,
119: bounds.height);
120: }
121:
122: public void saveBounds(String propName, int x, int y, int w, int h) {
123: this .workingProps.clear();
124:
125: this .workingProps.put(propName + ".x", "" + x);
126: this .workingProps.put(propName + ".y", "" + y);
127: this .workingProps.put(propName + ".width", "" + w);
128: this .workingProps.put(propName + ".height", "" + h);
129:
130: UserProperties.setDynamicProperties(this .name,
131: this .workingProps);
132: }
133:
134: public void saveLocation(String propName, int x, int y) {
135: this .workingProps.clear();
136:
137: this .workingProps.put(propName + ".x", "" + x);
138: this .workingProps.put(propName + ".y", "" + y);
139:
140: UserProperties.setDynamicProperties(this .name,
141: this .workingProps);
142: }
143:
144: public void saveSize(String propName, int w, int h) {
145: this .workingProps.clear();
146:
147: this .workingProps.put(propName + ".width", "" + w);
148: this .workingProps.put(propName + ".height", "" + h);
149:
150: UserProperties.setDynamicProperties(this .name,
151: this .workingProps);
152: }
153:
154: protected boolean isPropertySet(String propName) {
155: boolean result = true;
156:
157: String propValue = UserProperties.getProperty(propName, null);
158:
159: if (propValue == null)
160: result = false;
161:
162: return result;
163: }
164:
165: private void determineHomeDirectory() {
166: String userDirName = System.getProperty("user.home", null);
167:
168: if (userDirName == null) {
169: userDirName = System.getProperty("user.dir", null);
170: }
171:
172: if (userDirName == null) {
173: // REVIEW We are using a questionable algorithm here.
174: this .homeDir = new File((File.separatorChar == ':') ? ":"
175: : ".");
176: } else {
177: this .homeDir = new File(userDirName);
178: }
179: }
180: }
|