KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 12.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.edit.tree;
9
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.gef.EditPolicy;
15 import org.eclipse.gef.editparts.AbstractTreeEditPart;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.ui.views.properties.IPropertySource;
18
19 import com.nightlabs.editor2d.DrawComponent;
20 import com.nightlabs.editor2d.editpolicy.DrawComponentEditPolicy;
21 import com.nightlabs.editor2d.editpolicy.tree.DrawComponentTreeEditPolicy;
22 import com.nightlabs.editor2d.model.DrawComponentPropertySource;
23
24
25 public abstract class DrawComponentTreeEditPart
26 extends AbstractTreeEditPart
27 {
28   public static final Logger LOGGER = Logger.getLogger(DrawComponentTreeEditPart.class);
29   private IPropertySource propertySource = null;
30     
31   /**
32    * Creates a new DrawComponentTreeEditPart instance.
33    * @param model
34    */

35   public DrawComponentTreeEditPart(DrawComponent drawComponent) {
36     super(drawComponent);
37   }
38   
39   /* (non-Javadoc)
40    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
41    */

42   public Object JavaDoc getAdapter(Class JavaDoc key)
43   {
44     /* override the default behavior defined in AbstractEditPart
45     * which would expect the model to be a property sourced.
46     * instead the editpart can provide a property source
47     */

48     if (IPropertySource.class == key)
49     {
50       return getPropertySource();
51     }
52     return super.getAdapter(key);
53   }
54   
55   /* (non-Javadoc)
56    * @see org.eclipse.gef.editparts.AbstractTreeEditPart#getImage()
57    */

58   protected abstract Image getImage();
59   
60   /* (non-Javadoc)
61    * @see com.ibm.itso.sal330r.gefdemo.edit.WorkflowElementEditPart#getPropertySource()
62    */

63   protected IPropertySource getPropertySource()
64   {
65     if (propertySource == null)
66     {
67       propertySource =
68         new DrawComponentPropertySource(getDrawComponent());
69     }
70     return propertySource;
71   }
72   
73   /* (non-Javadoc)
74    * @see org.eclipse.gef.editparts.AbstractTreeEditPart#getText()
75    */

76   protected String JavaDoc getText()
77   {
78     return getDrawComponent().getName();
79 // // TODO Default Tree Name should come from somewhere else
80
// // respectively the default name should already be set at this point
81
// return
82
// (null != getDrawComponent().getName()
83
// ? getDrawComponent().getName()
84
// : EditorPlugin.getResourceString("outline_unnamed"));
85
}
86
87   /**
88    * Returns the model as <code>DrawComponent</code>.
89    * @return the model as <code>DrawComponent</code>
90    */

91   public DrawComponent getDrawComponent() {
92     return (DrawComponent) getModel();
93   }
94     
95
96   /* (non-Javadoc)
97    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
98    */

99   public void activate()
100   {
101     if (isActive())
102         return;
103
104     // start listening for changes in the model
105
hookIntoDrawComponent(getDrawComponent());
106
107     super.activate();
108   }
109
110
111   /* (non-Javadoc)
112    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
113    */

114   public void deactivate()
115   {
116     if (!isActive())
117         return;
118
119     // stop listening for changes in the model
120
unhookFromDrawComponent(getDrawComponent());
121
122     super.deactivate();
123   }
124
125   /**
126    * Registers this edit part as a listener for change notifications
127    * to the specified workflow element.
128    *
129    * @param element the drawComponent element that should be observed
130    * for change notifications
131    */

132   protected void hookIntoDrawComponent(DrawComponent element)
133   {
134     if (element != null)
135       element.addPropertyChangeListener(listener);
136   }
137
138   /**
139    * Removes this edit part from the specified drawComponent element.
140    * Thus, it will no longe receive change notifications.
141    *
142    * @param element the drawComponent element that should not be observed
143    * any more
144    */

145   protected void unhookFromDrawComponent(DrawComponent element)
146   {
147     if (element != null)
148       element.removePropertyChangeListener(listener);
149   }
150   
151   /**
152    * Creates and installs pertinent EditPolicies
153    * for this.
154    */

155   protected void createEditPolicies()
156   {
157     installEditPolicy(EditPolicy.COMPONENT_ROLE, new DrawComponentEditPolicy());
158     installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new DrawComponentTreeEditPolicy());
159   }
160   
161   protected PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc(){
162         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
163             propertyChanged(evt);
164         }
165     };
166   
167     protected void propertyChanged(PropertyChangeEvent JavaDoc evt)
168     {
169         String JavaDoc propertyName = evt.getPropertyName();
170         if (propertyName.equals(DrawComponent.PROP_BOUNDS)) {
171             LOGGER.debug(propertyName+"changed!");
172             refreshVisuals();
173         }
174         else if (propertyName.equals(DrawComponent.PROP_HEIGHT)) {
175             LOGGER.debug(propertyName+"changed!");
176             refreshVisuals();
177         }
178         else if (propertyName.equals(DrawComponent.PROP_WIDTH)) {
179             LOGGER.debug(propertyName+"changed!");
180             refreshVisuals();
181         }
182         else if (propertyName.equals(DrawComponent.PROP_X)) {
183             LOGGER.debug(propertyName+"changed!");
184             refreshVisuals();
185         }
186         else if (propertyName.equals(DrawComponent.PROP_Y)) {
187             LOGGER.debug(propertyName+"changed!");
188             refreshVisuals();
189         }
190         else if (propertyName.equals(DrawComponent.PROP_ROTATION)) {
191             LOGGER.debug(propertyName+"changed!");
192             refreshVisuals();
193         }
194         else if (propertyName.equals(DrawComponent.PROP_ROTATION_X)) {
195             LOGGER.debug(propertyName+"changed!");
196             refreshVisuals();
197         }
198         else if (propertyName.equals(DrawComponent.PROP_ROTATION_Y)) {
199             LOGGER.debug(propertyName+"changed!");
200             refreshVisuals();
201         }
202         else if (propertyName.equals(DrawComponent.PROP_RENDER_MODE)) {
203             LOGGER.debug(propertyName+"changed!");
204             refreshVisuals();
205         }
206         else if (propertyName.equals(DrawComponent.PROP_NAME)) {
207             LOGGER.debug(propertyName+"changed!");
208             refreshVisuals();
209         }
210         else if (propertyName.equals(DrawComponent.PROP_LANGUAGE_ID)) {
211             LOGGER.debug(propertyName+"changed!");
212             refreshVisuals();
213         }
214         
215     }
216     
217 ///* (non-Javadoc)
218
//* @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
219
//*/
220
//public void notifyChanged(Notification notification)
221
//{
222
// int type = notification.getEventType();
223
//
224
// switch (type)
225
// {
226
// case Notification.ADD :
227
// case Notification.ADD_MANY :
228
// case Notification.REMOVE :
229
// case Notification.REMOVE_MANY :
230
// // TODO: need more info to know if children need refresh?
231
//// LOGGER.debug("refreshChildren!");
232
// refreshChildren();
233
// break;
234
//
235
// case Notification.SET :
236
//// LOGGER.debug("refreshVisuals!");
237
// refreshVisuals();
238
// break;
239
// }
240
//}
241
}
242
Popular Tags