KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 package com.buchuki.ensmer;
17
18 import java.io.*;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.*;
21 import javax.media.j3d.Transform3D;
22 import javax.vecmath.*;
23 import com.buchuki.annotations.*;
24 import com.buchuki.ensmer.builtin.*;
25 import com.buchuki.ensmer.object.Backend;
26 import com.buchuki.ensmer.prevayler.queries.*;
27
28
29 /**
30  * This class serves two purposes. First, it behaves as a factory for special
31  * Areas such as inventory, and the areas for adding objects to the scene,
32  * etc. Second, it allows arbitrary objects to associate a name with a
33  * specific Area identifier so that it can be retrieved by that object or
34  * other objects at a later date. This functionality should only be necessary
35  * for objects that are not able to keep a reference to the area's actual
36  * identifier within themselves (for example, a container object that
37  * contains a link to another Area should not have to name the area, as it
38  * can reference the identifier directly), but for objects that need to share
39  * access to an area based on its name. Note to backend designers choosing to
40  * use this functionality: no namespace resolution is done on the string
41  * identifiers, so make sure you choose unique meaningful names. You may want
42  * to include your domain or your backend's package in the name, for example.
43  * Both functions of this class are designed to negate the need to subclass
44  * the Area class to add Area-Specific functionality.
45  *
46  * @author Dusty Phillips [dusty@buchuki.com]
47  */

