001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib;
049:
050: import java.io.File;
051: import java.awt.Component;
052: import java.util.List;
053: import java.util.HashMap;
054: import java.util.Iterator;
055: import java.util.ArrayList;
056: import java.beans.PropertyChangeEvent;
057: import java.beans.PropertyChangeListener;
058:
059: import javax.swing.UIManager;
060: import javax.swing.LookAndFeel;
061: import javax.swing.SwingUtilities;
062:
063: import org.apache.commons.vfs.FileObject;
064: import org.apache.commons.vfs.FileSystemException;
065:
066: import org.apache.commons.logging.Log;
067: import org.apache.commons.logging.LogFactory;
068:
069: import com.anthonyeden.lib.util.ClassUtilities;
070:
071: /** This class provides basic services which all applications may find useful.
072: The services include location or creation of the home directory,
073: installation of additional look and feels, and a mutable map of application
074: attributes.
075:
076: @author Anthony Eden
077: */
078:
079: public class Application {
080:
081: private static final Log log = LogFactory.getLog(Application.class);
082: private static final String[] additionalLookAndFeels = { "com.incors.plaf.kunststoff.KunststoffLookAndFeel" };
083:
084: private static HashMap apps = new HashMap();
085:
086: private ArrayList windows = new ArrayList();
087: private HashMap attributes = new HashMap();
088: private File homeDirectory;
089: private FileObject vfsHomeDirectory;
090: private String appName;
091:
092: /** Construct an application with the given name.
093:
094: @param appName The name of the application
095: */
096:
097: public Application(String appName) {
098: this .appName = appName;
099: this .homeDirectory = new File(System.getProperty("user.home"),
100: "." + appName);
101: }
102:
103: /** Get a instance of an Application object for the named application. If
104: the application does not exist then one will be created.
105:
106: @return The Application instance
107: */
108:
109: public static synchronized Application getInstance(String appName) {
110: Application app = (Application) apps.get(appName);
111: if (app == null) {
112: app = new Application(appName);
113: apps.put(appName, app);
114: }
115: return app;
116: }
117:
118: /** Get the current user's home directory. If the directory does
119: not exist then it will be created.
120:
121: @return The home directory
122: */
123:
124: public synchronized File getHomeDirectory() {
125: if (!homeDirectory.exists()) {
126: homeDirectory.mkdirs();
127: }
128: return homeDirectory;
129: }
130:
131: /** Get the current user's virtual home directory. If the virtual
132: directory does not exist then it will be created.
133:
134: @return The virtual home directory
135: @throws FileSystemException
136: */
137:
138: public synchronized FileObject getVFSHomeDirectory()
139: throws FileSystemException {
140: if (!vfsHomeDirectory.exists()) {
141: vfsHomeDirectory.createFolder();
142: }
143: return vfsHomeDirectory;
144: }
145:
146: /** Initialize the application using the given command line arguments.
147:
148: @param args The command line arguments
149: */
150:
151: public void initialize(String[] args) {
152: // install additional Look and Feels
153: for (int i = 0; i < additionalLookAndFeels.length; i++) {
154: try {
155: String className = additionalLookAndFeels[i];
156: LookAndFeel lookAndFeel = (LookAndFeel) ClassUtilities
157: .loadClass(className, this ).newInstance();
158: UIManager.installLookAndFeel(lookAndFeel.getName(),
159: className);
160: } catch (Exception e) {
161: log.error("Error installing look and feel: "
162: + e.getMessage());
163: }
164: }
165: }
166:
167: /** Get a list of open window objects.
168:
169: @return A List of window objects
170: */
171:
172: public List getWindows() {
173: return windows;
174: }
175:
176: /** Add a window to the list of window objects.
177:
178: @param window The window
179: */
180:
181: public void addWindow(Component window) {
182: windows.add(window);
183:
184: log.debug("Window added");
185: }
186:
187: /** Remove a window from the list of window objects.
188:
189: @param window The window
190: */
191:
192: public void removeWindow(Component window) {
193: windows.remove(window);
194:
195: log.debug("Window removed");
196: log.debug("Window count: " + windows.size());
197:
198: if (windows.size() == 0) {
199: System.exit(0);
200: }
201: }
202:
203: /** Get the attribute for the specified key.
204:
205: @param key The attribute key
206: @return The attribute value or null
207: */
208:
209: public Object getAttribute(Object key) {
210: return attributes.get(key);
211: }
212:
213: /** Set the attribute.
214:
215: @param key The attribute key
216: @param value The attribute value
217: */
218:
219: public void setAttribute(Object key, Object value) {
220: attributes.put(key, value);
221: }
222:
223: /** Return an Iterator for all attribute keys.
224:
225: @return An Iterator of attribute keys
226: */
227:
228: public Iterator getAttributeKeys() {
229: return attributes.keySet().iterator();
230: }
231:
232: /** Remove the specified attribute.
233:
234: @param key The attribute key
235: @return The removed object or null
236: */
237:
238: public Object removeAttribute(Object key) {
239: return attributes.remove(key);
240: }
241:
242: /** Clear all attributes. */
243:
244: public void clearAttributes() {
245: attributes.clear();
246: }
247:
248: }
|