Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 8 package com.nightlabs.editor2d.edit; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 import java.util.Iterator ; 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 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 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 it = getRoot().getChildren().iterator(); it.hasNext(); ) { 79 Object 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 103 protected abstract void createEditPolicies(); 104 105 108 public void activate() 109 { 110 if (isActive()) 111 return; 112 113 hookIntoDrawComponent(getDrawComponent()); 115 116 super.activate(); 117 } 118 119 122 public void deactivate() 123 { 124 if (!isActive()) 125 return; 126 127 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 } 161 162 protected LayerEditPart getLayerEditPart() 163 { 164 EditPart parent = getParent(); 165 if (parent == null) 166 throw new IllegalStateException ("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 186 public Object getAdapter(Class key) 187 { 188 192 if (IPropertySource.class == key) 193 { 194 return getPropertySource(); 195 } 196 return super.getAdapter(key); 197 } 198 199 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 listener = new PropertyChangeListener (){ 213 public void propertyChange(PropertyChangeEvent evt) { 214 propertyChanged(evt); 215 } 216 }; 217 218 protected void propertyChanged(PropertyChangeEvent evt) 219 { 220 String 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 267 protected void hookIntoDrawComponent(DrawComponent element) 268 { 269 if (element != null) 270 element.addPropertyChangeListener(listener); 271 } 272 273 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
|