KickJava   Java API By Example, From Geeks To Geeks.

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


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;
29
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.eclipse.draw2d.IFigure;
36 import org.eclipse.draw2d.Label;
37 import org.eclipse.draw2d.geometry.Rectangle;
38 import org.eclipse.gef.GraphicalEditPart;
39 import org.eclipse.gef.Request;
40 import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
41 import org.eclipse.gef.editparts.ZoomManager;
42 import org.eclipse.ui.views.properties.IPropertySource;
43 import org.nightlabs.editor2d.DrawComponent;
44 import org.nightlabs.editor2d.figures.DrawComponentFigure;
45 import org.nightlabs.editor2d.figures.RendererFigure;
46 import org.nightlabs.editor2d.model.DrawComponentPropertySource;
47 import org.nightlabs.editor2d.request.EditorRequestConstants;
48 import org.nightlabs.editor2d.util.EditorUtil;
49 import org.nightlabs.editor2d.util.J2DUtil;
50 import org.nightlabs.editor2d.viewer.descriptor.DescriptorManager;
51
52 public abstract class AbstractDrawComponentEditPart
53 extends AbstractGraphicalEditPart
54 implements EditorRequestConstants
55 {
56   public static final Logger LOGGER = Logger.getLogger(AbstractDrawComponentEditPart.class);
57   
58   protected IPropertySource propertySource = null;
59   
60   public AbstractDrawComponentEditPart(DrawComponent drawComponent)
61   {
62     setModel(drawComponent);
63   }
64   
65   protected Label tooltip = new Label();
66   protected Label getTooltip()
67   {
68     tooltip.setText(getTooltipText(getDrawComponent()));
69     return tooltip;
70   }
71   
72   protected String JavaDoc getTooltipText(DrawComponent dc)
73   {
74     DescriptorManager descMan = getModelRoot().getDescriptorManager();
75     descMan.setDrawComponent(getDrawComponent());
76     return descMan.getEntriesAsString(true);
77   }
78    
79   public void updateTooltip()
80   {
81     if (getModelRoot().getPreferencesConfigModule().isShowToolTips())
82         getFigure().setToolTip(getTooltip());
83     else
84         getFigure().setToolTip(null);
85   }
86   
87   protected IFigure createFigure()
88   {
89     RendererFigure figure = new DrawComponentFigure();
90 // figure.setDescriptorManager(getModelRoot().getDescriptorManager());
91
figure.setDrawComponent(getDrawComponent());
92     addRenderer(figure);
93     if (figure instanceof DrawComponentFigure) {
94       addZoomListener((DrawComponentFigure)figure);
95     }
96     figure.setToolTip(getTooltip());
97     return figure;
98   }
99       
100   protected void addRenderer(RendererFigure figure)
101   {
102     // add Renderer
103
if (getDrawComponent().getRenderer() != null) {
104         figure.setRenderer(getDrawComponent().getRenderer());
105     }
106     else {
107       if (getModelRoot() != null) {
108         getDrawComponent().setRenderModeManager(getModelRoot().getMultiLayerDrawComponent().getRenderModeManager());
109       }
110     }
111   }
112   
113   public MultiLayerDrawComponentEditPart getModelRoot()
114   {
115     if (getRoot() instanceof MultiLayerDrawComponentEditPart) {
116         return (MultiLayerDrawComponentEditPart) getRoot();
117     }
118     else {
119         for (Iterator JavaDoc it = getRoot().getChildren().iterator(); it.hasNext(); ) {
120             Object JavaDoc o = it.next();
121             if (o instanceof MultiLayerDrawComponentEditPart) {
122                 return (MultiLayerDrawComponentEditPart) o;
123             }
124         }
125     }
126     return null;
127   }
128   
129   protected void addZoomListener(DrawComponentFigure figure)
130   {
131     ZoomManager zoomManager = EditorUtil.getZoomManager(this);
132     if (zoomManager != null) {
133       zoomManager.addZoomListener(figure.getZoomListener());
134     }
135   }
136   
137   protected RendererFigure getRendererFigure()
138   {
139     return (RendererFigure) getFigure();
140   }
141   /*
142    * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
143    */

144   protected abstract void createEditPolicies();
145   
146   /*
147    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
148    */

149   public void activate()
150   {
151     if (isActive())
152       return;
153     
154     // start listening for changes in the model
155
hookIntoDrawComponent(getDrawComponent());
156     
157     super.activate();
158   }
159   
160   /*
161    * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
162    */

163   public void deactivate()
164   {
165     if (!isActive())
166       return;
167     
168     // stop listening for changes in the model
169
unhookFromDrawComponent(getDrawComponent());
170     
171     super.deactivate();
172   }
173   
174   public DrawComponent getDrawComponent() {
175     return (DrawComponent) getModel();
176   }
177         
178   protected void refreshVisuals()
179   {
180     Rectangle r = new Rectangle(J2DUtil.toDraw2D(getDrawComponent().getBounds()));
181     
182     ((GraphicalEditPart) getParent()).setLayoutConstraint(
183         this,
184         getFigure(),
185         r);
186     
187     if (getFigure() instanceof RendererFigure) {
188       getRendererFigure().setRenderer(getDrawComponent().getRenderer());
189       getRendererFigure().setDrawComponent(getDrawComponent());
190     }
191     
192     getFigure().repaint();
193     updateRoot(getFigure());
194     updateTooltip();
195 // LOGGER.debug("refreshVisuals!");
196
}
197
198   public void updateRoot(IFigure figure)
199   {
200     MultiLayerDrawComponentEditPart rootEditPart = getModelRoot();
201     if (rootEditPart != null) {
202         if (rootEditPart.getBufferedFreeformLayer() != null)
203             rootEditPart.getBufferedFreeformLayer().refresh(figure);
204     }
205   }
206     
207 // public void updateLayer(IFigure figure)
208
// {
209
// LayerEditPart layerEditPart = getLayerEditPart();
210
// if (layerEditPart != null)
211
// layerEditPart.getBufferedFreeformLayer().refresh(figure);
212
//
213
//// LOGGER.debug("Update Layer!");
214
// }
215
//
216
// protected LayerEditPart getLayerEditPart()
217
// {
218
// EditPart parent = getParent();
219
// if (parent == null)
220
// throw new IllegalStateException("Member parent may not be null for DrawComponent"+this.toString());
221
//
222
// if (this instanceof LayerEditPart)
223
// return (LayerEditPart) this;
224
// if (this instanceof MultiLayerDrawComponentEditPart)
225
// return null;
226
// if (this instanceof RootEditPart)
227
// return null;
228
// if (parent instanceof LayerEditPart)
229
// return (LayerEditPart) parent;
230
//
231
// while (!(parent instanceof LayerEditPart)) {
232
// parent = parent.getParent();
233
// }
234
// return (LayerEditPart) parent;
235
// }
236

237   /* (non-Javadoc)
238    * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
239    */

240   public Object JavaDoc getAdapter(Class JavaDoc key)
241   {
242     /* override the default behavior defined in AbstractEditPart
243      * which would expect the model to be a property sourced.
244      * instead the editpart can provide a property source
245      */

246     if (IPropertySource.class == key)
247     {
248       return getPropertySource();
249     }
250     return super.getAdapter(key);
251   }
252   
253   /* (non-Javadoc)
254    * @see com.ibm.itso.sal330r.gefdemo.edit.WorkflowElementEditPart#getPropertySource()
255    */

256   protected IPropertySource getPropertySource()
257   {
258     if (propertySource == null)
259     {
260       propertySource =
261         new DrawComponentPropertySource(getDrawComponent());
262     }
263     return propertySource;
264   }
265   
266   protected PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc(){
267         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
268             propertyChanged(evt);
269         }
270     };
271   
272     protected void propertyChanged(PropertyChangeEvent JavaDoc evt)
273     {
274         String JavaDoc propertyName = evt.getPropertyName();
275         
276         if (propertyName.equals(DrawComponent.PROP_BOUNDS)) {
277 // LOGGER.debug(propertyName+"changed!");
278
refreshVisuals();
279         }
280         else if (propertyName.equals(DrawComponent.PROP_NAME)) {
281 // LOGGER.debug(propertyName+"changed!");
282
refreshVisuals();
283         }
284         else if (propertyName.equals(DrawComponent.PROP_HEIGHT)) {
285 // LOGGER.debug(propertyName+"changed!");
286
refreshVisuals();
287         }
288         else if (propertyName.equals(DrawComponent.PROP_WIDTH)) {
289 // LOGGER.debug(propertyName+"changed!");
290
refreshVisuals();
291         }
292         else if (propertyName.equals(DrawComponent.PROP_X)) {
293 // LOGGER.debug(propertyName+"changed!");
294
refreshVisuals();
295         }
296         else if (propertyName.equals(DrawComponent.PROP_Y)) {
297 // LOGGER.debug(propertyName+"changed!");
298
refreshVisuals();
299         }
300         else if (propertyName.equals(DrawComponent.PROP_ROTATION)) {
301 // LOGGER.debug(propertyName+"changed!");
302
refreshVisuals();
303         }
304         else if (propertyName.equals(DrawComponent.PROP_ROTATION_X)) {
305 // LOGGER.debug(propertyName+"changed!");
306
refreshVisuals();
307         }
308         else if (propertyName.equals(DrawComponent.PROP_ROTATION_Y)) {
309 // LOGGER.debug(propertyName+"changed!");
310
refreshVisuals();
311         }
312         else if (propertyName.equals(DrawComponent.PROP_RENDER_MODE)) {
313 // LOGGER.debug(propertyName+"changed!");
314
refreshVisuals();
315         }
316         else if (propertyName.equals(DrawComponent.TRANSFORM_CHANGED)) {
317 // LOGGER.debug(propertyName);
318
refreshVisuals();
319         }
320     }
321       
322   /**
323    * Registers this edit part as a listener for change notifications
324    * to the specified DrawComponent element.
325    *
326    * @param element the DrawComponent element that should be observed
327    * for change notifications
328    */

329   protected void hookIntoDrawComponent(DrawComponent element)
330   {
331     if (element != null)
332       element.addPropertyChangeListener(listener);
333   }
334   
335   /**
336    * Removes this edit part from the specified DrawComponent element.
337    * Thus, it will no longe receive change notifications.
338    *
339    * @param element the DrawComponent element that should not be observed
340    * any more
341    */

342   protected void unhookFromDrawComponent(DrawComponent element)
343   {
344     if (element != null)
345       element.removePropertyChangeListener(listener);
346   }
347             
348   public boolean understandsRequest(Request req)
349   {
350     if (req.getType().equals(REQ_ROTATE))
351       return true;
352
353     else if (req.getType().equals(REQ_EDIT_ROTATE_CENTER))
354       return true;
355
356     else if (req.getType().equals(REQ_SHEAR))
357       return true;
358     
359     return super.understandsRequest(req);
360   }
361 }
362
Popular Tags