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
9 * Oracle Corporation - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.eclipse.jst.pagedesigner.editpolicies;
\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
23 public class AbsolutePointLocator implements Locator
\r
25 private static AbsolutePointLocator INSTANCE;
\r
26 private final static Point DEFAULT_POINT = new Point(0,0);
\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
34 * @return the singleton instance
\r
36 public synchronized static AbsolutePointLocator getInstance()
\r
38 if (INSTANCE == null)
\r
40 INSTANCE = new AbsolutePointLocator();
\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
49 public void relocate(IFigure target)
\r
51 Point leftTop = new Point(_referencePoint.x+_xOffset, _referencePoint.y+_yOffset);
\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
59 // to avoid enlargemeent
\r
60 if (_intersectFigure != null)
\r
62 rect = rect.intersect(_intersectFigure.getBounds());
\r
65 target.setBounds(rect);
\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
77 public void setReferencePoint(Point point, int xoffset, int yoffset)
\r
81 _referencePoint = DEFAULT_POINT;
\r
85 _referencePoint = point;
\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
98 * If intersectFigure is set to null, then no intersect calculation will
\r
101 * @param intersectFigure
\r
103 public void setIntersectFigure(IFigure intersectFigure)
\r
105 _intersectFigure = intersectFigure;
\r
108 private AbsolutePointLocator() {/*no external instantiation*/}
\r