Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / editpolicies / AbsolutePointLocator.java
1 /*******************************************************************************\r
2  * Copyright (c) 2001, 2007 Oracle Corporation and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  * \r
8  * Contributors:\r
9  *     Oracle Corporation - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.eclipse.jst.pagedesigner.editpolicies;\r
12 \r
13 import org.eclipse.draw2d.IFigure;\r
14 import org.eclipse.draw2d.Locator;\r
15 import org.eclipse.draw2d.geometry.Dimension;\r
16 import org.eclipse.draw2d.geometry.Point;\r
17 import org.eclipse.draw2d.geometry.Rectangle;\r
18 \r
19 /**\r
20  * @author cbateman\r
21  *\r
22  */\r
23 public class AbsolutePointLocator implements Locator \r
24 {\r
25     private static AbsolutePointLocator   INSTANCE;\r
26     private final static Point    DEFAULT_POINT = new Point(0,0);\r
27     \r
28     private Point  _referencePoint = DEFAULT_POINT; \r
29     private int    _xOffset = 0;\r
30     private int    _yOffset = 0;\r
31     private IFigure _intersectFigure;\r
32     \r
33     /**\r
34      * @return the singleton instance\r
35      */\r
36     public synchronized static AbsolutePointLocator getInstance()\r
37     {\r
38         if (INSTANCE == null)\r
39         {\r
40             INSTANCE = new AbsolutePointLocator();\r
41         }\r
42         return INSTANCE;\r
43     }\r
44     \r
45     /** \r
46      * Relocates the target figure to the reference point with possible x and y\r
47      * offsetting.  Uses the target's preferredSize as the new size.\r
48      */\r
49     public void relocate(IFigure target) \r
50     {\r
51         Point leftTop = new Point(_referencePoint.x+_xOffset, _referencePoint.y+_yOffset);\r
52         \r
53 \r
54         //figure.translateToAbsolute(leftTop);\r
55         target.translateToRelative(leftTop);\r
56         Dimension d = target.getPreferredSize();\r
57         Rectangle rect = new Rectangle(leftTop, d);\r
58 \r
59         // to avoid enlargemeent\r
60         if (_intersectFigure != null)\r
61         {\r
62             rect = rect.intersect(_intersectFigure.getBounds());\r
63         }\r
64         \r
65         target.setBounds(rect);\r
66     }\r
67 \r
68     /**\r
69      * Sets the reference point used to calculate the location to which\r
70      * relocate will relocate its target.  The x and y offset values are added\r
71      * to the reference point before final re-location.  If point is null\r
72      * then the reference is set to (0,0)\r
73      * @param point\r
74      * @param xoffset \r
75      * @param yoffset \r
76      */\r
77     public void setReferencePoint(Point point, int xoffset, int yoffset)\r
78     {\r
79         if (point == null)\r
80         {\r
81             _referencePoint = DEFAULT_POINT;\r
82         }\r
83         else\r
84         {\r
85             _referencePoint = point;\r
86         }\r
87        \r
88         _xOffset = xoffset;\r
89         _yOffset = yoffset;\r
90     }\r
91     \r
92     /**\r
93      * Sets the figure used to calculate a rectangular intersect of the \r
94      * relocated target.  This normally set to the parent of the target\r
95      * such as a layer to ensure that the relocate target does not enlarge\r
96      * its parent by relocating outside it's rectangle.\r
97      * \r
98      * If intersectFigure is set to null, then no intersect calculation will\r
99      * be performed.\r
100      * \r
101      * @param intersectFigure\r
102      */\r
103     public void setIntersectFigure(IFigure intersectFigure)\r
104     {\r
105         _intersectFigure = intersectFigure;\r
106     }\r
107     \r
108     private AbsolutePointLocator() {/*no external instantiation*/}\r
109 }\r