comparison org.eclipse.draw2d/src/org/eclipse/draw2d/Viewport.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 dbfb303e8fb0
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.Viewport;
14
15 import java.lang.all;
16 import org.eclipse.dwtxhelper.Bean;
17
18 import org.eclipse.draw2d.geometry.Point;
19 import org.eclipse.draw2d.geometry.Rectangle;
20 import org.eclipse.draw2d.geometry.Translatable;
21 import org.eclipse.draw2d.Figure;
22 import org.eclipse.draw2d.IFigure;
23 import org.eclipse.draw2d.Graphics;
24 import org.eclipse.draw2d.RangeModel;
25 import org.eclipse.draw2d.ViewportLayout;
26 import org.eclipse.draw2d.DefaultRangeModel;
27
28 /**
29 * A Viewport is a flexible window onto a {@link ScrollPane} and represents the visible
30 * portion of the ScrollPane.
31 */
32 public class Viewport
33 : Figure
34 , PropertyChangeListener
35 {
36
37 public alias Figure.getClientArea getClientArea;
38
39 /** ID for the view location property */
40 public static final String PROPERTY_VIEW_LOCATION = "viewLocation"; //$NON-NLS-1$
41 private IFigure view;
42
43 private bool useTranslate = false;
44 private bool trackWidth = false;
45 private bool trackHeight = false;
46 private bool ignoreScroll = false;
47 private RangeModel
48 horiztonalRangeModel = null,
49 verticalRangeModel = null;
50
51 private void instanceInit(){
52 setLayoutManager(new ViewportLayout());
53 setHorizontalRangeModel(new DefaultRangeModel());
54 setVerticalRangeModel(new DefaultRangeModel());
55 }
56
57 /**
58 * Constructs a new Viewport with the default values.
59 */
60 public this() {
61 instanceInit();
62 }
63
64 /**
65 * Constructs a new Viewport. If <i>setting</i> is <code>true</code>, the viewport will
66 * use graphics translation to paint.
67 * @param setting whether to use graphics translation
68 */
69 public this(bool setting) {
70 instanceInit();
71 useTranslate = setting;
72 }
73
74 /**
75 * @see IFigure#getClientArea(Rectangle)
76 */
77 public Rectangle getClientArea(Rectangle rect) {
78 super.getClientArea(rect);
79 if (useGraphicsTranslate())
80 rect.translate(getViewLocation());
81 return rect;
82 }
83
84 /**
85 * Returns the view, which is the contents of the {@link ScrollPane} associated with this
86 * Viewport.
87 *
88 * @return the contents
89 * @since 2.0
90 */
91 public IFigure getContents() {
92 return view;
93 }
94
95 /**
96 * Returns the RangeModel associated with the horizontal motion of this Viewport
97 * @return the RangeModel
98 *
99 * @since 2.0
100 */
101 public RangeModel getHorizontalRangeModel() {
102 return horiztonalRangeModel;
103 }
104
105 /**
106 * Returns <code>true</code> if the Viewport resizes itself in the vertical direction when
107 * the available height of its view is decreased, false otherwise. This option is turned
108 * off by default, and can be activated by calling
109 * {@link #setContentsTracksHeight(bool)} and passing in <code>true</code>.
110 *
111 * @return whether the contents tracks height
112 * @since 2.0
113 */
114 public bool getContentsTracksHeight() {
115 return trackHeight;
116 }
117
118 /**
119 * Returns <code>true</code> if the Viewport resizes itself in the horizontal direction
120 * when the available width of its view is decreased, false otherwise. This option is
121 * turned off by default, and can be activated by calling
122 * {@link #setContentsTracksWidth(bool)} and passing in <code>true</code>.
123 *
124 * @return whether the contents tracks width
125 * @since 2.0
126 */
127 public bool getContentsTracksWidth() {
128 return trackWidth;
129 }
130
131 /**
132 * Returns the range model associated with the vertical motion of the Viewport.
133 * @return the RangeModel
134 *
135 * @since 2.0
136 */
137 public RangeModel getVerticalRangeModel() {
138 return verticalRangeModel;
139 }
140
141 /**
142 * Returns the current location of this Viewport.
143 * @return the current location of this Viewport
144 *
145 * @since 2.0
146 */
147 public Point getViewLocation() {
148 return new Point(
149 getHorizontalRangeModel().getValue(),
150 getVerticalRangeModel().getValue());
151 }
152
153 private void localRevalidate() {
154 invalidate();
155 if (getLayoutManager() !is null)
156 getLayoutManager().invalidate();
157 getUpdateManager().addInvalidFigure(this);
158 }
159
160 /**
161 * @see Figure#paintClientArea(Graphics)
162 */
163 protected void paintClientArea(Graphics g) {
164 if (useGraphicsTranslate()) {
165 Point p = getViewLocation();
166 try {
167 g.translate(-p.x, -p.y);
168 g.pushState();
169 super.paintClientArea(g);
170 g.popState();
171 } finally {
172 g.translate(p.x, p.y);
173 }
174 } else
175 super.paintClientArea(g);
176 }
177
178 /**
179 * @see org.eclipse.draw2d.Figure#isCoordinateSystem()
180 */
181 public bool isCoordinateSystem() {
182 return useGraphicsTranslate() || super.isCoordinateSystem();
183 }
184
185 /**
186 * Listens for either of the {@link RangeModel RangeModels} to fire a property change
187 * event and updates the view accordingly.
188 * @param event the event
189 */
190 public void propertyChange(PropertyChangeEvent event) {
191 if (null !is cast(RangeModel)(event.getSource()) ) {
192 if (RangeModel.PROPERTY_VALUE.equals(event.getPropertyName())) {
193 if (!ignoreScroll) {
194 localRevalidate();
195 if (useGraphicsTranslate()) {
196 repaint();
197 fireMoved();
198 }
199 }
200 firePropertyChange(PROPERTY_VIEW_LOCATION, event.getOldValue(),
201 event.getNewValue());
202 }
203 }
204 }
205
206 /**
207 * Sets extents of {@link RangeModel RangeModels} to the client area of this Viewport.
208 * Sets RangeModel minimums to zero. Sets RangeModel maximums to this Viewport's
209 * height/width.
210 *
211 * @since 2.0
212 */
213 protected void readjustScrollBars() {
214 if (getContents() is null)
215 return;
216 getVerticalRangeModel().setAll(0, getClientArea().height,
217 getContents().getBounds().height);
218 getHorizontalRangeModel().setAll(0, getClientArea().width,
219 getContents().getBounds().width);
220 }
221
222 /**
223 * Sets this Viewport to be associated with the passed Figure.
224 *
225 * @param figure the new contents
226 * @since 2.0
227 */
228 public void setContents(IFigure figure) {
229 if (view is figure)
230 return;
231 if (view !is null)
232 remove(view);
233 view = figure;
234 if (view !is null)
235 add(figure);
236 }
237
238 /**
239 * Toggles the Viewport's ability to resize itself automatically when its view is
240 * decreased in size in the vertical direction. This is disabled by default.
241 *
242 * @param track whether this viewport should track its height
243 * @since 2.0
244 */
245 public void setContentsTracksHeight(bool track) {
246 trackHeight = track;
247 }
248
249 /**
250 * Toggles the Viewport's ability to resize itself automatically when its view is
251 * decreased in size in the horizontal direction. This is disabled by default.
252 *
253 * @param track whether this viewport should track its width
254 * @since 2.0
255 */
256 public void setContentsTracksWidth(bool track) {
257 trackWidth = track;
258 }
259
260 /**
261 * Sets the horizontal location of the Viewport's view to the passed value.
262 *
263 * @param value the new horizontal location
264 * @since 2.0
265 */
266 public void setHorizontalLocation(int value) {
267 setViewLocation(value, getVerticalRangeModel().getValue());
268 }
269
270 /**
271 * Sets the horizontal range model to the passed RangeModel.
272 *
273 * @param rangeModel the new horizontal range model
274 * @since 2.0
275 */
276 public void setHorizontalRangeModel(RangeModel rangeModel) {
277 if (horiztonalRangeModel !is null)
278 horiztonalRangeModel.removePropertyChangeListener(this);
279 horiztonalRangeModel = rangeModel;
280 horiztonalRangeModel.addPropertyChangeListener(this);
281 }
282
283 /**
284 * If <i>value</i> is <code>true</code>, this viewport will ignore any scrolling that
285 * occurs until this method is called again with <code>false</code>.
286 *
287 * @param value whether this viewport should ignore future scrolls
288 */
289 public void setIgnoreScroll(bool value) {
290 ignoreScroll = value;
291 }
292
293 /**
294 * Sets the vertical location of the Viewport's view to the passed value.
295 *
296 * @param value the new vertical location
297 * @since 2.0
298 */
299 public void setVerticalLocation(int value) {
300 setViewLocation(getHorizontalRangeModel().getValue(), value);
301 }
302
303 /**
304 * Sets the vertical range model to the passed RangeModel.
305 *
306 * @param rangeModel the new vertical RangeModel
307 * @since 2.0
308 */
309 public void setVerticalRangeModel(RangeModel rangeModel) {
310 if (verticalRangeModel !is null)
311 verticalRangeModel.removePropertyChangeListener(this);
312 verticalRangeModel = rangeModel;
313 verticalRangeModel.addPropertyChangeListener(this);
314 }
315
316 /**
317 * Sets the location of the Viewport's view to the passed values.
318 *
319 * @param x The new x coordinate of the Viewport's view.
320 * @param y The new y coordinate of the Viewport's view.
321 * @since 2.0
322 */
323 public void setViewLocation(int x, int y) {
324 if (getHorizontalRangeModel().getValue() !is x)
325 getHorizontalRangeModel().setValue(x);
326 if (getVerticalRangeModel().getValue() !is y)
327 getVerticalRangeModel().setValue(y);
328 }
329
330 /**
331 * Sets the location of the Viewport's view to the passed Point.
332 *
333 * @param p The new location of the Viewport's view.
334 * @since 2.0
335 */
336 public void setViewLocation(Point p) {
337 setViewLocation(p.x, p.y);
338 }
339
340 /**
341 * @see IFigure#translateFromParent(Translatable)
342 */
343 public void translateFromParent(Translatable t) {
344 if (useTranslate)
345 t.performTranslate(
346 getHorizontalRangeModel().getValue(),
347 getVerticalRangeModel().getValue()
348 );
349 super.translateFromParent(t);
350 }
351
352 /**
353 * @see IFigure#translateToParent(Translatable)
354 */
355 public void translateToParent(Translatable t) {
356 if (useTranslate)
357 t.performTranslate(
358 -getHorizontalRangeModel().getValue(),
359 -getVerticalRangeModel().getValue()
360 );
361 super.translateToParent(t);
362 }
363
364 /**
365 * Returns <code>true</code> if this viewport uses graphics translation.
366 * @return whether this viewport uses graphics translation
367 */
368 public bool useGraphicsTranslate() {
369 return useTranslate;
370 }
371
372 /**
373 * @see IFigure#validate()
374 */
375 public void validate() {
376 super.validate();
377 readjustScrollBars();
378 }
379
380 }