1 27 28 package org.nightlabs.editor2d; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import org.apache.log4j.Logger; 36 import org.eclipse.draw2d.FigureCanvas; 37 import org.eclipse.draw2d.IFigure; 38 import org.eclipse.draw2d.MouseMotionListener; 39 import org.eclipse.draw2d.Viewport; 40 import org.eclipse.draw2d.geometry.Point; 41 import org.eclipse.gef.EditPart; 42 import org.eclipse.gef.EditPartListener; 43 import org.eclipse.gef.EditPartViewer; 44 import org.eclipse.gef.RootEditPart; 45 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart; 46 import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; 47 import org.eclipse.jface.action.IStatusLineManager; 48 import org.nightlabs.config.Config; 49 import org.nightlabs.config.ConfigException; 50 import org.nightlabs.editor2d.config.PreferencesConfigModule; 51 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart; 52 import org.nightlabs.editor2d.edit.LayerEditPart; 53 import org.nightlabs.editor2d.edit.MultiLayerDrawComponentEditPart; 54 import org.nightlabs.editor2d.figures.DrawComponentFigure; 55 import org.nightlabs.editor2d.render.RenderConstants; 56 import org.nightlabs.editor2d.render.Renderer; 57 import org.nightlabs.editor2d.util.EditorUtil; 58 import org.nightlabs.editor2d.viewer.descriptor.DescriptorManager; 59 60 public class ViewerManager 61 { 62 public static final Logger LOGGER = Logger.getLogger(ViewerManager.class); 63 64 protected ScrollingGraphicalViewer viewer = null; 65 protected Viewport viewport = null; 66 protected Point mousePoint = new Point(); 67 protected RootEditPart root = null; 68 protected IStatusLineManager statusLineMan = null; 69 public ViewerManager(ScrollingGraphicalViewer viewer, IStatusLineManager statusLineMan) 70 { 71 super(); 72 this.viewer = viewer; 73 this.statusLineMan = statusLineMan; 74 root = viewer.getRootEditPart(); 75 root.addEditPartListener(rootListener); 76 FigureCanvas canvas = (FigureCanvas) viewer.getControl(); 77 viewport = canvas.getViewport(); 78 initExcludeListRef(); 79 conditionRef = new ConditionRef(createDefaultCondition()); 80 viewport.addMouseMotionListener(mouseListener); 81 mousePoint = new Point(); 82 initConfigModule(); 83 } 84 85 protected ExcludeListRef excludeListRef; 86 protected void initExcludeListRef() 87 { 88 List excludeList = new ArrayList (); 89 LOGGER.debug("root = "+root); 90 excludeList.add(root); 91 excludeListRef = new ExcludeListRef(excludeList); 92 } 93 94 protected EditPartListener rootListener = new EditPartListener.Stub() 95 { 96 public void removingChild(EditPart child, int index) 97 { 98 excludeListRef.getExcludeList().remove(child); 99 for (Iterator it = child.getChildren().iterator(); it.hasNext(); ) { 100 excludeListRef.getExcludeList().remove(it.next()); 101 } 102 } 103 public void childAdded(EditPart child, int index) 104 { 105 excludeListRef.getExcludeList().add(child); 107 for (Iterator it = child.getChildren().iterator(); it.hasNext(); ) { 108 excludeListRef.getExcludeList().add(it.next()); 109 } 110 } 111 }; 112 113 protected List ignoredClasses = new ArrayList (); 114 public void addIgnoreTypeCollection(Collection editPartTypes) 115 { 116 if (editPartTypes == null) 117 throw new IllegalArgumentException ("Param editPartTypes must not be null!"); 118 119 if (editPartTypes.isEmpty()) 120 return; 121 122 for (Iterator it = editPartTypes.iterator(); it.hasNext(); ) { 123 Object o = it.next(); 124 if (o instanceof Class ) { 125 Class c = (Class ) o; 126 addIgnoreType(c); 127 } 128 } 129 conditionRef.setCondition(createIgnoreCondition(ignoredClasses)); 130 } 131 132 public void addIgnoreType(Class c) 133 { 134 if (AbstractDrawComponentEditPart.class.isAssignableFrom(c) && !ignoredClasses.contains(c)) { 135 ignoredClasses.add(c); 136 } 137 conditionRef.setCondition(createIgnoreCondition(ignoredClasses)); 138 } 139 140 public void addExcludeCollection(Collection editParts) 141 { 142 if (editParts == null) 143 throw new IllegalArgumentException ("Param editParts must not be null!"); 144 145 for (Iterator it = editParts.iterator(); it.hasNext(); ) { 146 Object o = it.next(); 147 if (o instanceof AbstractDrawComponentEditPart && !excludeListRef.getExcludeList().contains(o)) { 148 EditPart ep = (EditPart) o; 149 excludeListRef.getExcludeList().add(ep); 150 } 151 } 152 } 153 154 public void addExclude(AbstractDrawComponentEditPart ep) 155 { 156 if (ep == null) 157 throw new IllegalArgumentException ("Param ep must not be null!"); 158 159 excludeListRef.getExcludeList().add(ep); 160 } 161 162 public void setCondition(EditPartViewer.Conditional condition) { 163 conditionRef.setCondition(condition); 164 } 165 166 protected Class exclusiveClass; 167 public void setExclusiveClass(Class c) { 168 exclusiveClass = c; 169 } 170 171 protected DescriptorManager descriptorManager = new DescriptorManager(); 172 public DescriptorManager getDescriptorManager() { 173 return descriptorManager; 174 } 175 public void setDescriptorManager(DescriptorManager descMan) { 176 this.descriptorManager = descMan; 177 } 178 179 protected Point relativePoint = null; 180 protected AbstractDrawComponentEditPart oldPart = null; 181 182 214 protected MouseMotionListener mouseListener = new MouseMotionListener.Stub() 215 { 216 public void mouseMoved(org.eclipse.draw2d.MouseEvent me) 217 { 218 relativePoint = new Point(me.x, me.y); 219 mousePoint = EditorUtil.toAbsoluteWithScrollOffset(root, me.x, me.y); 220 EditPart part = viewer.findObjectAtExcluding(relativePoint, excludeListRef.getExcludeList(), conditionRef.getCondition()); 221 statusLineMan.setMessage(getMouseCoordinates()); 222 if (part != null) 223 { 224 if (exclusiveClass == null) { 225 if (part instanceof AbstractDrawComponentEditPart) { 226 doRollOver((AbstractDrawComponentEditPart)part); 227 } 228 } 229 else { 230 if (exclusiveClass.equals(part.getClass())) { 231 AbstractDrawComponentEditPart dcPart = (AbstractDrawComponentEditPart) exclusiveClass.cast(part); 232 doRollOver(dcPart); 233 } 234 } 235 } 236 } 237 }; 238 239 protected String getMouseCoordinates() 240 { 241 return "MouseX = "+mousePoint.x+", MouseY = "+mousePoint.y; 242 } 243 244 protected IFigure getFeedbackLayer() 245 { 246 if (root instanceof ScalableFreeformRootEditPart) { 247 ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) root; 248 IFigure feedbackLayer = rootEditPart.getLayer(ScalableFreeformRootEditPart.FEEDBACK_LAYER); 249 return feedbackLayer; 250 } 251 return null; 252 } 253 254 protected DrawComponentFigure rollOverFigure = null; 255 protected void addRollOver(AbstractDrawComponentEditPart dcPart) 256 { 257 if (getFeedbackLayer() != null && dcPart != null) 258 { 259 long addStart = System.currentTimeMillis(); 260 DrawComponent dc = dcPart.getDrawComponent(); 261 if (rollOverFigure == null) 263 rollOverFigure = new DrawComponentFigure(); 264 Renderer r = dc.getRenderModeManager().getRenderer(RenderConstants.ROLLOVER_MODE, dc.getClass()); 265 266 if (r != null) 267 { 268 rollOverFigure.setRenderer(r); 269 rollOverFigure.setDrawComponent(dc); 270 getFeedbackLayer().add(rollOverFigure); 273 } 274 long addEnd = System.currentTimeMillis() - addStart; 275 LOGGER.debug("addRollOver took "+addEnd+" ms!"); 276 } 277 } 278 279 protected void removeRollOver() 280 { 281 if (getFeedbackLayer() != null && rollOverFigure != null) { 282 getFeedbackLayer().remove(rollOverFigure); 284 } 285 } 286 287 protected PreferencesConfigModule prefConfMod = null; 288 protected void initConfigModule() 289 { 290 try { 291 prefConfMod = (PreferencesConfigModule) Config.sharedInstance().createConfigModule(PreferencesConfigModule.class); 292 } catch (ConfigException ce) { 293 throw new RuntimeException (ce); 294 } 295 } 296 297 protected void doRollOver(AbstractDrawComponentEditPart dcPart) 298 { 299 if (dcPart != null) { 300 DrawComponent dc = dcPart.getDrawComponent(); 301 descriptorManager.setDrawComponent(dc); 302 if (prefConfMod.isShowStatusLine()) 303 statusLineMan.setMessage(getMouseCoordinates() + ", " + descriptorManager.getEntriesAsString(false)); 304 305 } 308 309 } 320 321 public static class ConditionRef 322 { 323 private EditPartViewer.Conditional condition; 324 public ConditionRef(EditPartViewer.Conditional condition) 325 { 326 this.condition = condition; 327 } 328 public EditPartViewer.Conditional getCondition() { 329 return condition; 330 } 331 public void setCondition(EditPartViewer.Conditional condition) { 332 this.condition = condition; 333 } 334 } 335 336 public static class ExcludeListRef 337 { 338 private Collection excludeList; 339 public ExcludeListRef(Collection excludeList) { 340 this.excludeList = excludeList; 341 } 342 public Collection getExcludeList() { 343 return excludeList; 344 } 345 public void setExcludeList(Collection excludeList) { 346 this.excludeList = excludeList; 347 } 348 } 349 350 protected ConditionRef conditionRef = null; 351 protected EditPartViewer.Conditional createIgnoreCondition(final Collection classes) 352 { 353 EditPartViewer.Conditional condition = new EditPartViewer.Conditional() 354 { 355 public boolean evaluate(EditPart part) 356 { 357 if (!(part instanceof AbstractDrawComponentEditPart)) 358 return false; 359 360 Class c = part.getClass(); 361 if (classes.contains(c)) { 362 return false; 363 } 364 365 return true; 366 } 367 }; 368 return condition; 369 } 370 371 protected EditPartViewer.Conditional createExclusiveCondition(final Class c) 372 { 373 EditPartViewer.Conditional condition = new EditPartViewer.Conditional() 374 { 375 public boolean evaluate(EditPart part) 376 { 377 if (part.getClass().equals(c)) 378 return true; 379 380 return false; 381 } 382 }; 383 return condition; 384 } 385 386 protected EditPartViewer.Conditional createDefaultCondition() 387 { 388 EditPartViewer.Conditional condition = new EditPartViewer.Conditional() 389 { 390 public boolean evaluate(EditPart part) 391 { 392 if (part instanceof MultiLayerDrawComponentEditPart || 393 part instanceof LayerEditPart) 394 return false; 395 396 if (part instanceof RootEditPart) 397 return false; 398 399 if (part instanceof AbstractDrawComponentEditPart) 400 return true; 401 402 return false; 403 } 404 }; 405 return condition; 406 } 407 408 } 409 | Popular Tags |