4
* Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx;
24
import javax.swing.JComponent;
25
import javax.swing.JScrollBar;
27
import org.jdesktop.swingx.util.Contract;
30
* Sizing hints for layout, useful f.i. in a Scrollable implementation.<p>
32
* Inspired by <a href=
33
* http://tips4java.wordpress.com/2009/12/20/scrollable-panel/> Rob Camick</a>.
35
* PENDING JW: naming... suggestions?
37
* @author Jeanette Winzenburg
39
public enum ScrollableSizeHint {
42
* Size should be unchanged.
47
* Size should be ajusted to parent size.
52
* Width should be streched to parent width if smaller, unchanged otherwise.
54
HORIZONTAL_STRETCH(JScrollBar.HORIZONTAL) {
60
boolean isSmallerThanParent(JComponent component) {
61
if (component.getParent() != null) {
62
return component.getParent().getWidth() >
63
component.getPreferredSize().width;
72
* Width should be streched to parent height if smaller, unchanged otherwise.
74
VERTICAL_STRETCH(JScrollBar.VERTICAL) {
80
boolean isSmallerThanParent(JComponent component) {
81
if (component.getParent() != null) {
82
return component.getParent().getHeight() >
83
component.getPreferredSize().height;
92
final int orientation;
94
ScrollableSizeHint(boolean track) {
98
ScrollableSizeHint(int orientation) {
99
this(false, orientation);
102
ScrollableSizeHint(boolean tracks, int orientation) {
103
this.tracks = tracks;
104
this.orientation = orientation;
109
* Returns a boolean indicating whether the component's size should be
110
* adjusted to parent.
112
* @param component the component resize, must not be null
113
* @return a boolean indicating whether the component's size should be
116
* @throws NullPointerException if component is null
118
public boolean getTracksParentSize(JComponent component) {
119
Contract.asNotNull(component, "component must be not-null");
120
if (orientation < 0) {
123
return isSmallerThanParent(component);
127
* Returns a boolean indicating whether the hint can be used in
128
* horizontal orientation.
130
* @return a boolean indicating whether the hint can be used in horizontal
133
public boolean isHorizontalCompatible() {
134
return (orientation < 0) ? true : JScrollBar.HORIZONTAL == orientation;
138
* Returns a boolean indicating whether the hint can be used in
139
* vertical orientation.
141
* @return a boolean indicating whether the hint can be used in vertical
144
public boolean isVerticalCompatible() {
145
return (orientation < 0) ? true : JScrollBar.VERTICAL == orientation;
149
boolean isSmallerThanParent(JComponent component) {