KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > edit > AbstractDrawComponentEditPart


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: 26.10.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7  **/

8 package com.nightlabs.editor2d.edit;
9
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.draw2d.IFigure;
16 import org.eclipse.draw2d.geometry.Rectangle;
17 import org.eclipse.gef.EditPart;
18 import org.eclipse.gef.GraphicalEditPart;
19 import org.eclipse.gef.Request;
20 import org.eclipse.gef.RootEditPart;
21 import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
22 import org.eclipse.gef.editparts.ZoomManager;
23 import org.eclipse.ui.views.properties.IPropertySource;
24
25 import com.nightlabs.editor2d.DrawComponent;
26 import com.nightlabs.editor2d.figures.DrawComponentFigure;
27 import com.nightlabs.editor2d.figures.RendererFigure;
28 import com.nightlabs.editor2d.model.DrawComponentPropertySource;
29 import com.nightlabs.editor2d.request.EditorRequestConstants;
30 import com.nightlabs.editor2d.util.EditorUtil;
31 import com.nightlabs.editor2d.util.J2DUtil;
32
33 public abstract class AbstractDrawComponentEditPart
34 extends AbstractGraphicalEditPart
35 implements EditorRequestConstants
36 {
37   public static final Logger LOGGER = Logger.getLogger(AbstractDrawComponentEditPart.class);
38   
39   protected IPropertySource propertySource = null;
40   
41   public AbstractDrawComponentEditPart(DrawComponent drawComponent)
42   {
43     setModel(drawComponent);
44   }
45   
46 // /*
47
// * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
48
// */
49
// protected abstract IFigure createFigure();
50
protected IFigure createFigure()
51   {
52     DrawComponentFigure figure = new DrawComponentFigure();
53     figure.setDrawComponent(getDrawComponent());
54     addRenderer(figure);
55     addZoomListener(figure);
56     return figure;
57   }
58   
59   protected void addRenderer(DrawComponentFigure figure)
60   {
61     // add Renderer
62
if (getDrawComponent().getRenderer() != null) {
63         figure.setRenderer(getDrawComponent().getRenderer());
64     }
65     else {
66       if (getModelRoot() != null) {
67         getDrawComponent().setRenderModeManager(getModelRoot().getMultiLayerDrawComponent().getRenderModeManager());
68       }
69     }
70   }
71   
72   public MultiLayerDrawComponentEditPart getModelRoot()
73   {
74     if (getRoot() instanceof MultiLayerDrawComponentEditPart) {
75         return (MultiLayerDrawComponentEditPart) getRoot();
76     }
77     else {
78         for (Iterator JavaDoc it = getRoot().getChildren().iterator(); it.hasNext(); ) {
79             Object JavaDoc o = it.next();
80             if (o instanceof MultiLayerDrawComponentEditPart) {
81                 return (MultiLayerDrawComponentEditPart) o;
82             }
83         }
84     }
85     return null;
86   }
87   
88   protected void addZoomListener(DrawComponentFigure figure)
89   {
90     ZoomManager zoomManager = EditorUtil.getZoomManager(this);
91     if (zoomManager != null) {
92       zoomManager.addZoomListener(figure.getZoomListener());
93     }
94   }
95   
96   protected RendererFigure getRendererFigure()
97   {
98     return (RendererFigure) getFigure();
99   }
100   /*
101    * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
102    */

103   protected abstract void createEditPolicies();
104   
105   /*
106    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
107    */

108   public void activate()
109   {
110     if (isActive())
111       return;
112     
113     // start listening for changes in the model
114
hookIntoDrawComponent(getDrawComponent());
115     
116     super.activate();
117   }
118   
119   /*
120    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
121    */

122   public void deactivate()
123   {
124     if (!isActive())
125       return;
126     
127     // stop listening for changes in the model
128
unhookFromDrawComponent(getDrawComponent());
129     
130     super.deactivate();
131   }
132   
133   public DrawComponent getDrawComponent() {
134     return (DrawComponent) getModel();
135   }
136         
137   protected void refreshVisuals()
138   {
139     Rectangle r = new Rectangle(J2DUtil.toDraw2D(getDrawComponent().getBounds()));
140     
141     ((GraphicalEditPart) getParent()).setLayoutConstraint(
142         this,
143         getFigure(),
144         r);
145     
146     if (getFigure() instanceof DrawComponentFigure) {
147       getRendererFigure().setRenderer(getDrawComponent().getRenderer());
148       getRendererFigure().setDrawComponent(getDrawComponent());
149     }
150     updateLayer();
151   }
152   
153   public void updateLayer()
154   {
155     LayerEditPart layerEditPart = getLayerEditPart();
156     if (layerEditPart != null)
157       layerEditPart.getBufferedFreeformLayer().refresh();
158     
159 // LOGGER.debug("Update Layer!");
160
}
161   
162   protected LayerEditPart getLayerEditPart()
163   {
164     EditPart parent = getParent();
165     if (parent == null)
166       throw new IllegalStateException JavaDoc("Member parent may not be null for DrawComponent"+this.toString());
167     
168     if (this instanceof LayerEditPart)
169       return (LayerEditPart) this;
170     if (this instanceof MultiLayerDrawComponentEditPart)
171       return null;
172     if (this instanceof RootEditPart)
173       return null;
174     if (parent instanceof LayerEditPart)
175       return (LayerEditPart) parent;
176     
177     while (!(parent instanceof LayerEditPart)) {
178       parent = parent.getParent();
179     }
180     return (LayerEditPart) parent;
181   }
182   
183   /* (non-Javadoc)
184    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
185    */

186   public Object JavaDoc getAdapter(Class JavaDoc key)
187   {
188     /* override the default behavior defined in AbstractEditPart
189      * which would expect the model to be a property sourced.
190      * instead the editpart can provide a property source
191      */

192     if (IPropertySource.class == key)
193     {
194       return getPropertySource();
195     }
196     return super.getAdapter(key);
197   }
198   
199   /* (non-Javadoc)
200    * @see com.ibm.itso.sal330r.gefdemo.edit.WorkflowElementEditPart#getPropertySource()
201    */

202   protected IPropertySource getPropertySource()
203   {
204     if (propertySource == null)
205     {
206       propertySource =
207         new DrawComponentPropertySource(getDrawComponent());
208     }
209     return propertySource;
210   }
211   
212   protected PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc(){
213         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
214             propertyChanged(evt);
215         }
216     };
217   
218     protected void propertyChanged(PropertyChangeEvent JavaDoc evt)
219     {
220         String JavaDoc propertyName = evt.getPropertyName();
221         
222         if (propertyName.equals(DrawComponent.PROP_BOUNDS)) {
223             LOGGER.debug(propertyName+"changed!");
224             refreshVisuals();
225         }
226         else if (propertyName.equals(DrawComponent.PROP_HEIGHT)) {
227             LOGGER.debug(propertyName+"changed!");
228             refreshVisuals();
229         }
230         else if (propertyName.equals(DrawComponent.PROP_WIDTH)) {
231             LOGGER.debug(propertyName+"changed!");
232             refreshVisuals();
233         }
234         else if (propertyName.equals(DrawComponent.PROP_X)) {
235             LOGGER.debug(propertyName+"changed!");
236             refreshVisuals();
237         }
238         else if (propertyName.equals(DrawComponent.PROP_Y)) {
239             LOGGER.debug(propertyName+"changed!");
240             refreshVisuals();
241         }
242         else if (propertyName.equals(DrawComponent.PROP_ROTATION)) {
243             LOGGER.debug(propertyName+"changed!");
244             refreshVisuals();
245         }
246         else if (propertyName.equals(DrawComponent.PROP_ROTATION_X)) {
247             LOGGER.debug(propertyName+"changed!");
248             refreshVisuals();
249         }
250         else if (propertyName.equals(DrawComponent.PROP_ROTATION_Y)) {
251             LOGGER.debug(propertyName+"changed!");
252             refreshVisuals();
253         }
254         else if (propertyName.equals(DrawComponent.PROP_RENDER_MODE)) {
255             LOGGER.debug(propertyName+"changed!");
256             refreshVisuals();
257         }
258     }
259       
260   /**
261    * Registers this edit part as a listener for change notifications
262    * to the specified DrawComponent element.
263    *
264    * @param element the DrawComponent element that should be observed
265    * for change notifications
266    */

267   protected void hookIntoDrawComponent(DrawComponent element)
268   {
269     if (element != null)
270       element.addPropertyChangeListener(listener);
271   }
272   
273   /**
274    * Removes this edit part from the specified DrawComponent element.
275    * Thus, it will no longe receive change notifications.
276    *
277    * @param element the DrawComponent element that should not be observed
278    * any more
279    */

280   protected void unhookFromDrawComponent(DrawComponent element)
281   {
282     if (element != null)
283       element.removePropertyChangeListener(listener);
284   }
285             
286   public boolean understandsRequest(Request req)
287   {
288     if (req.getType().equals(REQ_ROTATE))
289       return true;
290
291     else if (req.getType().equals(REQ_EDIT_ROTATE_CENTER))
292       return true;
293
294     else if (req.getType().equals(REQ_SHEAR))
295       return true;
296     
297     return super.understandsRequest(req);
298   }
299 }
300
Popular Tags