KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > handle > ShapeHandleLocator


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 14.01.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

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
16 import com.nightlabs.editor2d.ShapeDrawComponent;
17 import com.nightlabs.editor2d.edit.ShapeDrawComponentEditPart;
18 import com.nightlabs.editor2d.figures.ShapeFigure;
19 import com.nightlabs.editor2d.j2d.GeneralShape;
20 import com.nightlabs.editor2d.j2d.PathSegment;
21 import com.nightlabs.editor2d.util.J2DUtil;
22
23
24 public class ShapeHandleLocator
25 implements Locator
26 {
27   protected IFigure figure;
28   protected IFigure getFigure() {
29     return figure;
30   }
31
32   protected ShapeDrawComponentEditPart sdcep;
33   public GeneralShape getGeneralShape() {
34     return sdcep.getGeneralShape();
35   }
36       
37   protected int index;
38   public int getIndex() {
39     return index;
40   }
41   public void setIndex(int index) {
42     this.index = index;
43   }
44   
45   public ShapeHandleLocator(ShapeDrawComponentEditPart sdcep, int index)
46   {
47     super();
48     this.figure = sdcep.getFigure();
49     this.sdcep = sdcep;
50     this.index = index;
51   }
52   
53   protected Point getReferencePoint(IFigure target)
54   {
55     Point p = getLocation();
56     getFigure().translateToAbsolute(p);
57     target.translateToRelative(p);
58         
59     return p;
60   }
61   
62   protected Point getLocation()
63   {
64     PathSegment ps = getGeneralShape().getPathSegment(index);
65     if (ps != null) {
66       return J2DUtil.toDraw2D(ps.getPoint());
67     }
68     return new Point();
69   }
70   
71   protected IFigure getReference() {
72     return figure;
73   }
74   
75   public void relocate(IFigure target)
76   {
77     target.setLocation(calcCenterPoint(target.getBounds(), getReferencePoint(target)));
78   }
79   
80   protected Point calcCenterPoint(Rectangle rect, Point point)
81   {
82     Dimension boundsSize = rect.getSize();
83     return new Point(point.x - boundsSize.width/2, point.y - boundsSize.height/2);
84   }
85 }
86
Free Books   Free Magazines  
Popular Tags