1 8 package com.nightlabs.editor2d.edit.tree; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 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 35 public DrawComponentTreeEditPart(DrawComponent drawComponent) { 36 super(drawComponent); 37 } 38 39 42 public Object getAdapter(Class key) 43 { 44 48 if (IPropertySource.class == key) 49 { 50 return getPropertySource(); 51 } 52 return super.getAdapter(key); 53 } 54 55 58 protected abstract Image getImage(); 59 60 63 protected IPropertySource getPropertySource() 64 { 65 if (propertySource == null) 66 { 67 propertySource = 68 new DrawComponentPropertySource(getDrawComponent()); 69 } 70 return propertySource; 71 } 72 73 76 protected String getText() 77 { 78 return getDrawComponent().getName(); 79 } 86 87 91 public DrawComponent getDrawComponent() { 92 return (DrawComponent) getModel(); 93 } 94 95 96 99 public void activate() 100 { 101 if (isActive()) 102 return; 103 104 hookIntoDrawComponent(getDrawComponent()); 106 107 super.activate(); 108 } 109 110 111 114 public void deactivate() 115 { 116 if (!isActive()) 117 return; 118 119 unhookFromDrawComponent(getDrawComponent()); 121 122 super.deactivate(); 123 } 124 125 132 protected void hookIntoDrawComponent(DrawComponent element) 133 { 134 if (element != null) 135 element.addPropertyChangeListener(listener); 136 } 137 138 145 protected void unhookFromDrawComponent(DrawComponent element) 146 { 147 if (element != null) 148 element.removePropertyChangeListener(listener); 149 } 150 151 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 listener = new PropertyChangeListener (){ 162 public void propertyChange(PropertyChangeEvent evt) { 163 propertyChanged(evt); 164 } 165 }; 166 167 protected void propertyChanged(PropertyChangeEvent evt) 168 { 169 String 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 } 242 | Popular Tags |