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