KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package com.buchuki.ensmer.builtin;
18
19 import com.buchuki.ensmer.*;
20 import com.buchuki.ensmer.object.Backend;
21 import com.buchuki.annotations.*;
22 import java.io.Serializable JavaDoc;
23 import javax.vecmath.*;
24
25 /**
26  * Class to represent a special type of backend that basically maintains a
27  * reference to another backend. This is used in the buildAddObjectArea() method
28  * in the SpecialAreaManager, but could also be used to allow certain types of
29  * objects to be added to the scene directly. The backend provides methods to
30  * return new instances of the represented backend.
31  *
32  * @author Dusty Phillips [dusty@buchuki.com]
33  */

34 public class ClassIcon extends Backend {
35     
36     /**
37      * Construct a default instance
38      */

39     public ClassIcon() {}
40     
41     /**
42      * Construct a serialized instance.
43      *
44      * @param data the data to reconstruct from
45      */

46     public ClassIcon(Serializable JavaDoc data) {
47         representedClass = (Class JavaDoc) data;
48     }
49     
50     /**
51      * Get the serializable data for this object.
52      * @return the serializable data for this object
53      */

54     @Override JavaDoc
55     public Serializable JavaDoc getSerializable() {
56         return representedClass;
57     }
58     
59     /**
60      * Get the class associated with the object
61      * @return the class associated with the object
62      */

63     public Class JavaDoc getRepresentedClass() {
64         return representedClass;
65     }
66     
67     /**
68      * Set the backend class associated with the object
69      * @param backendClass the backend class associated with the object
70      */

71     public void setRepresentedClass(Class JavaDoc backendClass) {
72         this.representedClass = backendClass;
73         fireChangeEvent();
74     }
75     
76     /**
77      * Instantiate a new instance of the BackendClass and add it to the previous
78      * to current Area, THEN move to the previous area. This method is
79      * very tightly coupled to the design of the
80      * SpecialAreaManager's ADD_OBJECT area. It is basically a workaround to
81      * the fact that Areas are not meant to be customizeable. If too much of
82      * this type of method occurs, then this may be considered a design flaw.
83      */

84     public void addObjectToPrevious() {
85         try {
86             AreaManager man = EnsmerManager.instance().getAreaManager();
87             /*
88              * Special case for Static objects: show what's available in a new area.
89              * This illustrates how inelegant special cases are. I am currently
90              * assuming that Static will be the *only* such special case, as
91              * it has been treated specially from the beginning (it contains
92              * scenegraph information in the backend). If this turns out to
93              * not be the case, Backend may require a special newInstance()
94              * method that allows any object to take care of initting itself.
95              */

96             if (representedClass.getName().equals("com.buchuki.ensmer.builtin.Static")) {
97                 SpecialAreaManager spec = EnsmerManager.instance().getSpecialAreaManager();
98                 spec.buildAddStaticArea();
99                 man.setCurrentArea(spec.getAreaID(spec.ADD_STATIC_OBJECT));
100                 return;
101             }
102             int count = 0;
103             Area prev;
104             do {
105                 Long JavaDoc prevId = man.getHistoricalAreaID(count);
106                 if (prevId == null) {
107                     return; //theoretically should never have to be called, as the original area should always be non-read only
108
}
109                 prev = man.getArea(prevId);
110                 count++;
111             } while (prev.isReadOnly());
112             Backend backend = (Backend) representedClass.newInstance();
113             Matrix4f location = SceneGraphUtils.positionInFrontOfUser(prev);
114             prev.newObject(backend, location);
115             //go to previous area where the object was added
116
for (int i = 0; i < count - 1; i++) {
117                 man.previousArea();
118             }
119             //in case the area didn't get changed
120
EnsmerManager.instance().getUserManager().getInputManager().systemMode();
121             EnsmerManager.instance().getSelectionManager().clearSelection();
122             EnsmerManager.instance().getSelectionManager().toggleSelection(backend.getId());
123         } catch (Exception JavaDoc e) {
124             e.printStackTrace();
125         }
126     }
127     
128     
129     private Class JavaDoc representedClass;
130 }
131
Popular Tags