comparison org.eclipse.draw2d/src/org/eclipse/draw2d/ScrollPane.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.draw2d.ScrollPane;
14
15 import java.lang.all;
16
17 import org.eclipse.draw2d.geometry.Point;
18 import org.eclipse.draw2d.Figure;
19 import org.eclipse.draw2d.IFigure;
20 import org.eclipse.draw2d.ScrollBar;
21 import org.eclipse.draw2d.Viewport;
22 import org.eclipse.draw2d.ScrollPaneLayout;
23
24 /**
25 * A class which implements automatic horizontal and/or vertical scrolling for a single
26 * IFigure child.
27 * <p>
28 * ScrollBar visibilites are represented by integer class constants:
29 * <ul>
30 * <li>NEVER: Never show the ScrollBar
31 * <li>AUTOMATIC: Show as needed, when the ScrollPane can no longer contain its view
32 * <li>ALWAYS: Always show the ScrollBar
33 * </ul>
34 * To use, instantiate a ScrollPane object and call its setView(IFigure) method passing
35 * the IFigure that is desired to have scrolling ability.
36 */
37 public class ScrollPane
38 : Figure
39 {
40
41 /** Constant indicating to never show the ScrollBar */
42 public static const int NEVER = 0;
43 /** Constant indicating to show as needed, when the ScrollPane can't contain its view */
44 public static const int AUTOMATIC = 1;
45 /** Constant indicating to always show the ScrollBar */
46 public static const int ALWAYS = 2;
47
48 /** The viewport being scrolled */
49 protected Viewport viewport;
50 /** The horizontal scrollbar */
51 protected ScrollBar hBar;
52 /** The vertical scrollbar */
53 protected ScrollBar vBar;
54 private int
55 hVisibility = AUTOMATIC,
56 vVisibility = AUTOMATIC;
57
58 /**
59 * Constructs a new ScrollPane with a ScrollPaneLayout.
60 *
61 * @since 2.0
62 */
63 public this() {
64 setLayoutManager(new ScrollPaneLayout());
65 }
66
67 /**
68 * Creates a new horizontally oriented ScrollBar and adds it to this ScrollPane.
69 *
70 * @since 2.0
71 */
72 protected void createHorizontalScrollBar() {
73 ScrollBar bar = new ScrollBar();
74 bar.setHorizontal(true);
75 setHorizontalScrollBar(bar);
76 }
77
78 /**
79 * Creates a new Viewport and adds it to this ScrollPane.
80 *
81 * @since 2.0
82 */
83 protected void createViewport() {
84 setViewport(new Viewport());
85 }
86
87 /**
88 * Creates a new vertically oriented ScrollBar and adds it to this ScrollPane.
89 *
90 * @since 2.0
91 */
92 protected void createVerticalScrollBar() {
93 ScrollBar bar = new ScrollBar();
94 setVerticalScrollBar(bar);
95 }
96
97 /**
98 * Returns the ScrollPane's horizontal ScrollBar.
99 *
100 * @return the horizontal scrollbar
101 * @since 2.0
102 */
103 public ScrollBar getHorizontalScrollBar() {
104 if (hBar is null)
105 createHorizontalScrollBar();
106 return hBar;
107 }
108
109 /**
110 * Returns the visibility of the ScrollPane's horizontal ScrollBar. These are represented
111 * by the integer class constants {@link #NEVER}, {@link #AUTOMATIC}, and {@link #ALWAYS}.
112 * The default is {@link #AUTOMATIC}.
113 *
114 * @return the visiblity of the horizontal scrollbar
115 * @since 2.0
116 */
117 public int getHorizontalScrollBarVisibility() {
118 return hVisibility;
119 }
120
121 /**
122 * Returns the ScrollPane's vertical ScrollBar.
123 *
124 * @return teh vertical scrollbar
125 * @since 2.0
126 */
127 public ScrollBar getVerticalScrollBar() {
128 if (vBar is null)
129 createVerticalScrollBar();
130 return vBar;
131 }
132
133 /**
134 * Returns the visibility of the ScrollPane's vertical ScrollBar. These are represented
135 * by the integer class constants {@link #NEVER}, {@link #AUTOMATIC}, and {@link #ALWAYS}.
136 * The default is {@link #AUTOMATIC}.
137 *
138 * @return the visibility of the vertical scrollbar
139 * @since 2.0
140 */
141 public int getVerticalScrollBarVisibility() {
142 return vVisibility;
143 }
144
145 /**
146 * Returns the contents of the viewport.
147 * @return the contents of the viewport
148 */
149 public IFigure getContents() {
150 return getView();
151 }
152
153 /**
154 * Returns the ScrollPane's view. The view is the IFigure that is the contents of the
155 * ScrollPane.
156 * @return the contents
157 * @deprecated use getContents()
158 * @since 2.0
159 */
160 public IFigure getView() {
161 return getViewport().getContents();
162 }
163
164 /**
165 * Returns the ScrollPane's {@link Viewport}.
166 *
167 * @return the viewport
168 * @since 2.0
169 */
170 public Viewport getViewport() {
171 if (viewport is null)
172 createViewport();
173 return viewport;
174 }
175
176 /**
177 * Returns true because ScrollPanes are always opaque.
178 * @see IFigure#isOpaque()
179 */
180 public bool isOpaque() {
181 return true;
182 }
183
184 /**
185 * Scrolls the Scrollpane horizontally x pixels from its left-most position.
186 *
187 * @param x the amount to scroll horizontally
188 * @since 2.0
189 */
190 public void scrollHorizontalTo(int x) {
191 getViewport().setHorizontalLocation(x);
192 }
193
194 /**
195 * Scrolls the Scrollpane horizontally from its left-most position by location.x pixels
196 * and vertically from its top-most position by location.y pixels.
197 *
198 * @param location the point to scroll to
199 * @since 2.0
200 */
201 public void scrollTo(Point location) {
202 scrollHorizontalTo(location.x);
203 scrollVerticalTo(location.y);
204 }
205
206 /**
207 * Scrolls the Scrollpane vertically y pixels from its top-most position.
208 *
209 * @param y the amount to scroll vertically
210 * @since 2.0
211 */
212 public void scrollVerticalTo(int y) {
213 getViewport().setVerticalLocation(y);
214 }
215
216 /**
217 * Sets the contents of the current viewport.
218 * @param figure the contents of the viewport
219 */
220 public void setContents(IFigure figure) {
221 setView(figure);
222 }
223
224 /**
225 * Sets the ScrollPane's horizontal ScrollBar to the passed ScrollBar.
226 *
227 * @param bar the new horizontal scrollbar
228 * @since 2.0
229 */
230 public void setHorizontalScrollBar(ScrollBar bar) {
231 if (hBar !is null) {
232 remove(hBar);
233 hBar.getRangeModel().removePropertyChangeListener(hBar);
234 }
235 hBar = bar;
236 if (hBar !is null) {
237 add(hBar);
238 hBar.setRangeModel(getViewport().getHorizontalRangeModel());
239 }
240 }
241
242 /**
243 * Sets the horizontal ScrollBar visibility of the ScrollPane to the passed value. These
244 * are represented by the integer class constants {@link #NEVER}, {@link #AUTOMATIC}, and
245 * {@link #ALWAYS}. The default is {@link #AUTOMATIC}.
246 *
247 * @param v the new horizontal visibility
248 * @since 2.0
249 */
250 public void setHorizontalScrollBarVisibility(int v) {
251 if (hVisibility is v)
252 return;
253 hVisibility = v;
254 revalidate();
255 }
256
257 /**
258 * Sets both the horizontal and vertical ScrollBar visibilities of the ScrollPane to the
259 * passed value. These are represented by the integer class constants {@link #NEVER},
260 * {@link #AUTOMATIC}, and {@link #ALWAYS}. The default is {@link #AUTOMATIC}.
261 *
262 * @param v the new vertical and horizontal visibility
263 * @since 2.0
264 */
265 public void setScrollBarVisibility(int v) {
266 setHorizontalScrollBarVisibility(v);
267 setVerticalScrollBarVisibility(v);
268 }
269
270 /**
271 * Sets the ScrollPane's vertical ScrollBar to the passed Scrollbar.
272 *
273 * @param bar the new vertical scrollbar
274 * @since 2.0
275 */
276 public void setVerticalScrollBar(ScrollBar bar) {
277 if (vBar !is null) {
278 remove(vBar);
279 vBar.getRangeModel().removePropertyChangeListener(vBar);
280 }
281 vBar = bar;
282 if (vBar !is null) {
283 add(vBar);
284 vBar.setRangeModel(getViewport().getVerticalRangeModel());
285 }
286 }
287
288 /**
289 * Sets the vertical ScrollBar visibility of the ScrollPane to the passed value. These are
290 * represented by the integer class constants {@link #NEVER}, {@link #AUTOMATIC}, and
291 * {@link #ALWAYS}. The default is {@link #AUTOMATIC}.
292 *
293 * @param v the new vertical scrollbar visibility
294 * @since 2.0
295 */
296 public void setVerticalScrollBarVisibility(int v) {
297 if (vVisibility is v)
298 return;
299 vVisibility = v;
300 revalidate();
301 }
302
303 /**
304 * Sets the ScrollPane's view to the passed IFigure. The view is the top-level IFigure
305 * which represents the contents of the ScrollPane.
306 * @param figure the new contents
307 * @deprecated call setContents(IFigure) instead
308 * @since 2.0
309 */
310 public void setView(IFigure figure) {
311 getViewport().setContents(figure);
312 }
313
314 /**
315 * Sets the ScrollPane's Viewport to the passed value.
316 *
317 * @param vp the new viewport
318 * @since 2.0
319 */
320 public void setViewport(Viewport vp) {
321 if (viewport !is null)
322 remove(viewport);
323 viewport = vp;
324 if (vp !is null)
325 add(vp, 0);
326 }
327
328 /**
329 * @see IFigure#validate()
330 */
331 public void validate() {
332 super.validate();
333 getHorizontalScrollBar().validate();
334 getVerticalScrollBar().validate();
335 }
336
337 }