2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.Component
;
42 import java
.awt
.Dimension
;
43 import java
.awt
.Graphics
;
44 import java
.awt
.Insets
;
45 import java
.awt
.Point
;
46 import java
.awt
.Rectangle
;
48 import javax
.swing
.border
.Border
;
49 import javax
.swing
.event
.ChangeEvent
;
50 import javax
.swing
.event
.ChangeListener
;
51 import javax
.swing
.plaf
.ViewportUI
;
57 * +-------------------------------+ ...........Y1 \
59 * | (this component's child) | . > VY
61 * | +------------------------------+ ....Y2_/
63 * | | (this component) | | .
68 * | +------------------------------+ ....Y3
72 * +---------.---------------------+ ...........Y4
76 * X1.......X2.....................X3.......X4
82 * <p>A viewport is, like all swing components, located at some position in
83 * the swing component tree; that location is exactly the same as any other
84 * components: the viewport's "bounds".</p>
86 * <p>But in terms of drawing its child, the viewport thinks of itself as
87 * covering a particular position <em>of the view's coordinate space</em>.
88 * For example, the {@link javax.JViewPort.getViewPosition} method returns
89 * the position <code>(VX,VY)</code> shown above, which is an position in
90 * "view space", even though this is <em>implemented</em> by positioning
91 * the underlying child at position <code>(-VX,-VY)</code></p>
94 public class JViewport
extends JComponent
96 private static final long serialVersionUID
= -6925142919680527970L;
98 public static final int SIMPLE_SCROLL_MODE
= 0;
99 public static final int BLIT_SCROLL_MODE
= 1;
100 public static final int BACKINGSTORE_SCROLL_MODE
= 2;
102 ChangeEvent changeEvent
= new ChangeEvent(this);
106 protected boolean scrollUnderway
;
107 protected boolean isViewSizeSet
;
110 * The width and height of the Viewport's area in terms of view
111 * coordinates. Typically this will be the same as the width and height
112 * of the viewport's bounds, unless the viewport transforms units of
113 * width and height, which it may do, for example if it magnifies or
116 * @see #toViewCoordinates
118 Dimension extentSize
;
121 * The width and height of the view in its own coordinate space.
126 Point lastPaintPosition
;
131 setScrollMode(BLIT_SCROLL_MODE
);
135 public Dimension
getExtentSize()
137 if (extentSize
== null)
138 return toViewCoordinates(getSize());
143 public Dimension
toViewCoordinates(Dimension size
)
148 public Point
toViewCoordinates(Point p
)
150 Point pos
= getViewPosition();
151 return new Point(p
.x
+ pos
.x
,
155 public void setExtentSize(Dimension newSize
)
157 extentSize
= newSize
;
162 * Returns the viewSize when set, or the preferred size of the set
163 * Component view. If no viewSize and no Component view is set an
164 * empty Dimension is returned.
166 public Dimension
getViewSize()
172 Component view
= getView();
174 return view
.getPreferredSize();
176 return new Dimension();
181 public void setViewSize(Dimension newSize
)
184 Component view
= getView();
186 view
.setSize(viewSize
);
187 isViewSizeSet
= true;
192 * Get the viewport's position in view space. Despite confusing name,
193 * this really does return the viewport's (0,0) position in view space,
194 * not the view's position.
197 public Point
getViewPosition()
199 Component view
= getView();
201 return new Point(0,0);
204 Point p
= view
.getLocation();
211 public void setViewPosition(Point p
)
213 Component view
= getView();
216 Point q
= new Point(-p
.x
, -p
.y
);
222 public Rectangle
getViewRect()
224 return new Rectangle(getViewPosition(),
231 public boolean isBackingStoreEnabled()
233 return scrollMode
== BACKINGSTORE_SCROLL_MODE
;
239 public void setBackingStoreEnabled(boolean b
)
241 if (b
&& scrollMode
!= BACKINGSTORE_SCROLL_MODE
)
243 scrollMode
= BACKINGSTORE_SCROLL_MODE
;
248 public void setScrollMode(int mode
)
254 public int getScrollMode()
259 public Component
getView()
261 if (getComponentCount() == 0)
264 return getComponents()[0];
267 public void setView(Component v
)
269 while (getComponentCount() > 0)
278 public void revalidate()
284 public void reshape(int x
, int y
, int w
, int h
)
290 || (h
!= getHeight());
291 super.reshape(x
, y
, w
, h
);
296 protected void addImpl(Component comp
, Object constraints
, int index
)
298 if (getComponentCount() > 0)
299 remove(getComponents()[0]);
301 super.addImpl(comp
, constraints
, index
);
304 public final Insets
getInsets()
306 return new Insets(0,0,0,0);
309 public final Insets
getInsets(Insets insets
)
320 public boolean isOptimizedDrawingEnabled()
325 public void paint(Graphics g
)
330 public void addChangeListener(ChangeListener listener
)
332 listenerList
.add(ChangeListener
.class, listener
);
335 public void removeChangeListener(ChangeListener listener
)
337 listenerList
.remove(ChangeListener
.class, listener
);
340 public ChangeListener
[] getChangeListeners()
342 return (ChangeListener
[]) getListeners(ChangeListener
.class);
345 protected void fireStateChanged()
347 ChangeListener
[] listeners
= getChangeListeners();
348 for (int i
= 0; i
< listeners
.length
; ++i
)
349 listeners
[i
].stateChanged(changeEvent
);
353 * This method returns the String ID of the UI class of Separator.
355 * @return The UI class' String ID.
357 public String
getUIClassID()
363 * This method resets the UI used to the Look and Feel defaults..
365 public void updateUI()
367 setUI((ViewportUI
) UIManager
.getUI(this));
371 * This method returns the viewport's UI delegate.
373 * @return The viewport's UI delegate.
375 public ViewportUI
getUI()
377 return (ViewportUI
) ui
;
381 * This method sets the viewport's UI delegate.
383 * @param ui The viewport's UI delegate.
385 public void setUI(ViewportUI ui
)
390 public final void setBorder(Border border
)
393 throw new IllegalArgumentException();