components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/ExtScrollPane.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     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 import javax.swing.plaf.basic.*;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class ExtScrollPane extends JScrollPane {
       
    34     //
       
    35     // Instance data
       
    36     //
       
    37 
       
    38     private int defaultUnitInc;
       
    39 
       
    40     //
       
    41     // Constructors
       
    42     //
       
    43 
       
    44     public ExtScrollPane() {
       
    45 	init();
       
    46     }
       
    47 
       
    48     public ExtScrollPane(Component view) {
       
    49 	super(view);
       
    50 	init();
       
    51     }
       
    52 
       
    53     public ExtScrollPane(Component view, int vsbPolicy, int hsbPolicy) {
       
    54 	super(view, vsbPolicy, hsbPolicy);
       
    55 	init();
       
    56     }
       
    57 
       
    58     public ExtScrollPane(int vsbPolicy, int hsbPolicy) {
       
    59 	super(vsbPolicy, hsbPolicy);
       
    60 	init();
       
    61     }
       
    62 
       
    63     //
       
    64     // JComponent methods
       
    65     //
       
    66 
       
    67     /**
       
    68      * Sets the opacity of this {@code JScrollPane} and its {@code JViewport}.
       
    69      */
       
    70     @Override
       
    71     public void setOpaque(boolean opaque) {
       
    72 	super.setOpaque(opaque);
       
    73 	getViewport().setOpaque(opaque);
       
    74     }
       
    75 
       
    76     //
       
    77     // JScrollPane methods
       
    78     //
       
    79 
       
    80     @Override
       
    81     protected JViewport createViewport() {
       
    82         return new JViewport() {
       
    83 	    @Override
       
    84 	    public void setView(Component view) {
       
    85 		Component oldView = getView();
       
    86 		if (oldView != view) {
       
    87 		    super.setView(view);
       
    88 		    if (view != null) {
       
    89 			setUnitIncrement();
       
    90 		    }
       
    91 		}
       
    92 	    }
       
    93 	};
       
    94     }
       
    95 
       
    96     //
       
    97     // ExtScrollPane methods
       
    98     //
       
    99 
       
   100     public void removeBorder() {
       
   101 	removeBorder(this);
       
   102     }
       
   103 
       
   104     public int getDefaultUnitIncrement() {
       
   105 	if (defaultUnitInc == 0) {
       
   106 	    defaultUnitInc = GUIUtil.getScrollableUnitIncrement();
       
   107 	}
       
   108 	return defaultUnitInc;
       
   109     }
       
   110 
       
   111     public void setDefaultUnitIncrement(int defaultUnitInc) {
       
   112 	if (this.defaultUnitInc != defaultUnitInc) {
       
   113 	    this.defaultUnitInc = defaultUnitInc;
       
   114 	    setUnitIncrement();
       
   115 	}
       
   116     }
       
   117 
       
   118     //
       
   119     // Private methods
       
   120     //
       
   121 
       
   122     private void init() {
       
   123 	setOpaque(false);
       
   124     }
       
   125 
       
   126     private void setUnitIncrement() {
       
   127 	int defaultUnitInc = getDefaultUnitIncrement();
       
   128 	int hInc = defaultUnitInc;
       
   129 	JScrollBar hScroll = getHorizontalScrollBar();
       
   130 
       
   131 	int vInc = defaultUnitInc;
       
   132 	JScrollBar vScroll = getVerticalScrollBar();
       
   133 
       
   134 	Component view = getViewport().getView();
       
   135 
       
   136 	if (view instanceof Scrollable) {
       
   137 	    Scrollable scrollable = (Scrollable)view;
       
   138 	    Rectangle r = viewport.getViewRect();
       
   139 
       
   140 	    hInc = scrollable.getScrollableUnitIncrement(
       
   141 		r, hScroll.getOrientation(), 1);
       
   142 
       
   143 	    vInc = scrollable.getScrollableUnitIncrement(
       
   144 		r, hScroll.getOrientation(), 1);
       
   145 	}
       
   146 
       
   147 	getHorizontalScrollBar().setUnitIncrement(hInc);
       
   148 	getVerticalScrollBar().setUnitIncrement(vInc);
       
   149     }
       
   150 
       
   151     //
       
   152     // Static methods
       
   153     //
       
   154 
       
   155     /**
       
   156      * Removes the border from the given {@code JScrollPane}.  Since some
       
   157      * UIs show a border under all circumstances (synth, for example), this is
       
   158      * more involved than setting the border to {@code null}.
       
   159      */
       
   160     public static void removeBorder(JScrollPane scroll) {
       
   161 	scroll.setUI(new BasicScrollPaneUI());
       
   162 
       
   163 	// Setting to null would result in a default border
       
   164 	scroll.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
       
   165 
       
   166 	scroll.getViewport().setUI(new BasicViewportUI());
       
   167     }
       
   168 }