KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > ConfigManager


1
2 /*
3  * Copyright 2005 Dusty Phillips
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package com.buchuki.ensmer;
18
19 import java.io.*;
20 import java.util.*;
21 import javax.vecmath.Matrix4f;
22 import com.buchuki.ensmer.builtin.Configurator;
23 import com.buchuki.ensmer.input.InputManager;
24 import com.buchuki.ensmer.input.event.semantic.*;
25 import com.buchuki.ensmer.prevayler.queries.*;
26
27 import org.prevayler.Prevayler;
28
29 /**
30  * Manages configuration information for Ensmer. Currently it only manages
31  * the location of the settings directory. In the future it will likely
32  * maintain generic lists of properties, some of which will be prevayled, and
33  * some, such as the settings directory, will be instance-specific.
34  *
35  * @author Dusty Phillips [dusty@buchuki.com]
36  */

37 public class ConfigManager {
38
39     /**
40      * Creates a new instance of ConfigManager with a default settings
41      * directory, a subdirectory of the user.home system property. If this
42      * cannot be found either, then the current directory is used.
43      */

44     public ConfigManager() {
45         this(null);
46     }
47
48     /**
49      * Creates a new instance of ConfigManager
50      *
51      * @param settingsDirectory The location of the directory that
52      * contains the user settings. If the directory does not exist, it is
53      * created.
54      * @throws IllegalArgumentException if the file is not a directory or
55      * cannot be read or written
56      */

57     public ConfigManager(File settingsDirectory) {
58         if (settingsDirectory == null) {
59             settingsDirectory = new File(
60                 System.getProperty("user.home", ".") + File.separator + ".ensmer");
61         }
62         if (!settingsDirectory.exists()) {
63             String JavaDoc ensmerHome = System.getenv("ENSMER_HOME");
64             if (ensmerHome == null) {
65                 ensmerHome = ".";
66             }
67             File skeleton = new File(ensmerHome, "skeleton");
68             if (skeleton.exists() && skeleton.isDirectory()) {
69                 try {
70                     com.buchuki.util.FileUtils.copyDirectory(skeleton, settingsDirectory);
71                 } catch (IOException e) {
72                     settingsDirectory.mkdirs();
73                 }
74             }
75             else {
76                 settingsDirectory.mkdirs();
77             }
78         }
79         if (!settingsDirectory.isDirectory()) {
80             throw new IllegalArgumentException JavaDoc("Settings directory " + settingsDirectory.getAbsolutePath() + " points to a regular file");
81         }
82         if (!settingsDirectory.canRead() || !settingsDirectory.canWrite()) {
83             throw new IllegalArgumentException JavaDoc("Insufficient privleges to access settings directory");
84         }
85
86         this.settingsDirectory = settingsDirectory;
87     }
88
89     /**
90      * Show the properties object for the first selected object. If no
91      * properties object has been created, an empty one is created.
92      *
93      * @return boolean value indicating whether or not a configurator was
94      * shown Should only be false if no objects are selected
95      */

96     public boolean showConfigurator() {
97         EnsmerManager ensmer = EnsmerManager.instance();
98         SelectionManager sel = ensmer.getSelectionManager();
99         Prevayler prevayler = ensmer.getPrevayler();
100         InputManager inMan = ensmer.getUserManager().getInputManager();
101         final Area current = ensmer.getAreaManager().getCurrentArea();
102         final Object JavaDoc[] ids = sel.getSelectedObjects();
103         if (ids == null) {
104             return false; //no configurator to show
105
}
106         Configurator config = getConfigurator((Long JavaDoc) ids[0]);
107         final Long JavaDoc id = config.getId();
108         Matrix4f loc = SceneGraphUtils.positionInFrontOfUser(current, 1);
109         try {
110             prevayler.execute(new SetObjectLocationTransaction(id, loc));
111         } catch (Exception JavaDoc e) {
112             e.printStackTrace();
113         }
114
115         inMan.getObjectManager().addFocusListener(
116                     new FocusListener() {
117                         public void focusChanged(FocusEvent e) {
118                             if (e.getFocusedObject() == id) {
119                                 if (!e.gainedFocus()) {
120                                     current.removeObject(id);
121                                 }
122                             }
123                         }
124                     });
125
126         current.addObject(config);
127         inMan.objectMode(id);
128         return true;
129     }
130
131     /**
132      * Get a file pointing to the current settings Directory
133      *
134      * @return the current settings directory
135      */

136     public File getSettingsDirectory() {
137         return settingsDirectory;
138     }
139
140     /**
141      * Get a file pointing to a subfile of the current settings directory.
142      *
143      * @param subfile the relative path to the subdirectory
144      * @return file pointing to the subdir in settingsDirectory. The
145      * subfile MAY NOT EXIST
146      */

147     public File getSettingsDirectory(String JavaDoc subfile) {
148         return new File(settingsDirectory, subfile);
149     }
150
151     /**
152      * Get the configurator object for a particular object. This method creates
153      * a new Backend for the object in question each time. If no configurator
154      * object exists, an empty one is created.
155      *
156      * @param id the identifier of the object that you wish to get the
157      * configurator for.
158      * @return a Configurator associated with the object ID
159      */

160     public Configurator getConfigurator(final Long JavaDoc id) {
161         Prevayler prevayler = EnsmerManager.instance().getPrevayler();
162         try {
163             Long JavaDoc configID = (Long JavaDoc) prevayler.execute(new GetConfiguratorIDQuery(id));
164             Configurator config;
165             if (configCache.containsKey(configID)) {
166                 return configCache.get(configID);
167             }
168             else if (configID == null) {
169                 //create a new one
170
configID = (Long JavaDoc) prevayler.execute(new GetNextObjectIDQuery());
171                 config = new Configurator(id);
172                 prevayler.execute(new AddObjectTransaction(
173                     configID, config.getClass()));
174                 prevayler.execute(new SetObjectDataTransaction(
175                     configID, config.getSerializable()));
176                 config.setId(configID);
177                 prevayler.execute(new SetConfigPropertiesTransaction(id, configID));
178             }
179             else {
180                 //create a backend for the old one
181
config = (Configurator)
182                     EnsmerManager.instance().getBackhoe().getBackend(configID);
183             }
184             config.addChangeListener(new StoreChangesListener());
185             configCache.put(configID, config);
186             return config;
187         } catch (Exception JavaDoc e) {
188             e.printStackTrace();
189             return null;
190         }
191     }
192
193     /**
194      * Called whenever an object is destroyed. If a Configurator exists, this
195      * method will remove it as well.
196      *
197      * @param id Description of the Parameter
198      */

199     void objectDestroyed(Long JavaDoc id) {
200         Prevayler prevayler = EnsmerManager.instance().getPrevayler();
201         try {
202             Long JavaDoc configID = (Long JavaDoc) prevayler.execute(new GetConfiguratorIDQuery(id));
203             if (configID != null) {
204                 configCache.remove(configID);
205                 prevayler.execute(new RemoveObjectTransaction(id));
206             }
207         } catch (Exception JavaDoc e) {
208             e.printStackTrace();
209         }
210     }
211
212     /**
213      * The location of the user settings directory.
214      */

215     private File settingsDirectory;
216
217     /**
218      * Cache of configurator objects. Maps Config ID to its configurator
219      */

220     private Map<Long JavaDoc, Configurator> configCache = new HashMap<Long JavaDoc, Configurator>();
221 }
222
223
Popular Tags