1 27 28 package org.nightlabs.editor2d.edit.tree; 29 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 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 55 public DrawComponentTreeEditPart(DrawComponent drawComponent) { 56 super(drawComponent); 57 } 58 59 62 public Object getAdapter(Class key) 63 { 64 68 if (IPropertySource.class == key) 69 { 70 return getPropertySource(); 71 } 72 return super.getAdapter(key); 73 } 74 75 78 protected abstract Image getImage(); 79 80 83 protected IPropertySource getPropertySource() 84 { 85 if (propertySource == null) 86 { 87 propertySource = 88 new DrawComponentPropertySource(getDrawComponent()); 89 } 90 return propertySource; 91 } 92 93 96 protected String getText() 97 { 98 return getDrawComponent().getName(); 99 } 100 101 105 public DrawComponent getDrawComponent() { 106 return (DrawComponent) getModel(); 107 } 108 109 110 113 public void activate() 114 { 115 if (isActive()) 116 return; 117 118 hookIntoDrawComponent(getDrawComponent()); 120 121 super.activate(); 122 } 123 124 125 128 public void deactivate() 129 { 130 if (!isActive()) 131 return; 132 133 unhookFromDrawComponent(getDrawComponent()); 135 136 super.deactivate(); 137 } 138 139 146 protected void hookIntoDrawComponent(DrawComponent element) 147 { 148 if (element != null) 149 element.addPropertyChangeListener(listener); 150 } 151 152 159 protected void unhookFromDrawComponent(DrawComponent element) 160 { 161 if (element != null) 162 element.removePropertyChangeListener(listener); 163 } 164 165 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 listener = new PropertyChangeListener (){ 176 public void propertyChange(PropertyChangeEvent evt) { 177 propertyChanged(evt); 178 } 179 }; 180 181 protected void propertyChanged(PropertyChangeEvent evt) 182 { 183 String propertyName = evt.getPropertyName(); 184 if (propertyName.equals(DrawComponent.PROP_BOUNDS)) { 185 refreshVisuals(); 187 } 188 else if (propertyName.equals(DrawComponent.PROP_HEIGHT)) { 189 refreshVisuals(); 191 } 192 else if (propertyName.equals(DrawComponent.PROP_WIDTH)) { 193 refreshVisuals(); 195 } 196 else if (propertyName.equals(DrawComponent.PROP_X)) { 197 refreshVisuals(); 199 } 200 else if (propertyName.equals(DrawComponent.PROP_Y)) { 201 refreshVisuals(); 203 } 204 else if (propertyName.equals(DrawComponent.PROP_ROTATION)) { 205 refreshVisuals(); 207 } 208 else if (propertyName.equals(DrawComponent.PROP_ROTATION_X)) { 209 refreshVisuals(); 211 } 212 else if (propertyName.equals(DrawComponent.PROP_ROTATION_Y)) { 213 refreshVisuals(); 215 } 216 else if (propertyName.equals(DrawComponent.PROP_RENDER_MODE)) { 217 refreshVisuals(); 219 } 220 else if (propertyName.equals(DrawComponent.PROP_NAME)) { 221 refreshVisuals(); 223 } 224 else if (propertyName.equals(DrawComponent.PROP_LANGUAGE_ID)) { 225 refreshVisuals(); 227 } 228 229 } 230 231 } 232 | Popular Tags |