KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > builtin > Container


1 /*
2  * Copyright 2004-2005 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
17 package com.buchuki.ensmer.builtin;
18
19 import com.buchuki.ensmer.*;
20 import com.buchuki.ensmer.input.event.semantic.*;
21 import com.buchuki.ensmer.object.Backend;
22 import com.buchuki.ensmer.text.TextInput;
23 import com.sun.j3d.utils.geometry.Text2D;
24 import java.awt.Font JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import javax.media.j3d.*;
28 import javax.vecmath.Color3f;
29
30 /**
31  * Class to represent a container of objects, with commands to add selected
32  * object and enter the container. This class is actually more generic than
33  * a container in that it behaves as a link to a new Area. It adds a command
34  * to the new area to remove selected objects and place them in the previously
35  * active area.
36  *
37  * @author Dusty Phillips [dusty@buchuki.com]
38  */

39 public class Container extends Backend {
40     
41     /**
42      * Set up and initialize a default Container. Create the area for it (empty)
43      * and add the command
44      */

45     public Container() {
46         area = EnsmerManager.instance().getAreaManager().newArea();
47         setupCommands();
48     }
49     
50     /**
51      * Set up and initialize a serialized container. Load the area for it
52      * and add the command
53      *
54      * @param data the serialized data to load the container from
55      */

56     public Container(Serializable JavaDoc data) {
57         AreaManager man = EnsmerManager.instance().getAreaManager();
58         area = man.getArea((Long JavaDoc) data);
59         setupCommands();
60     }
61     
62     /**
63      * Get the Serializable object associated with the backend.
64      *
65      * @return the Serializable associated with the backend
66      */

67     @Override JavaDoc
68     public Serializable JavaDoc getSerializable() {
69         return area.getAreaID();
70     }
71     
72     /**
73      * When we get an ID, set up the Configurator defaults
74      */

75     public void setId(Long JavaDoc id) {
76         super.setId(id);
77         if (getConfigProperty("name") == null) {
78             setConfigProperty("name", "");
79         }
80     }
81     
82     /**
83      * Get the Area associated with the Backend. Having this public could be a
84      * security issue, but it allows the area command to be set up in the
85      * Frontend. Since getSerializable returns the id anyway, the Area can be
86      * accessed anyway.
87      */

88     public Area getArea() {
89         return area;
90     }
91     
92     /**
93      * Remove all currently selected objects from the current area and place
94      * them in the container's area in front of the user's current view.
95      */

96     public void addSelectedObjects() {
97         Area current = EnsmerManager.instance().getAreaManager().getCurrentArea();
98         for (Object JavaDoc obj : EnsmerManager.instance().getSelectionManager().getSelectedObjects()) {
99             Long JavaDoc id = (Long JavaDoc) obj;
100             if (id != getId()) {
101                 current.removeObject(id);
102                 area.addObject(id);
103                 area.setObjectPosition(id, SceneGraphUtils.randomPositionInFrontOfUser(area));
104             }
105         }
106         fireChangeEvent();
107     }
108     
109     /**
110      * If the container's area is the current area, remove all currently selected
111      * objects from the container's area and place them in the previous area.
112      */

113     public void removeSelectedObjects() {
114         AreaManager man = EnsmerManager.instance().getAreaManager();
115         SelectionManager sel = EnsmerManager.instance().getSelectionManager();
116         if (man.getCurrentArea() != area) {
117             return;
118         }
119         Area prev = man.getArea(man.getHistoricalAreaID(1));
120         if (prev == null || prev.isReadOnly()) {
121             return;
122         }
123         Object JavaDoc[] objs = sel.getSelectedObjects();
124         for (Object JavaDoc obj : objs) {
125             Long JavaDoc id = (Long JavaDoc) obj;
126             area.removeObject(id);
127             prev.addObject(id);
128             prev.setObjectPosition(id, SceneGraphUtils.randomPositionInFrontOfUser(prev));
129         }
130         man.previousArea();
131         for (Object JavaDoc obj : objs) {
132             sel.toggleSelection((Long JavaDoc) obj);
133         }
134     }
135     
136     /**
137      * Set the container's area as the current area. The previous are automatically
138      * becomes the previous area.
139      */

140     public void lookInside() {
141         EnsmerManager.instance().getUserManager().getInputManager().systemMode();
142         EnsmerManager.instance().getAreaManager().setCurrentArea(area.getAreaID());
143     }
144     
145     /**
146      * Return a Branchgroup representing the <b>type</b> of this object so that
147      * Container objects can be manually added to the scene. Probably an interesting
148      * objicon could be created, but for now it simply creates a text2D containing
149      * the word 'Container'.
150      *
151      * @return a branchgroup representing the class of Static objects
152      */

153     @Override JavaDoc
154     public static BranchGroup getRepresentation() {
155         Text2D text = new Text2D("Container", new Color3f(0f, .6f, .4f), "Times", 16, Font.BOLD);
156         Appearance app = text.getAppearance();
157         PolygonAttributes poly = new PolygonAttributes();
158         poly.setCullFace(poly.CULL_NONE);
159         app.setPolygonAttributes(poly);
160         BranchGroup ret = new BranchGroup();
161         ret.addChild(text);
162         return ret;
163     }
164     
165     /**
166      * The area where the contained objects are stored.
167      */

168     private Area area;
169     
170     /**
171      * This method sets up the backend as a listener for its own destruction
172      * on the AreaManager; when it is destroyed, the associated area is also
173      * removed.
174      */

175     private void setupCommands() {
176         
177         EnsmerManager.instance().getAreaManager().addObjectListener(
178             new ObjectListener() {
179                 public void objectStateChanged(ObjectEvent event) {
180                     if (event.getObject() != getId()) {
181                         return;
182                     }
183                     if (event.getAction() == ObjectEvent.EventType.OBJECT_DESTROYED) {
184                         AreaManager man = EnsmerManager.instance().getAreaManager();
185                         man.removeArea(area.getAreaID());
186                         man.removeObjectListener(this);
187                     }
188                 }
189         });
190     }
191 }
192
Popular Tags