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

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/ScrollableSizeHint.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-07-26 12:11:27 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100726121127-k0d3b21nhja0dn93
Tags: 1:1.6.1-1
* New upstream release.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.1: no changes needed.
* Drop Depends on JRE: not requested anymore by new Java Policy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id$
 
3
 *
 
4
 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
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.
 
11
 * 
 
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.
 
16
 * 
 
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
 
20
 *
 
21
 */
 
22
package org.jdesktop.swingx;
 
23
 
 
24
import javax.swing.JComponent;
 
25
import javax.swing.JScrollBar;
 
26
 
 
27
import org.jdesktop.swingx.util.Contract;
 
28
 
 
29
/**
 
30
 * Sizing hints for layout, useful f.i. in a Scrollable implementation.<p>
 
31
 * 
 
32
 * Inspired by <a href=
 
33
 * http://tips4java.wordpress.com/2009/12/20/scrollable-panel/> Rob Camick</a>.
 
34
 * 
 
35
 * PENDING JW: naming... suggestions?
 
36
 * 
 
37
 * @author Jeanette Winzenburg
 
38
 */
 
39
public enum ScrollableSizeHint {
 
40
 
 
41
    /**
 
42
     * Size should be unchanged.
 
43
     */
 
44
    NONE(false), 
 
45
    
 
46
    /**
 
47
     * Size should be ajusted to parent size. 
 
48
     */
 
49
    FIT(true), 
 
50
    
 
51
    /**
 
52
     * Width should be streched to parent width if smaller, unchanged otherwise.
 
53
     */
 
54
    HORIZONTAL_STRETCH(JScrollBar.HORIZONTAL) {
 
55
        /**
 
56
         * @param component
 
57
         * @return
 
58
         */
 
59
        @Override
 
60
        boolean isSmallerThanParent(JComponent component) {
 
61
            if (component.getParent() != null) {
 
62
                return component.getParent().getWidth() > 
 
63
                    component.getPreferredSize().width;
 
64
            }
 
65
 
 
66
            return false;
 
67
        }
 
68
         
 
69
    },
 
70
    
 
71
    /**
 
72
     * Width should be streched to parent height if smaller, unchanged otherwise.
 
73
     */
 
74
    VERTICAL_STRETCH(JScrollBar.VERTICAL) {
 
75
        /**
 
76
         * @param component
 
77
         * @return
 
78
         */
 
79
         @Override
 
80
        boolean isSmallerThanParent(JComponent component) {
 
81
            if (component.getParent() != null) {
 
82
                return component.getParent().getHeight() > 
 
83
                    component.getPreferredSize().height;
 
84
            }
 
85
 
 
86
            return false;
 
87
        }
 
88
        
 
89
    };
 
90
    
 
91
    final boolean tracks;
 
92
    final int orientation;
 
93
    
 
94
    ScrollableSizeHint(boolean track) {
 
95
        this(track, -1);
 
96
    }
 
97
    
 
98
    ScrollableSizeHint(int orientation) {
 
99
        this(false, orientation);
 
100
    }
 
101
    
 
102
    ScrollableSizeHint(boolean tracks, int orientation) {
 
103
        this.tracks = tracks;
 
104
        this.orientation = orientation;
 
105
        
 
106
    }
 
107
    
 
108
    /**
 
109
     * Returns a boolean indicating whether the component's size should be
 
110
     * adjusted to parent.
 
111
     *  
 
112
     * @param component the component resize, must not be null
 
113
     * @return a boolean indicating whether the component's size should be
 
114
     *    adjusted to parent
 
115
     *    
 
116
     * @throws NullPointerException if component is null   
 
117
     */
 
118
    public boolean getTracksParentSize(JComponent component) {
 
119
        Contract.asNotNull(component, "component must be not-null");
 
120
        if (orientation < 0) {
 
121
            return tracks;
 
122
        }
 
123
        return isSmallerThanParent(component);
 
124
    }
 
125
 
 
126
    /**
 
127
     * Returns a boolean indicating whether the hint can be used in 
 
128
     * horizontal orientation.
 
129
     * 
 
130
     * @return a boolean indicating whether the hint can be used in horizontal
 
131
     *   orientation. 
 
132
     */
 
133
    public boolean isHorizontalCompatible() {
 
134
        return (orientation < 0) ? true : JScrollBar.HORIZONTAL == orientation;
 
135
    }
 
136
    
 
137
    /**
 
138
     * Returns a boolean indicating whether the hint can be used in 
 
139
     * vertical orientation.
 
140
     * 
 
141
     * @return a boolean indicating whether the hint can be used in vertical
 
142
     *   orientation. 
 
143
     */
 
144
    public boolean isVerticalCompatible() {
 
145
        return (orientation < 0) ? true : JScrollBar.VERTICAL == orientation;
 
146
    }
 
147
    
 
148
    
 
149
    boolean isSmallerThanParent(JComponent component) {
 
150
        return tracks;
 
151
    }
 
152
    
 
153
}