Project

General

Profile

Download (4.58 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 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
 *******************************************************************************/
11
package org.eclipse.gef.editparts;
12

    
13
import java.util.HashMap;
14
import java.util.Iterator;
15
import java.util.Map;
16

    
17
import org.eclipse.draw2d.FreeformLayer;
18
import org.eclipse.draw2d.IFigure;
19
import org.eclipse.draw2d.geometry.Dimension;
20
import org.eclipse.draw2d.geometry.Rectangle;
21

    
22
/**
23
 * This is a layer where the guide lines are displayed. The figures in this
24
 * layer should have a Boolean constraint indicating whether or not they are
25
 * horizontal guide lines.
26
 * 
27
 * @author Pratik Shah
28
 * @since 3.0
29
 */
30
public class GuideLayer extends FreeformLayer {
31

    
32
	private Map constraints;
33

    
34
	/**
35
	 * @param child
36
	 *            the figure whose constraint is to be found
37
	 * @return the constraint (Boolean indicating whether or not it is
38
	 *         horizontal) set for the given IFigure; <code>null</code>, if none
39
	 *         exists
40
	 */
41
	public Object getConstraint(IFigure child) {
42
		return getConstraints().get(child);
43
	}
44

    
45
	/**
46
	 * @return the Map of IFigures to their constraints (Booleans indicating
47
	 *         whether or not they are horizontal guide lines)
48
	 */
49
	public Map getConstraints() {
50
		if (constraints == null) {
51
			constraints = new HashMap();
52
		}
53
		return constraints;
54
	}
55

    
56
	/**
57
	 * @see org.eclipse.draw2d.FreeformFigure#getFreeformExtent()
58
	 */
59
	public Rectangle getFreeformExtent() {
60
		/*
61
		 * The freeform extents are just big enough to include all the children:
62
		 * tall enough to include the highest and the lowest horizontal guide
63
		 * lines, and wide enough to include the leftmost and the rightmost
64
		 * vertical guide lines. They always include the point 5,5 and are
65
		 * further expanded by 5-pixels in all directions so that none of the
66
		 * guide lines are on the edge.
67
		 */
68
		/*
69
		 * These ints are initialized to 5, and not 0, so that when the final
70
		 * extent is expanded by 5, the bounds do not go into the negative
71
		 * (unless necessary).
72
		 */
73
		int maxX = 5, minX = 5, maxY = 5, minY = 5;
74
		Iterator children = getChildren().iterator();
75
		while (children.hasNext()) {
76
			IFigure child = (IFigure) children.next();
77
			Boolean isHorizontal = (Boolean) getConstraint(child);
78
			if (isHorizontal != null) {
79
				if (isHorizontal.booleanValue()) {
80
					int position = child.getBounds().y;
81
					minY = Math.min(minY, position);
82
					maxY = Math.max(maxY, position);
83
				} else {
84
					int position = child.getBounds().x;
85
					minX = Math.min(minX, position);
86
					maxX = Math.max(maxX, position);
87
				}
88
			}
89
		}
90
		Rectangle r = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY
91
				+ 1);
92
		if (r.width > 1)
93
			r.expand(5, 0);
94
		if (r.height > 1)
95
			r.expand(0, 5);
96
		return r;
97
	}
98

    
99
	/**
100
	 * @see org.eclipse.draw2d.IFigure#getPreferredSize(int, int)
101
	 */
102
	public Dimension getPreferredSize(int wHint, int hHint) {
103
		Rectangle extents = getFreeformExtent();
104
		return new Dimension(extents.right(), extents.bottom());
105
	}
106

    
107
	/**
108
	 * @see org.eclipse.draw2d.IFigure#remove(org.eclipse.draw2d.IFigure)
109
	 */
110
	public void remove(IFigure child) {
111
		getConstraints().remove(child);
112
		super.remove(child);
113
	}
114

    
115
	/**
116
	 * @see org.eclipse.draw2d.IFigure#setBounds(org.eclipse.draw2d.geometry.Rectangle)
117
	 */
118
	public void setBounds(Rectangle rect) {
119
		super.setBounds(rect);
120
		Iterator children = getChildren().iterator();
121
		while (children.hasNext()) {
122
			IFigure child = (IFigure) children.next();
123
			Boolean isHorizontal = (Boolean) getConstraint(child);
124
			if (isHorizontal != null) {
125
				if (isHorizontal.booleanValue()) {
126
					Rectangle.SINGLETON.setLocation(getBounds().x,
127
							child.getBounds().y);
128
					Rectangle.SINGLETON.setSize(getBounds().width, 1);
129
				} else {
130
					Rectangle.SINGLETON.setLocation(child.getBounds().x,
131
							getBounds().y);
132
					Rectangle.SINGLETON.setSize(1, getBounds().height);
133
				}
134
				child.setBounds(Rectangle.SINGLETON);
135
			}
136
		}
137
	}
138

    
139
	/**
140
	 * The constraint is expected to be a Boolean indicating whether the given
141
	 * guide line figure is horizontal or not.
142
	 * 
143
	 * @see org.eclipse.draw2d.IFigure#setConstraint(org.eclipse.draw2d.IFigure,
144
	 *      java.lang.Object)
145
	 */
146
	public void setConstraint(IFigure child, Object constraint) {
147
		getConstraints().put(child, constraint);
148
	}
149

    
150
}
(8-8/21)