~ubuntu-branches/ubuntu/natty/libswingx-java/natty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * $Id$
 *
 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package org.jdesktop.swingx;

import javax.swing.JComponent;
import javax.swing.JScrollBar;

import org.jdesktop.swingx.util.Contract;

/**
 * Sizing hints for layout, useful f.i. in a Scrollable implementation.<p>
 * 
 * Inspired by <a href=
 * http://tips4java.wordpress.com/2009/12/20/scrollable-panel/> Rob Camick</a>.
 * 
 * PENDING JW: naming... suggestions?
 * 
 * @author Jeanette Winzenburg
 */
public enum ScrollableSizeHint {

    /**
     * Size should be unchanged.
     */
    NONE(false), 
    
    /**
     * Size should be ajusted to parent size. 
     */
    FIT(true), 
    
    /**
     * Width should be streched to parent width if smaller, unchanged otherwise.
     */
    HORIZONTAL_STRETCH(JScrollBar.HORIZONTAL) {
        /**
         * @param component
         * @return
         */
        @Override
        boolean isSmallerThanParent(JComponent component) {
            if (component.getParent() != null) {
                return component.getParent().getWidth() > 
                    component.getPreferredSize().width;
            }

            return false;
        }
         
    },
    
    /**
     * Width should be streched to parent height if smaller, unchanged otherwise.
     */
    VERTICAL_STRETCH(JScrollBar.VERTICAL) {
        /**
         * @param component
         * @return
         */
         @Override
        boolean isSmallerThanParent(JComponent component) {
            if (component.getParent() != null) {
                return component.getParent().getHeight() > 
                    component.getPreferredSize().height;
            }

            return false;
        }
        
    };
    
    final boolean tracks;
    final int orientation;
    
    ScrollableSizeHint(boolean track) {
        this(track, -1);
    }
    
    ScrollableSizeHint(int orientation) {
        this(false, orientation);
    }
    
    ScrollableSizeHint(boolean tracks, int orientation) {
        this.tracks = tracks;
        this.orientation = orientation;
        
    }
    
    /**
     * Returns a boolean indicating whether the component's size should be
     * adjusted to parent.
     *  
     * @param component the component resize, must not be null
     * @return a boolean indicating whether the component's size should be
     *    adjusted to parent
     *    
     * @throws NullPointerException if component is null   
     */
    public boolean getTracksParentSize(JComponent component) {
        Contract.asNotNull(component, "component must be not-null");
        if (orientation < 0) {
            return tracks;
        }
        return isSmallerThanParent(component);
    }

    /**
     * Returns a boolean indicating whether the hint can be used in 
     * horizontal orientation.
     * 
     * @return a boolean indicating whether the hint can be used in horizontal
     *   orientation. 
     */
    public boolean isHorizontalCompatible() {
        return (orientation < 0) ? true : JScrollBar.HORIZONTAL == orientation;
    }
    
    /**
     * Returns a boolean indicating whether the hint can be used in 
     * vertical orientation.
     * 
     * @return a boolean indicating whether the hint can be used in vertical
     *   orientation. 
     */
    public boolean isVerticalCompatible() {
        return (orientation < 0) ? true : JScrollBar.VERTICAL == orientation;
    }
    
    
    boolean isSmallerThanParent(JComponent component) {
        return tracks;
    }
    
}