48 public class SpecialAreaManager {
49
50     /**
51      * Name of the well known built-in addObject area
52      */

53     public final static String JavaDoc ADD_OBJECT = "ensmer.addObjectArea";
54
55     /**
56      * Name of the well-known built-in addStaticObject area
57      */

58     public final static String JavaDoc ADD_STATIC_OBJECT = "ensmer.addStaticObjectArea";
59
60     /**
61      * Name of the well-known built-in inventory area
62      */

63     public final static String JavaDoc INVENTORY = "ensmer.inventory";
64
65     /**
66      * Name of the well-known built-in storage area
67      */

68     public final static String JavaDoc STORAGE = "ensmer.storage";
69
70     /**
71      * Remove a special identifier mapping for a particular special ID.
72      *
73      * @param specialID the special identifier to remove a mapping for
74      * @return the actual ID removed, or null if no such special ID
75      * existed
76      */

77     public Long JavaDoc removeSpecialID(String JavaDoc specialID) {
78         try {
79             return (Long JavaDoc) EnsmerManager.instance().getPrevayler().execute(
80                 new RemoveSpecialAreaIDTransaction(specialID));
81         } catch (Exception JavaDoc e) {
82             e.printStackTrace();
83             return null;
84         }
85     }
86
87     /**
88      * Build the special Add Object area. If the Area has already been built,
89      * nothing happens. Currently it is not possible to dynamically add new
90      * classes of objects at runtime, so once its built, its correct.
91      */

92     public void buildAddObjectArea() {
93         AreaManager man = EnsmerManager.instance().getAreaManager();
94         Long JavaDoc id = getAreaID(ADD_OBJECT);
95         Area area;
96         if (id == null) {
97             area = man.newArea();
98             id = area.getAreaID();
99             setSpecialID(ADD_OBJECT, id);
100         }
101         else {
102             area = man.getArea(id);
103         }
104         area.setReadOnly(false);
105
106         Set<Class JavaDoc<? extends Backend>> classes = EnsmerManager.instance().getBackhoe().getBackendClasses();
107         List<Backend> existingBackends = area.getBackends();
108         //Remove all existing Backends not having an associated class
109
for (Backend backend : existingBackends) {
110             ClassIcon icon = (ClassIcon) backend;
111             if (!classes.contains(icon.getRepresentedClass())) {
112                 area.destroyObject(backend.getId());
113             }
114         }
115         //Add all classes not having existing backends
116
for (Class JavaDoc<? extends Backend> backendClass : classes) {
117             if (!containsClass(existingBackends, backendClass)) {
118                 Object JavaDoc obj = null;
119                 try {
120                     Method JavaDoc getRep = backendClass.getMethod("getRepresentation");
121                     obj = getRep.invoke(null);
122                 } catch (Exception JavaDoc e) {} //obj stays null
123
if (obj != null) {
124                     ClassIcon icon = new ClassIcon();
125                     icon.setRepresentedClass(backendClass);
126                     Matrix4f mat = SceneGraphUtils.randomPositionInFrontOfUser(area);
127                     area.newObject(icon, mat);
128                 }
129             }
130         }
131         area.setReadOnly(true);
132     }
133
134
135     /**
136      * Build the special Add Static Object area. If the Area has already been
137      * built, it is removed and a new one built from scratch. If the prebuilt
138      * area is the current area, then nothing happens.
139      */

140     public void buildAddStaticArea() {
141         EnsmerManager ens = EnsmerManager.instance();
142         AreaManager man = ens.getAreaManager();
143         Long JavaDoc id = getAreaID(ADD_STATIC_OBJECT);
144         Area area;
145         if (id == null) {
146             area = man.newArea();
147             id = area.getAreaID();
148             setSpecialID(ADD_STATIC_OBJECT, id);
149         }
150         else {
151             area = man.getArea(id);
152         }
153         area.setReadOnly(false);
154         File dir = EnsmerManager.instance().getConfigManager().getSettingsDirectory("static");
155         List<String JavaDoc> files = Arrays.asList(dir.list(
156                     new FilenameFilter() {
157                         public boolean accept(File dir, String JavaDoc name) {
158                             return name.endsWith(".wrl");
159                         }
160                     }));
161
162         List<Backend> existingBackends = area.getBackends();
163         //Remove all existing Statics not having an associated file
164
for (Backend backend : existingBackends) {
165             StaticIcon icon = (StaticIcon) backend;
166             if (!files.contains(icon.getFilename())) {
167                 area.destroyObject(backend.getId());
168             }
169         }
170         //Add all files not having existing Backends
171
for (String JavaDoc filename : files) {
172             if (!containsStatic(existingBackends, filename)) {
173                 StaticIcon obj = new StaticIcon();
174                 obj.setFilename(filename);
175                 Matrix4f mat = SceneGraphUtils.randomPositionInFrontOfUser(area);
176                 area.newObject(obj, mat);
177             }
178         }
179
180         /*
181         File dir = EnsmerManager.instance().getConfigManager().
182             getSettingsDirectory("static");
183         if (id != null) {
184             //Don't bother to rebuild if the directory state hasn't changed
185             if (dir.lastModified() == staticLastModified) {
186                 return;
187             }
188             staticLastModified = dir.lastModified();
189             try {
190                 man.removeArea(id);
191             } catch (IllegalStateException e) {
192                 return; //if current area, don't rebuild
193             }
194         }
195         else {
196             staticLastModified = dir.lastModified();
197         }
198         Area newArea = man.newArea();
199         String[] files = dir.list(
200                     new FilenameFilter() {
201                         public boolean accept(File dir, String name) {
202                             return name.endsWith(".wrl");
203                         }
204                     });
205         for (String file : files) {
206             StaticIcon obj = new StaticIcon();
207             obj.setFilename(file);
208             Matrix4f mat = SceneGraphUtils.randomPositionInFrontOfUser(newArea);
209             newArea.newObject(obj, mat);
210         }
211         setSpecialID(ADD_STATIC_OBJECT, newArea.getAreaID());
212         */

213         area.setReadOnly(true);
214     }
215
216     /**
217      * Get a long Area identifier based on a given specialID.
218      *
219      * @param specialID the arbitrary identifier to retrieve an actual Area ID
220      * for.
221      * @return the Long area identifier associated with that special
222      * ID, or null if no identifier is associated
223      */

224     public Long JavaDoc getAreaID(String JavaDoc specialID) {
225         try {
226             return (Long JavaDoc) EnsmerManager.instance().getPrevayler().execute(
227                 new GetAreaIDForSpecial(specialID));
228         } catch (Exception JavaDoc e) {
229             e.printStackTrace();
230             return null;
231         }
232     }
233
234     /**
235      * Set a special identifier for a particular AreaID to the map.
236      *
237      * @param specialID an arbitrary String identifier to associate with an
238      * areaID.
239      * @param areaID the actual (autogenerated) identifier of the area
240      */

241     public void setSpecialID(String JavaDoc specialID, Long JavaDoc areaID) {
242         try {
243             EnsmerManager.instance().getPrevayler().execute(
244                 new SetSpecialAreaIDTransaction(specialID, areaID));
245         } catch (Exception JavaDoc e) {
246             e.printStackTrace();
247         }
248     }
249
250     /**
251      * Return true if the List of backends contains a representation of the
252      * given class
253      *
254      * @param backends List of ClassIcon objects
255      * @param clazz the class to test for existence
256      * @return true if the backends list contans a backend with that
257      * class
258      */

259     private boolean containsClass(List<Backend> backends, Class JavaDoc clazz) {
260         for (Backend backend : backends) {
261             ClassIcon icon = (ClassIcon) backend;
262             if (icon.getRepresentedClass().equals(clazz)) {
263                 return true;
264             }
265         }
266         return false;
267     }
268
269     /**
270      * Return true if the List of backends contains a representation of the
271      * given file.
272      *
273      * @param backends List of StaticIcon objects
274      * @param file the filename to test for existence
275      * @return true if backends contains a backend with that filename
276      */

277     private boolean containsStatic(List<Backend> backends, String JavaDoc file) {
278         for (Backend backend : backends) {
279             StaticIcon icon = (StaticIcon) backend;
280             if (icon.getFilename().equals(file)) {
281                 return true;
282             }
283         }
284         return false;
285     }
286
287     /**
288      * The time the static directory was last modified.
289      */

290     private long staticLastModified;
291
292     /**
293      * The previous set of Class objects used to prevent having to rebuild the
294      * add Object area every time.
295      */

296     private Set<Class JavaDoc<? extends Backend>> classSet;
297 }
298
299
Popular Tags