Project

General

Profile

Download (5.88 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 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.draw2d;
12

    
13
import org.eclipse.draw2d.geometry.Dimension;
14
import org.eclipse.draw2d.geometry.Insets;
15
import org.eclipse.draw2d.geometry.Rectangle;
16
import org.eclipse.draw2d.geometry.Transposer;
17

    
18
/**
19
 * Lays out the Figures that make up a ScrollBar.
20
 */
21
public class ScrollBarLayout extends AbstractLayout {
22

    
23
	/** Used as a constraint for the up arrow figure. */
24
	public static final String UP_ARROW = "up arrow"; //$NON-NLS-1$
25
	/** Used as a constraint for the down arrow figure. */
26
	public static final String DOWN_ARROW = "down arrow"; //$NON-NLS-1$
27
	/** Used as a constraint for the thumb figure. */
28
	public static final String THUMB = "thumb"; //$NON-NLS-1$
29
	/** Used as a constraint for the page up figure. */
30
	public static final String PAGE_UP = "page_up"; //$NON-NLS-1$
31
	/** Used as a constraint for the page down figure. */
32
	public static final String PAGE_DOWN = "page_down"; //$NON-NLS-1$
33

    
34
	IFigure up, down, thumb, pageUp, pageDown;
35

    
36
	/**
37
	 * Transposes values if the ScrollBar is horizontally oriented. When used
38
	 * properly, the layout manager just needs to code for one case: vertical
39
	 * orientation.
40
	 */
41
	protected final Transposer transposer;
42

    
43
	/**
44
	 * Constructs a ScrollBarLayout. If the given Transposer is enabled, the
45
	 * Scrollbar will be horizontally oriented. Otherwise, the ScrollBar will be
46
	 * vertically oriented.
47
	 * 
48
	 * @param t
49
	 *            the Transposer
50
	 * @since 2.0
51
	 */
52
	public ScrollBarLayout(Transposer t) {
53
		transposer = t;
54
	}
55

    
56
	/**
57
	 * @see AbstractLayout#setConstraint(IFigure, Object)
58
	 */
59
	public void setConstraint(IFigure figure, Object constraint) {
60
		if (constraint.equals(UP_ARROW))
61
			up = figure;
62
		else if (constraint.equals(DOWN_ARROW))
63
			down = figure;
64
		else if (constraint.equals(THUMB))
65
			thumb = figure;
66
		else if (constraint.equals(PAGE_UP))
67
			pageUp = figure;
68
		else if (constraint.equals(PAGE_DOWN))
69
			pageDown = figure;
70
	}
71

    
72
	/**
73
	 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
74
	 */
75
	protected Dimension calculatePreferredSize(IFigure parent, int w, int h) {
76
		Insets insets = transposer.t(parent.getInsets());
77
		Dimension d = new Dimension(16, 16 * 4);
78
		d.expand(insets.getWidth(), insets.getHeight());
79
		return transposer.t(d);
80
	}
81

    
82
	/**
83
	 * @see LayoutManager#layout(IFigure)
84
	 */
85
	public void layout(IFigure parent) {
86
		ScrollBar scrollBar = (ScrollBar) parent;
87

    
88
		Rectangle trackBounds = layoutButtons(scrollBar);
89

    
90
		int extent = scrollBar.getExtent();
91
		int max = scrollBar.getMaximum();
92
		int min = scrollBar.getMinimum();
93
		int totalRange = max - min;
94
		int valueRange = totalRange - extent;
95
		if ((valueRange < 1) || (!scrollBar.isEnabled())) {
96
			Rectangle boundsUpper = new Rectangle(trackBounds), boundsLower = new Rectangle(
97
					trackBounds);
98
			boundsUpper.height /= 2;
99
			boundsLower.y += boundsUpper.height;
100
			boundsLower.height = trackBounds.height - boundsUpper.height;
101
			if (pageUp != null)
102
				pageUp.setBounds(transposer.t(boundsUpper));
103
			if (pageDown != null)
104
				pageDown.setBounds(transposer.t(boundsLower));
105
			return;
106
		}
107

    
108
		if (totalRange == 0)
109
			return;
110
		int thumbHeight = Math.max(thumb == null ? 0
111
				: thumb.getMinimumSize().height, trackBounds.height * extent
112
				/ totalRange);
113

    
114
		if (thumb != null)
115
			thumb.setVisible(trackBounds.height > thumbHeight);
116

    
117
		int thumbY = trackBounds.y + (trackBounds.height - thumbHeight)
118
				* (scrollBar.getValue() - min) / valueRange;
119

    
120
		Rectangle thumbBounds = new Rectangle(trackBounds.x, thumbY,
121
				trackBounds.width, thumbHeight);
122

    
123
		if (thumb != null)
124
			thumb.setBounds(transposer.t(thumbBounds));
125

    
126
		if (pageUp != null)
127
			pageUp.setBounds(transposer.t(new Rectangle(trackBounds.x,
128
					trackBounds.y, trackBounds.width, thumbBounds.y
129
							- trackBounds.y)));
130

    
131
		if (pageDown != null)
132
			pageDown.setBounds(transposer.t(new Rectangle(trackBounds.x,
133
					thumbBounds.y + thumbHeight, trackBounds.width, trackBounds
134
							.bottom() - thumbBounds.bottom())));
135
	}
136

    
137
	/**
138
	 * Places the buttons and returns the Rectangle into which the track should
139
	 * be placed. The track consists of the pageup, pagedown, and thumb figures.
140
	 * The Rectangle returned should be transposed correctly, that is, it should
141
	 * be vertically oriented. Users of the rectangle will re-transpose it for
142
	 * horizontal use.
143
	 * 
144
	 * @param scrollBar
145
	 *            the scrollbar whose buttons are being layed out
146
	 * @return the Rectangle into which the track should be placed
147
	 * @since 2.0
148
	 */
149
	protected Rectangle layoutButtons(ScrollBar scrollBar) {
150
		Rectangle bounds = transposer.t(scrollBar.getClientArea());
151
		Dimension buttonSize = new Dimension(bounds.width, Math.min(
152
				bounds.width, bounds.height / 2));
153

    
154
		if (up != null)
155
			up.setBounds(transposer.t(new Rectangle(bounds.getTopLeft(),
156
					buttonSize)));
157
		if (down != null) {
158
			Rectangle r = new Rectangle(bounds.x, bounds.bottom()
159
					- buttonSize.height, buttonSize.width, buttonSize.height);
160
			down.setBounds(transposer.t(r));
161
		}
162

    
163
		Rectangle trackBounds = bounds.getCropped(new Insets((up == null) ? 0
164
				: buttonSize.height, 0, (down == null) ? 0 : buttonSize.height,
165
				0));
166

    
167
		return trackBounds;
168
	}
169

    
170
	/**
171
	 * @see LayoutManager#remove(IFigure)
172
	 */
173
	public void remove(IFigure child) {
174
		if (child == up) {
175
			up = null;
176
		} else if (child == down) {
177
			down = null;
178
		} else if (child == thumb) {
179
			thumb = null;
180
		} else if (child == pageUp) {
181
			pageUp = null;
182
		} else if (child == pageDown) {
183
			pageDown = null;
184
		}
185
	}
186
}
(143-143/171)