KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > object > SelectableFrontend


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.object;
18
19 import com.buchuki.ensmer.EnsmerManager;
20 import com.buchuki.ensmer.SelectionManager;
21 import com.buchuki.ensmer.input.ObjectInputManager;
22 import com.buchuki.ensmer.input.event.semantic.*;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.awt.event.ItemListener JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.media.j3d.Appearance;
27 import javax.media.j3d.Material;
28
29 /**
30  * Abstract implementation of Frontend that takes care of allowing objects to
31  * emit on selection and focus. Whitish light is emitted on selection, and
32  * bluish light on focus.
33  *
34  * @author Dusty Phillips [dusty@buchuki.com]
35  */

36 public abstract class SelectableFrontend implements Frontend, ItemListener JavaDoc, FocusListener {
37     
38     /**
39      * Creates a new instance of SelectableFrontend. Ensure this object
40      * is set up as a SelectionListener and FocusListener.
41      */

42     public SelectableFrontend() {
43         EnsmerManager.instance().getSelectionManager().addItemListener(this);
44         EnsmerManager.instance().getUserManager().getInputManager().
45             getObjectManager().addFocusListener(this);
46     }
47     
48     /**
49      * Notification of an ItemEvent. This method is called whenever an item
50      * is selected in the SelectionManager
51      *
52      * @param event the ItemEvent generated
53      */

54     public void itemStateChanged(ItemEvent JavaDoc event) {
55         if (!event.getItem().equals(getBackend().getId())) {
56             return;
57         }
58         updateSelection();
59     }
60     
61     public void focusChanged(FocusEvent event) {
62         if (!event.getFocusedObject().equals(getBackend().getId())) {
63             return;
64         }
65         updateSelection();
66     }
67     
68     /**
69      * Set the collection of Appearance nodes that should have their emmissive properties
70      * set on selection and focus.
71      */

72     protected void setAppearanceCollection(Collection JavaDoc<Appearance> collection) {
73         this.appearanceNodes = collection;
74         updateSelection();
75     }
76     
77     /**
78      * Check the value of the selection and set up emmissive properties accordingly
79      */

80     private void updateSelection() {
81         if (appearanceNodes == null || appearanceNodes.isEmpty()) {
82             return;
83         }
84         SelectionManager sel = EnsmerManager.instance().getSelectionManager();
85         ObjectInputManager obj = EnsmerManager.instance().getUserManager().getInputManager().getObjectManager();
86         if (getBackend().getId().equals(obj.getFocusedObject())) {
87             for (Appearance app : appearanceNodes) {
88                 Material matt = app.getMaterial();
89                 if (matt != null) {
90                     matt.setEmissiveColor(.4f, .4f, 1.0f);
91                 }
92             }
93         }
94         else if (sel.isSelected(getBackend().getId())) {
95             for (Appearance app : appearanceNodes) {
96                 Material matt = app.getMaterial();
97                 if (matt != null) {
98                     matt.setEmissiveColor(.5f, .5f, .5f);
99                 }
100             }
101         }
102         else {
103             for (Appearance app : appearanceNodes) {
104                 Material matt = app.getMaterial();
105                 if (matt != null) {
106                     matt.setEmissiveColor(0, 0, 0);
107                 }
108             }
109         }
110     }
111     
112     /**
113      * destroy the listeners
114      */

115     public void destroy() {
116         EnsmerManager.instance().getSelectionManager().removeItemListener(this);
117         EnsmerManager.instance().getUserManager().getInputManager().
118             getObjectManager().removeFocusListener(this);
119     }
120     
121     /**
122      * Collection of Appearance nodes that should have their emmissive properties
123      * set on selection and focus.
124      */

125     private Collection JavaDoc<Appearance> appearanceNodes;
126 }
127
Popular Tags