Changeset 1391 for branches/GNU/src/gcc/libjava/java/awt/ScrollPane.java
- Timestamp:
- Apr 27, 2004, 8:39:34 PM (22 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
libjava/java/awt/ScrollPane.java (modified) (1 diff, 1 prop)
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libjava/java/awt/ScrollPane.java
-
Property cvs2svn:cvs-rev
changed from
1.1to1.1.1.2
r1390 r1391 1 /* Copyright (C) 2000 Free Software Foundation 2 3 This file is part of libgcj. 4 5 This software is copyrighted work licensed under the terms of the 6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for 7 details. */ 1 /* ScrollPane.java -- Scrolling window 2 Copyright (C) 1999, 2002 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 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. 10 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. 15 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. 20 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. 25 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. */ 37 8 38 9 39 package java.awt; 10 40 11 import java.awt.event.AdjustmentListener;12 41 import java.awt.peer.ScrollPanePeer; 13 14 /** A ScrollPane is a component that has vertical and horizontal 15 * scrollbars as well as a single child which is scrolled by them. 16 * @author Tom Tromey <[email protected]> 17 * @date December 31, 2000 42 import java.awt.peer.ContainerPeer; 43 import java.awt.peer.ComponentPeer; 44 import java.io.Serializable; 45 import javax.accessibility.Accessible; 46 47 /** 48 * This widget provides a scrollable region that allows a single 49 * subcomponent to be viewed through a smaller window. 50 * 51 * @author Aaron M. Renn ([email protected]) 52 */ 53 public class ScrollPane extends Container implements Accessible, Serializable 54 { 55 56 /* 57 * Static Variables 18 58 */ 19 public class ScrollPane extends Container 20 { 21 /** This indicates that scrollbars should only be displayed when 22 * needed. */ 23 public static final int SCROLLBARS_AS_NEEDED = 0; 24 /** This indicates that scrollbars should always be displayed. */ 25 public static final int SCROLLBARS_ALWAYS = 1; 26 /** This indicates that scrollbars should never be displayed. */ 27 public static final int SCROLLBARS_NEVER = 2; 28 29 /** Create a new ScrollPane object using the indicated scrollbar 30 * display policy. If the policy is not specified it defaults to 31 * SCROLLBARS_AS_NEEDED. The default size of this component is 32 * 100x100. 33 * @param policy The scrollbar display policy 34 */ 35 public ScrollPane () 36 { 37 this (SCROLLBARS_AS_NEEDED); 38 } 39 40 public ScrollPane (int policy) 41 { 42 if (policy != SCROLLBARS_AS_NEEDED 43 && policy != SCROLLBARS_ALWAYS 44 && policy != SCROLLBARS_NEVER) 45 throw new IllegalArgumentException ("invalid value for policy"); 46 47 this.policy = policy; 48 setSize (100, 100); 49 } 50 51 /** Add a component to this ScrollPane. 52 * @param comp The component to add 53 * @param constraints Constraints. This is ignored. 54 * @param pos Position. This must be <= 0, but is otherwise ignored. 55 */ 56 protected final void addImpl (Component comp, Object constraints, 57 int pos) 58 { 59 if (pos > 0) 60 throw new IllegalArgumentException ("pos must be <= 0"); 61 62 if (ncomponents > 0) 63 remove (component[0]); 64 65 if (comp.isLightweight ()) 66 { 67 Panel p = new Panel (); 68 p.add (comp); 69 comp = p; 70 } 71 72 super.addImpl (comp, constraints, pos); 73 } 74 75 /** This creates the component's peer. */ 76 public void addNotify () 77 { 78 if (peer == null) 79 peer = getToolkit ().createScrollPane (this); 80 super.addNotify (); 81 } 82 83 /** Lays out the components in this container. */ 84 public void doLayout () 85 { 86 ScrollPanePeer spp = (ScrollPanePeer) peer; 87 Dimension c = component[0].getPreferredSize (); 88 component[0].setSize (c.width, c.height); 89 spp.childResized (c.width, c.height); 90 // Update the scrollbar position to the closest valid value. 91 setScrollPosition (hscroll.getValue (), vscroll.getValue ()); 92 } 93 94 /** Returns an Adjustable representing the horizontal scrollbar. 95 * The methods setMaximum, setMinimum, and setVisibleAmount should 96 * not be called on this Adjustable. They will throw AWTError if 97 * called. 98 */ 99 public Adjustable getHAdjustable () 100 { 101 return hscroll; 102 } 103 104 /** Returns the height of the horizontal scrollbar. */ 105 public int getHScrollbarHeight () 106 { 107 if (peer == null) 108 return 0; 109 ScrollPanePeer spp = (ScrollPanePeer) peer; 110 return spp.getHScrollbarHeight (); 111 } 112 113 /** Returns the scrollbar display policy. */ 114 public int getScrollbarDisplayPolicy () 115 { 116 return policy; 117 } 118 119 /** Returns the viewport's scroll position. */ 120 public Point getScrollPosition () 121 { 122 return new Point (hscroll.getValue (), vscroll.getValue ()); 123 } 124 125 /** Returns an Adjustable representing the vertical scrollbar. 126 * The methods setMaximum, setMinimum, and setVisibleAmount should 127 * not be called on this Adjustable. They will throw AWTError if 128 * called. 129 */ 130 public Adjustable getVAdjustable () 131 { 132 return vscroll; 133 } 134 135 /** Returns the size of the viewport. */ 136 public Dimension getViewportSize () 137 { 138 // Note: according to the online docs, the Insets are 139 // automatically updated by the peer to include the scrollbar 140 // sizes. 141 Insets ins = getInsets (); 142 int myw = width - ins.left - ins.right; 143 int myh = height - ins.top - ins.bottom; 144 145 Dimension cs; 146 if (ncomponents > 0) 147 cs = component[0].getPreferredSize (); 148 else 149 cs = new Dimension (myw, myh); 150 151 // A little optimization -- reuse the Dimension. 152 cs.setSize (myw, myh); 153 return cs; 154 } 155 156 /** Returns the width of the vertical scrollbar. */ 157 public int getVScrollbarWidth () 158 { 159 if (peer == null) 160 return 0; 161 ScrollPanePeer spp = (ScrollPanePeer) peer; 162 return spp.getVScrollbarWidth (); 163 } 164 165 /** Generates a String representation of this ScrollPane's state. */ 166 public String paramString () 167 { 168 return ("[" + getClass ().getName () 169 + ": " + ((ncomponents > 0) ? component[0].paramString () : "") 170 + "]"); 171 } 172 173 /** Set the layout manager for this component. ScrollPane has its 174 * own layout manager and overrides this method so that the layout 175 * manager cannot be changed. 176 * @param m The new layout manager (ignored) 177 */ 178 public final void setLayout (LayoutManager m) 179 { 180 // Nothing. 181 } 182 183 /** Sets the scroll position for this ScrollPane. If the point if 184 * out of range it is silently moved within range. 185 * @param x The x coordinate 186 * @param y The y coordinate 187 */ 188 public void setScrollPosition (int x, int y) 189 { 190 // According to the JCL we throw a NullPointerException if there 191 // is no child. 192 if (ncomponents == 0) 193 throw new NullPointerException ("no child in ScrollPane"); 194 195 Dimension child_d = component[0].getPreferredSize (); 196 Dimension our_d = getViewportSize (); 197 198 int xmax = Math.max (0, child_d.width - our_d.width); 199 int ymax = Math.max (0, child_d.height - our_d.height); 200 201 if (x < 0) 202 x = 0; 203 else if (x > xmax) 204 x = xmax; 205 if (y < 0) 206 y = 0; 207 else if (y > ymax) 208 y = ymax; 209 210 ScrollPanePeer spp = (ScrollPanePeer) peer; 211 spp.setScrollPosition (x, y); 212 } 213 214 /** Sets the scroll position for this ScrollPane. If the point if 215 * out of range it is silently moved within range. 216 * @param p The new point 217 */ 218 public void setScrollPosition (Point p) 219 { 220 setScrollPosition (p.x, p.y); 221 } 222 223 // This implements the Adjustable for each scrollbar. The 224 // expectation is that the peer will look at these objects directly 225 // and modify the values in them when the user manipulates the 226 // scrollbars. This has to be done from CNI to bypass Java 227 // protection rules. The peer should also take care of calling the 228 // adjustment listeners. 229 class ScrollPaneAdjustable implements Adjustable 230 { 231 AdjustmentListener listeners; 232 int orient; 233 int unit; 234 int block; 235 int value; 236 237 public ScrollPaneAdjustable (int orient) 59 60 /** 61 * Constant indicating that scrollbars are created as needed in this 62 * windows. 63 */ 64 public static final int SCROLLBARS_AS_NEEDED = 0; 65 66 /** 67 * Constant indicating that scrollbars are always displayed in this 68 * window. 69 */ 70 public static final int SCROLLBARS_ALWAYS = 1; 71 72 /** 73 * Constant indicating that scrollbars are never displayed in this window. 74 */ 75 public static final int SCROLLBARS_NEVER = 2; 76 77 // Serialization constant 78 private static final long serialVersionUID = 7956609840827222915L; 79 80 /*************************************************************************/ 81 82 /* 83 * Instance Variables 84 */ 85 86 /** 87 * @serial The horizontal scrollbar for this window. The methods 88 * <code>setMinimum()</code>, <code>setMaximum</code>, and 89 * <code>setVisibleAmount</code> must not be called on this scrollbar. 90 */ 91 private ScrollPaneAdjustable hAdjustable; 92 93 /** 94 * @serial The vertical scrollbar for this window. The methods 95 * <code>setMinimum()</code>, <code>setMaximum</code>, and 96 * <code>setVisibleAmount</code> must not be called on this scrollbar. 97 */ 98 private ScrollPaneAdjustable vAdjustable; 99 100 /** 101 * @serial Indicates when scrollbars are displayed in this window, will 102 * be one of the constants from this class. 103 */ 104 private int scrollbarDisplayPolicy; 105 106 // Current scroll position 107 private Point scrollPosition = new Point(0, 0); 108 109 /*************************************************************************/ 110 111 /* 112 * Constructors 113 */ 114 115 /** 116 * Initializes a new instance of <code>ScrollPane</code> with a default 117 * scrollbar policy of <code>SCROLLBARS_AS_NEEDED</code>. 118 * 119 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. 120 */ 121 public 122 ScrollPane() 123 { 124 this(SCROLLBARS_AS_NEEDED); 125 } 126 127 /*************************************************************************/ 128 129 /** 130 * Initializes a new instance of <code>ScrollPane</code> with the 131 * specified scrollbar policy. 132 * 133 * @param scrollbarDisplayPolicy When to display scrollbars, which must 134 * be one of the constants defined in this class. 135 * 136 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true. 137 */ 138 public 139 ScrollPane(int scrollbarDisplayPolicy) 140 { 141 if (GraphicsEnvironment.isHeadless ()) 142 throw new HeadlessException (); 143 144 this.scrollbarDisplayPolicy = scrollbarDisplayPolicy; 145 146 if (scrollbarDisplayPolicy != SCROLLBARS_ALWAYS 147 && scrollbarDisplayPolicy != SCROLLBARS_AS_NEEDED 148 && scrollbarDisplayPolicy != SCROLLBARS_NEVER) 149 throw new IllegalArgumentException("Bad scrollbarDisplayPolicy: " + 150 scrollbarDisplayPolicy); 151 152 if (scrollbarDisplayPolicy != SCROLLBARS_NEVER) 238 153 { 239 this.orient = orient; 154 hAdjustable = new ScrollPaneAdjustable(Scrollbar.HORIZONTAL); 155 vAdjustable = new ScrollPaneAdjustable(Scrollbar.VERTICAL); 240 156 } 241 242 public void addAdjustmentListener (AdjustmentListener l) 157 } 158 159 /*************************************************************************/ 160 161 /* 162 * Instance Variables 163 */ 164 165 /** 166 * Returns the current scrollbar display policy. 167 * 168 * @return The current scrollbar display policy. 169 */ 170 public int 171 getScrollbarDisplayPolicy() 172 { 173 return(scrollbarDisplayPolicy); 174 } 175 176 /*************************************************************************/ 177 178 /** 179 * Returns the horizontal scrollbar for this object. If the scrollbar 180 * display policy is set to <code>SCROLLBARS_NEVER</code> then this 181 * will be <code>null</code>. 182 * 183 * @return The horizontal scrollbar for this window. 184 */ 185 public Adjustable 186 getHAdjustable() 187 { 188 return(hAdjustable); 189 } 190 191 /*************************************************************************/ 192 193 /** 194 * Returns the vertical scrollbar for this object. If the scrollbar 195 * display policy is set to <code>SCROLLBARS_NEVER</code> then this 196 * will be <code>null</code>. 197 * 198 * @return The horizontal scrollbar for this window. 199 */ 200 public Adjustable 201 getVAdjustable() 202 { 203 return(vAdjustable); 204 } 205 206 /*************************************************************************/ 207 208 /** 209 * Returns the current viewport size. The viewport is the region of 210 * this object's window where the child is actually displayed. 211 * 212 * @return The viewport size. 213 */ 214 public Dimension 215 getViewportSize() 216 { 217 Dimension viewsize = getSize(); 218 Insets insets = getInsets(); 219 viewsize.width = viewsize.width - (insets.left + insets.right); 220 viewsize.height = viewsize.height - (insets.top + insets.bottom); 221 222 ScrollPaneAdjustable v = (ScrollPaneAdjustable)getVAdjustable(); 223 ScrollPaneAdjustable h = (ScrollPaneAdjustable)getHAdjustable(); 224 225 if ((v != null) && v.isVisible()) 226 viewsize.width = viewsize.width - v.getSize().width; 227 if ((h != null) && h.isVisible()) 228 viewsize.height = viewsize.height - v.getSize().height; 229 230 return(viewsize); 231 } 232 233 /*************************************************************************/ 234 235 /** 236 * Returns the height of a horizontal scrollbar. 237 * 238 * @return The height of a horizontal scrollbar. 239 */ 240 public int 241 getHScrollbarHeight() 242 { 243 ScrollPanePeer spp = (ScrollPanePeer)getPeer(); 244 if (spp != null) 245 return(spp.getHScrollbarHeight()); 246 else 247 return(0); // FIXME: What to do here? 248 } 249 250 /*************************************************************************/ 251 252 /** 253 * Returns the width of a vertical scrollbar. 254 * 255 * @return The width of a vertical scrollbar. 256 */ 257 public int 258 getVScrollbarWidth() 259 { 260 ScrollPanePeer spp = (ScrollPanePeer)getPeer(); 261 if (spp != null) 262 return(spp.getVScrollbarWidth()); 263 else 264 return(0); // FIXME: What to do here? 265 } 266 267 /*************************************************************************/ 268 269 /** 270 * Returns the current scroll position of the viewport. 271 * 272 * @return The current scroll position of the viewport. 273 */ 274 public Point 275 getScrollPosition() 276 { 277 int x = 0; 278 int y = 0; 279 280 Adjustable v = getVAdjustable(); 281 Adjustable h = getHAdjustable(); 282 283 if (v != null) 284 y = v.getValue(); 285 if (h != null) 286 x = h.getValue(); 287 288 return(new Point(x, y)); 289 } 290 291 /*************************************************************************/ 292 293 /** 294 * Sets the scroll position to the specified value. 295 * 296 * @param scrollPosition The new scrollPosition. 297 * 298 * @exception IllegalArgumentException If the specified value is outside 299 * the legal scrolling range. 300 */ 301 public void 302 setScrollPosition(Point scrollPosition) throws IllegalArgumentException 303 { 304 setScrollPosition(scrollPosition.x, scrollPosition.y); 305 } 306 307 /*************************************************************************/ 308 309 /** 310 * Sets the scroll position to the specified value. 311 * 312 * @param x The new X coordinate of the scroll position. 313 * @param y The new Y coordinate of the scroll position. 314 * 315 * @exception IllegalArgumentException If the specified value is outside 316 * the legal scrolling range. 317 */ 318 public void 319 setScrollPosition(int x, int y) 320 { 321 Adjustable h = getHAdjustable(); 322 Adjustable v = getVAdjustable(); 323 324 if (h != null) 325 h.setValue(x); 326 if (v != null) 327 v.setValue(y); 328 329 ScrollPanePeer spp = (ScrollPanePeer)getPeer(); 330 if (spp != null) 331 spp.setScrollPosition(x, y); 332 } 333 334 /*************************************************************************/ 335 336 /** 337 * Notifies this object that it should create its native peer. 338 */ 339 public void 340 addNotify() 341 { 342 if (getPeer() == null) 343 return; 344 345 setPeer((ComponentPeer)getToolkit().createScrollPane(this)); 346 347 if (hAdjustable != null) 348 hAdjustable.addNotify(); 349 if (vAdjustable != null) 350 vAdjustable.removeNotify(); 351 } 352 353 /*************************************************************************/ 354 355 /** 356 * Notifies this object that it should destroy its native peers. 357 */ 358 public void 359 removeNotify() 360 { 361 if (hAdjustable != null) 362 hAdjustable.removeNotify(); 363 if (vAdjustable != null) 364 vAdjustable.removeNotify(); 365 366 super.removeNotify(); 367 } 368 369 /*************************************************************************/ 370 371 /** 372 * Adds the specified child component to this container. A 373 * <code>ScrollPane</code> can have at most one child, so if a second 374 * one is added, then first one is removed. 375 * 376 * @param component The component to add to this container. 377 * @param constraints A list of layout constraints for this object. 378 * @param index The index at which to add the child, which is ignored 379 * in this implementation. 380 */ 381 public final void 382 addImpl(Component component, Object constraints, int index) 383 { 384 Component[] list = getComponents(); 385 if ((list != null) && (list.length > 0)) 386 remove(list[0]); 387 388 super.addImpl(component, constraints, -1); 389 390 doLayout(); 391 } 392 393 /*************************************************************************/ 394 395 /** 396 * Lays out this component. This consists of resizing the sole child 397 * component to its perferred size. 398 */ 399 public void 400 doLayout() 401 { 402 Component[] list = getComponents(); 403 if ((list != null) && (list.length > 0)) 243 404 { 244 listeners = AWTEventMulticaster.add (listeners, l); 405 Dimension dim = list[0].getPreferredSize(); 406 list[0].resize(dim); 407 408 Point p = getScrollPosition(); 409 if (p.x > dim.width) 410 p.x = dim.width; 411 if (p.y > dim.height) 412 p.y = dim.height; 413 414 setScrollPosition(p); 245 415 } 246 247 public int getBlockIncrement () 248 { 249 return block; 250 } 251 252 public int getMaximum () 253 { 254 Dimension child_d = component[0].getPreferredSize (); 255 Dimension our_d = getViewportSize (); 256 257 int xmax = Math.max (0, child_d.width - our_d.width); 258 int ymax = Math.max (0, child_d.height - our_d.height); 259 260 return (orient == Adjustable.HORIZONTAL) ? xmax : ymax; 261 } 262 263 public int getMinimum () 264 { 265 return 0; 266 } 267 268 public int getOrientation () 269 { 270 return orient; 271 } 272 273 public int getUnitIncrement () 274 { 275 return unit; 276 } 277 278 public int getValue () 279 { 280 return value; 281 } 282 283 public int getVisibleAmount () 284 { 285 Dimension d = getViewportSize (); 286 return (orient == Adjustable.HORIZONTAL) ? d.width : d.height; 287 } 288 289 public void removeAdjustmentListener (AdjustmentListener l) 290 { 291 listeners = AWTEventMulticaster.remove (listeners, l); 292 } 293 294 public void setBlockIncrement (int b) 295 { 296 throw new AWTError ("can't use setBlockIncrement on this Adjustable"); 297 } 298 299 public void setMaximum (int max) 300 { 301 throw new AWTError ("can't use setMaximum on this Adjustable"); 302 } 303 304 public void setMinimum (int min) 305 { 306 throw new AWTError ("can't use setMinimum on this Adjustable"); 307 } 308 309 public void setUnitIncrement (int u) 310 { 311 unit = u; 312 if (peer != null) 313 { 314 ScrollPanePeer spp = (ScrollPanePeer) peer; 315 spp.setUnitIncrement (this, u); 316 } 317 } 318 319 public void setValue (int v) 320 { 321 value = v; 322 if (peer != null) 323 { 324 ScrollPanePeer spp = (ScrollPanePeer) peer; 325 spp.setValue (this, v); 326 } 327 } 328 329 public void setVisibleAmount (int v) 330 { 331 throw new AWTError ("can't use setVisibleAmount on this Adjustable"); 332 } 333 } 334 335 ScrollPaneAdjustable hscroll 336 = new ScrollPaneAdjustable (Adjustable.HORIZONTAL); 337 ScrollPaneAdjustable vscroll 338 = new ScrollPaneAdjustable (Adjustable.VERTICAL); 339 int policy; 340 } 416 } 417 418 /*************************************************************************/ 419 420 /** 421 * Lays out this component. This consists of resizing the sole child 422 * component to its perferred size. 423 * 424 * @deprecated This method is deprecated in favor of 425 * <code>doLayout()</code>. 426 */ 427 public void 428 layout() 429 { 430 doLayout(); 431 } 432 433 /*************************************************************************/ 434 435 /** 436 * This method overrides its superclass method to ensure no layout 437 * manager is set for this container. <code>ScrollPane</code>'s do 438 * not have layout managers. 439 * 440 * @param layoutManager Ignored 441 */ 442 public final void 443 setLayout(LayoutManager layoutManager) 444 { 445 return; 446 } 447 448 /*************************************************************************/ 449 450 /** 451 * Prints all of the components in this container. 452 * 453 * @param graphics The desired graphics context for printing. 454 */ 455 public void 456 printComponents(Graphics graphics) 457 { 458 super.printComponents(graphics); 459 } 460 461 /*************************************************************************/ 462 463 /** 464 * Returns a debug string for this object. 465 * 466 * @return A debug string for this object. 467 */ 468 public String 469 paramString() 470 { 471 return(getClass().getName()); 472 } 473 474 } // class ScrollPane 475 -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.
