comparison dwtx/draw2d/RelativeLocator.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM 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 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.RelativeLocator;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Dimension;
18 import dwtx.draw2d.geometry.PrecisionRectangle;
19 import dwtx.draw2d.geometry.Rectangle;
20 import dwtx.draw2d.Locator;
21 import dwtx.draw2d.IFigure;
22 import dwtx.draw2d.PositionConstants;
23
24 /**
25 * Places a handle relative to a figure's bounds. The placement is determined by
26 * indicating the figure to which the placement is relative, and two floating-point value
27 * indicating the horizontal and vertical offset from that figure's top-left corner. The
28 * values (0.0, 0.0) would indicate the figure's top-left corner, while the values (1.0,
29 * 1.0) would indicate the figure's bottom-right corner.
30 * <P>
31 * Constants such as {@link PositionConstants#NORTH NORTH} and {@link
32 * PositionConstants#SOUTH SOUTH} can be used to set the placement.
33 */
34 public class RelativeLocator
35 : Locator
36 {
37
38 private double relativeX;
39 private double relativeY;
40 private IFigure reference;
41
42 /**
43 * Null constructor. The reference figure must be set before use. The relative locations
44 * will default to (0.0, 0.0).
45 * @since 2.0
46 */
47 public this() {
48 relativeX = 0.0;
49 relativeY = 0.0;
50 }
51
52 /**
53 * Constructs a RelativeLocator with the given reference figure and relative location. The
54 * location is a constant from {@link PositionConstants} used as a convenient and readable
55 * way to set both the relativeX and relativeY values.
56 * @param reference the reference figure
57 * @param location one of NORTH, NORTH_EAST, etc.
58 * @since 2.0
59 */
60 public this(IFigure reference, int location) {
61 setReferenceFigure(reference);
62 switch (location & PositionConstants.NORTH_SOUTH) {
63 case PositionConstants.NORTH:
64 relativeY = 0; break;
65 case PositionConstants.SOUTH:
66 relativeY = 1.0; break;
67 default:
68 relativeY = 0.5;
69 }
70
71 switch (location & PositionConstants.EAST_WEST) {
72 case PositionConstants.WEST:
73 relativeX = 0; break;
74 case PositionConstants.EAST:
75 relativeX = 1.0; break;
76 default:
77 relativeX = 0.5;
78 }
79 }
80
81 /**
82 * Constructs a RelativeLocator with the given reference Figure and offset ratios.
83 * @param reference the reference figure
84 * @param relativeX the relative X offset
85 * @param relativeY the relative Y offset
86 * @since 2.0
87 */
88 public this(IFigure reference, double relativeX, double relativeY) {
89 setReferenceFigure(reference);
90 this.relativeX = relativeX;
91 this.relativeY = relativeY;
92 }
93
94 /**
95 * Returns the Reference Box in the Reference Figure's coordinate system.
96 * The returned Rectangle may be by reference, and should <b>not</b> be modified.
97 * @return the reference box
98 * @since 2.0
99 */
100 protected Rectangle getReferenceBox() {
101 return getReferenceFigure().getBounds();
102 }
103
104 /**
105 * Returns the Figure this locator is relative to.
106 * @return the reference figure
107 * @since 2.0
108 */
109 protected IFigure getReferenceFigure() {
110 return reference;
111 }
112
113 /**
114 * Relocates the target using the relative offset locations.
115 * @see dwtx.draw2d.Locator#relocate(dwtx.draw2d.IFigure)
116 */
117 public void relocate(IFigure target) {
118 IFigure reference = getReferenceFigure();
119 Rectangle targetBounds = new PrecisionRectangle(getReferenceBox().getResized(-1, -1));
120 reference.translateToAbsolute(targetBounds);
121 target.translateToRelative(targetBounds);
122 targetBounds.resize(1, 1);
123
124 Dimension targetSize = target.getPreferredSize();
125
126 targetBounds.x
127 += cast(int) (targetBounds.width * relativeX - ((targetSize.width + 1) / 2));
128 targetBounds.y
129 += cast(int) (targetBounds.height * relativeY - ((targetSize.height + 1) / 2));
130 targetBounds.setSize(targetSize);
131 target.setBounds(targetBounds);
132 }
133
134 /**
135 * Sets the reference figure this locator uses to place the target figure.
136 * @param reference the reference figure
137 * @since 2.0
138 */
139 public void setReferenceFigure(IFigure reference) {
140 this.reference = reference;
141 }
142
143 }