Merge from the pain train
[official-gcc.git] / libjava / javax / swing / JViewport.java
blob657f192fd04d86a2fda34770e727dd250979c034
1 /* JViewport.java --
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
53 /**
55 * <pre>
56 * _
57 * +-------------------------------+ ...........Y1 \
58 * | view | . \
59 * | (this component's child) | . > VY
60 * | | . / = Y2-Y1
61 * | +------------------------------+ ....Y2_/
62 * | | viewport | | .
63 * | | (this component) | | .
64 * | | | | .
65 * | | | | .
66 * | | | | .
67 * | | | | .
68 * | +------------------------------+ ....Y3
69 * | | .
70 * | . | . .
71 * | . | . .
72 * +---------.---------------------+ ...........Y4
73 * . . . .
74 * . . . .
75 * . . . .
76 * X1.......X2.....................X3.......X4
77 * \____ ___/
78 * \/
79 * VX = X2-X1
80 *</pre>
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);
104 int scrollMode;
106 protected boolean scrollUnderway;
107 protected boolean isViewSizeSet;
109 /**
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
114 * rotates its view.
116 * @see #toViewCoordinates
118 Dimension extentSize;
121 * The width and height of the view in its own coordinate space.
124 Dimension viewSize;
126 Point lastPaintPosition;
128 public JViewport()
130 setOpaque(true);
131 setScrollMode(BLIT_SCROLL_MODE);
132 updateUI();
135 public Dimension getExtentSize()
137 if (extentSize == null)
138 return toViewCoordinates(getSize());
139 else
140 return extentSize;
143 public Dimension toViewCoordinates(Dimension size)
145 return size;
148 public Point toViewCoordinates(Point p)
150 Point pos = getViewPosition();
151 return new Point(p.x + pos.x,
152 p.y + pos.y);
155 public void setExtentSize(Dimension newSize)
157 extentSize = newSize;
158 fireStateChanged();
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()
168 if (isViewSizeSet)
169 return viewSize;
170 else
172 Component view = getView();
173 if (view != null)
174 return view.getPreferredSize();
175 else
176 return new Dimension();
181 public void setViewSize(Dimension newSize)
183 viewSize = newSize;
184 Component view = getView();
185 if (view != null)
186 view.setSize(viewSize);
187 isViewSizeSet = true;
188 fireStateChanged();
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();
200 if (view == null)
201 return new Point(0,0);
202 else
204 Point p = view.getLocation();
205 p.x = -p.x;
206 p.y = -p.y;
207 return p;
211 public void setViewPosition(Point p)
213 Component view = getView();
214 if (view != null)
216 Point q = new Point(-p.x, -p.y);
217 view.setLocation(q);
218 fireStateChanged();
222 public Rectangle getViewRect()
224 return new Rectangle(getViewPosition(),
225 getExtentSize());
229 * @deprecated 1.4
231 public boolean isBackingStoreEnabled()
233 return scrollMode == BACKINGSTORE_SCROLL_MODE;
237 * @deprecated 1.4
239 public void setBackingStoreEnabled(boolean b)
241 if (b && scrollMode != BACKINGSTORE_SCROLL_MODE)
243 scrollMode = BACKINGSTORE_SCROLL_MODE;
244 fireStateChanged();
248 public void setScrollMode(int mode)
250 scrollMode = mode;
251 fireStateChanged();
254 public int getScrollMode()
256 return scrollMode;
259 public Component getView()
261 if (getComponentCount() == 0)
262 return null;
264 return getComponents()[0];
267 public void setView(Component v)
269 while (getComponentCount() > 0)
270 remove(0);
271 if (v != null)
273 add(v);
274 fireStateChanged();
278 public void revalidate()
280 fireStateChanged();
281 super.revalidate();
284 public void reshape(int x, int y, int w, int h)
286 boolean changed =
287 (x != getX())
288 || (y != getY())
289 || (w != getWidth())
290 || (h != getHeight());
291 super.reshape(x, y, w, h);
292 if (changed)
293 fireStateChanged();
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)
311 if (insets == null)
312 return getInsets();
313 insets.top = 0;
314 insets.bottom = 0;
315 insets.left = 0;
316 insets.right = 0;
317 return insets;
320 public boolean isOptimizedDrawingEnabled()
322 return false;
325 public void paint(Graphics g)
327 paintComponent(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()
359 return "ViewportUI";
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)
387 super.setUI(ui);
390 public final void setBorder(Border border)
392 if (border != null)
393 throw new IllegalArgumentException();