KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > edit > tree > DrawComponentTreeEditPart


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.edit.tree;
29
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.eclipse.gef.EditPolicy;
35 import org.eclipse.gef.editparts.AbstractTreeEditPart;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.ui.views.properties.IPropertySource;
38
39 import org.nightlabs.editor2d.DrawComponent;
40 import org.nightlabs.editor2d.editpolicy.DrawComponentEditPolicy;
41 import org.nightlabs.editor2d.editpolicy.tree.DrawComponentTreeEditPolicy;
42 import org.nightlabs.editor2d.model.DrawComponentPropertySource;
43
44
45 public abstract class DrawComponentTreeEditPart
46 extends AbstractTreeEditPart
47 {
48   public static final Logger LOGGER = Logger.getLogger(DrawComponentTreeEditPart.class);
49   protected IPropertySource propertySource = null;
50     
51   /**
52    * Creates a new DrawComponentTreeEditPart instance.
53    * @param model
54    */

55   public DrawComponentTreeEditPart(DrawComponent drawComponent) {
56     super(drawComponent);
57   }
58   
59   /* (non-Javadoc)
60    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
61    */

62   public Object JavaDoc getAdapter(Class JavaDoc key)
63   {
64     /* override the default behavior defined in AbstractEditPart
65     * which would expect the model to be a property sourced.
66     * instead the editpart can provide a property source
67     */

68     if (IPropertySource.class == key)
69     {
70       return getPropertySource();
71     }
72     return super.getAdapter(key);
73   }
74   
75   /* (non-Javadoc)
76    * @see org.eclipse.gef.editparts.AbstractTreeEditPart#getImage()
77    */

78   protected abstract Image getImage();
79   
80   /* (non-Javadoc)
81    * @see com.ibm.itso.sal330r.gefdemo.edit.WorkflowElementEditPart#getPropertySource()
82    */

83   protected IPropertySource getPropertySource()
84   {
85     if (propertySource == null)
86     {
87       propertySource =
88         new DrawComponentPropertySource(getDrawComponent());
89     }
90     return propertySource;
91   }
92   
93   /* (non-Javadoc)
94    * @see org.eclipse.gef.editparts.AbstractTreeEditPart#getText()
95    */

96   protected String JavaDoc getText()
97   {
98     return getDrawComponent().getName();
99   }
100
101   /**
102    * Returns the model as <code>DrawComponent</code>.
103    * @return the model as <code>DrawComponent</code>
104    */

105   public DrawComponent getDrawComponent() {
106     return (DrawComponent) getModel();
107   }
108     
109
110   /* (non-Javadoc)
111    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
112    */

113   public void activate()
114   {
115     if (isActive())
116         return;
117
118     // start listening for changes in the model
119
hookIntoDrawComponent(getDrawComponent());
120
121     super.activate();
122   }
123
124
125   /* (non-Javadoc)
126    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
127    */

128   public void deactivate()
129   {
130     if (!isActive())
131         return;
132
133     // stop listening for changes in the model
134
unhookFromDrawComponent(getDrawComponent());
135
136     super.deactivate();
137   }
138
139   /**
140    * Registers this edit part as a listener for change notifications
141    * to the specified workflow element.
142    *
143    * @param element the drawComponent element that should be observed
144    * for change notifications
145    */

146   protected void hookIntoDrawComponent(DrawComponent element)
147   {
148     if (element != null)
149       element.addPropertyChangeListener(listener);
150   }
151
152   /**
153    * Removes this edit part from the specified drawComponent element.
154    * Thus, it will no longe receive change notifications.
155    *
156    * @param element the drawComponent element that should not be observed
157    * any more
158    */

159   protected void unhookFromDrawComponent(DrawComponent element)
160   {
161     if (element != null)
162       element.removePropertyChangeListener(listener);
163   }
164   
165   /**
166    * Creates and installs pertinent EditPolicies
167    * for this.
168    */

169   protected void createEditPolicies()
170   {
171     installEditPolicy(EditPolicy.COMPONENT_ROLE, new DrawComponentEditPolicy());
172     installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new DrawComponentTreeEditPolicy());
173   }
174   
175   protected PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc(){
176         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
177             propertyChanged(evt);
178         }
179     };
180   
181     protected void propertyChanged(PropertyChangeEvent JavaDoc evt)
182     {
183         String JavaDoc propertyName = evt.getPropertyName();
184         if (propertyName.equals(DrawComponent.PROP_BOUNDS)) {
185 // LOGGER.debug(propertyName+"changed!");
186
refreshVisuals();
187         }
188         else if (propertyName.equals(DrawComponent.PROP_HEIGHT)) {
189 // LOGGER.debug(propertyName+"changed!");
190
refreshVisuals();
191         }
192         else if (propertyName.equals(DrawComponent.PROP_WIDTH)) {
193 // LOGGER.debug(propertyName+"changed!");
194
refreshVisuals();
195         }
196         else if (propertyName.equals(DrawComponent.PROP_X)) {
197 // LOGGER.debug(propertyName+"changed!");
198
refreshVisuals();
199         }
200         else if (propertyName.equals(DrawComponent.PROP_Y)) {
201 // LOGGER.debug(propertyName+"changed!");
202
refreshVisuals();
203         }
204         else if (propertyName.equals(DrawComponent.PROP_ROTATION)) {
205 // LOGGER.debug(propertyName+"changed!");
206
refreshVisuals();
207         }
208         else if (propertyName.equals(DrawComponent.PROP_ROTATION_X)) {
209 // LOGGER.debug(propertyName+"changed!");
210
refreshVisuals();
211         }
212         else if (propertyName.equals(DrawComponent.PROP_ROTATION_Y)) {
213 // LOGGER.debug(propertyName+"changed!");
214
refreshVisuals();
215         }
216         else if (propertyName.equals(DrawComponent.PROP_RENDER_MODE)) {
217 // LOGGER.debug(propertyName+"changed!");
218
refreshVisuals();
219         }
220         else if (propertyName.equals(DrawComponent.PROP_NAME)) {
221 // LOGGER.debug(propertyName+"changed!");
222
refreshVisuals();
223         }
224         else if (propertyName.equals(DrawComponent.PROP_LANGUAGE_ID)) {
225 // LOGGER.debug(propertyName+"changed!");
226
refreshVisuals();
227         }
228         
229     }
230     
231 }
232
Popular Tags