1 8 package com.nightlabs.editor2d.handle; 9 10 import org.eclipse.draw2d.IFigure; 11 import org.eclipse.draw2d.Locator; 12 import org.eclipse.draw2d.geometry.Dimension; 13 import org.eclipse.draw2d.geometry.Point; 14 import org.eclipse.draw2d.geometry.Rectangle; 15 import org.eclipse.gef.GraphicalEditPart; 16 17 18 public abstract class AbstractLocator 19 implements Locator 20 { 21 protected GraphicalEditPart owner; 22 protected IFigure figure; 23 public AbstractLocator(GraphicalEditPart owner) 24 { 25 super(); 26 this.owner = owner; 27 this.figure = owner.getFigure(); 28 } 29 30 public void relocate(IFigure target) 31 { 32 target.setLocation(calcCenterPoint(target.getBounds(), getConstrainedPoint(target))); 34 } 35 36 protected abstract Point getLocation(); 37 38 protected Point calcCenterPoint(Rectangle rect, Point point) 39 { 40 Dimension boundsSize = rect.getSize(); 41 return new Point(point.x - boundsSize.width/2, point.y - boundsSize.height/2); 42 } 43 44 protected Point getConstrainedPoint(IFigure target) 45 { 46 Point p = getLocation(); 47 figure.translateToAbsolute(p); 48 target.translateToRelative(p); 49 return p; 50 } 51 } 52 | Popular Tags |