components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/UnidirectionalScrollPane.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing;
       
    27 
       
    28 import java.awt.*;
       
    29 import javax.swing.*;
       
    30 
       
    31 @SuppressWarnings({"serial"})
       
    32 public class UnidirectionalScrollPane extends ExtScrollPane {
       
    33     //
       
    34     // Inner classes/enums
       
    35     //
       
    36 
       
    37     public enum Orientation {
       
    38 	HORIZONTAL, VERTICAL
       
    39     }
       
    40 
       
    41     protected static class ScrollablePanel extends JPanel
       
    42 	implements Scrollable {
       
    43 
       
    44 	//
       
    45 	// Instance data
       
    46 	//
       
    47 
       
    48 	private Orientation orientation;
       
    49 	private Component view;
       
    50 
       
    51 	//
       
    52 	// Constructors
       
    53 	//
       
    54 
       
    55 	public ScrollablePanel(Orientation orientation) {
       
    56 	    this.orientation = orientation;
       
    57 	    setLayout(new BorderLayout());
       
    58 	    setOpaque(false);
       
    59 	}
       
    60 
       
    61 	public ScrollablePanel(Orientation orientation, Component view) {
       
    62 	    this(orientation);
       
    63 	    setView(view);
       
    64 	}
       
    65 
       
    66 	//
       
    67 	// Scrollable methods
       
    68 	//
       
    69 
       
    70 	@Override
       
    71 	public Dimension getPreferredScrollableViewportSize() {
       
    72 	    return getPreferredSize();
       
    73 	}
       
    74 
       
    75 	@Override
       
    76 	public int getScrollableBlockIncrement(Rectangle visibleRect,
       
    77 	    int orientation, int direction) {
       
    78 	    if (view instanceof Scrollable) {
       
    79 		return ((Scrollable)view).getScrollableBlockIncrement(
       
    80 		    visibleRect, orientation, direction);
       
    81 	    }
       
    82 
       
    83 	    if (orientation == SwingConstants.VERTICAL) {
       
    84 		return visibleRect.height;
       
    85 	    }
       
    86 
       
    87 	    return visibleRect.width;
       
    88 	}
       
    89 
       
    90 	@Override
       
    91 	public boolean getScrollableTracksViewportHeight() {
       
    92 	    return orientation != Orientation.VERTICAL;
       
    93 	}
       
    94 
       
    95 	@Override
       
    96 	public boolean getScrollableTracksViewportWidth() {
       
    97 	    return orientation != Orientation.HORIZONTAL;
       
    98 	}
       
    99 
       
   100 	@Override
       
   101 	public int getScrollableUnitIncrement(Rectangle visibleRect,
       
   102 	    int orientation, int direction) {
       
   103 	    if (view instanceof Scrollable) {
       
   104 		return ((Scrollable)view).getScrollableUnitIncrement(
       
   105 		    visibleRect, orientation, direction);
       
   106 	    }
       
   107 	    return GUIUtil.getScrollableUnitIncrement();
       
   108 	}
       
   109 
       
   110 	//
       
   111 	// ScrollablePanel methods
       
   112 	//
       
   113 
       
   114 	public Component getView() {
       
   115 	    return view;
       
   116 	}
       
   117 
       
   118 	public void setView(Component view) {
       
   119 	    this.view = view;
       
   120 	    add(view, BorderLayout.CENTER);
       
   121 	}
       
   122     }
       
   123 
       
   124     //
       
   125     // Instance data
       
   126     //
       
   127 
       
   128     private Orientation orientation;
       
   129     private ScrollablePanel scrollablePanel;
       
   130 
       
   131     //
       
   132     // Constructors
       
   133     //
       
   134 
       
   135     public UnidirectionalScrollPane(Orientation orientation) {
       
   136 	this.orientation = orientation;
       
   137 	scrollablePanel = new ScrollablePanel(orientation);
       
   138     }
       
   139 
       
   140     public UnidirectionalScrollPane(Orientation orientation,
       
   141 	Component view) {
       
   142 	this(orientation);
       
   143 
       
   144 	// Stretch the view when the viewport is larger
       
   145 //	getViewport().setLayout(new SimpleViewportLayout(true));
       
   146 
       
   147 	setViewportView(view);
       
   148     }
       
   149 
       
   150     //
       
   151     // JScollPane methods
       
   152     //
       
   153 
       
   154     @Override
       
   155     public void setViewportView(Component view) {
       
   156 	scrollablePanel.setView(view);
       
   157 	super.setViewportView(scrollablePanel);
       
   158     }
       
   159 }