| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MaxHeightJScrollPane |
|
| 1.3;1.3 |
| 1 | package us.daveread.basicquery.gui; | |
| 2 | ||
| 3 | import java.awt.Component; | |
| 4 | import java.awt.Dimension; | |
| 5 | import javax.swing.JScrollPane; | |
| 6 | ||
| 7 | /** | |
| 8 | * A fixed height version of the JScrollPane where the height can be modified | |
| 9 | */ | |
| 10 | public class MaxHeightJScrollPane extends JScrollPane { | |
| 11 | /** | |
| 12 | * Serial UID | |
| 13 | */ | |
| 14 | private static final long serialVersionUID = 5437758260344332149L; | |
| 15 | ||
| 16 | /** | |
| 17 | * Minimum height for the scroll pane | |
| 18 | */ | |
| 19 | private static final int MIN_HEIGHT = 20; | |
| 20 | ||
| 21 | /** | |
| 22 | * Maximum height for the scroll pane | |
| 23 | */ | |
| 24 | private int maxHeight; | |
| 25 | ||
| 26 | /** | |
| 27 | * Constructs an empty MaxHeightJScrollPane where scrollbars appear | |
| 28 | * when needed | |
| 29 | */ | |
| 30 | 1 | public MaxHeightJScrollPane() { |
| 31 | 1 | } |
| 32 | ||
| 33 | /** | |
| 34 | * Constructs a MaxHeighJScrollPane where the scrollbars will appear | |
| 35 | * only when the dimensions are larger than the view. | |
| 36 | * | |
| 37 | * @param comp | |
| 38 | * The component that will be contained within this | |
| 39 | * JScrollPane. | |
| 40 | */ | |
| 41 | public MaxHeightJScrollPane(Component comp) { | |
| 42 | 6 | super(comp); |
| 43 | 6 | } |
| 44 | ||
| 45 | /** | |
| 46 | * Constructs a MaxJScrollpane whose view position can be controlled | |
| 47 | * by a pair of scrollbars.The scrollbar policies define when the | |
| 48 | * scrollbars are needed | |
| 49 | * | |
| 50 | * @param comp | |
| 51 | * The component that appears in the scrollpanes viewport | |
| 52 | * @param vsbPolicy | |
| 53 | * The integer that defines the vertical scrollbar policy | |
| 54 | * @param hsbPolicy | |
| 55 | * The integer that defines the horizontal scrollbar | |
| 56 | * policy | |
| 57 | * | |
| 58 | * @see #VerticalScrollBarPolicy(int) | |
| 59 | * @see #HorizontalScrollBarPolicy(int) | |
| 60 | */ | |
| 61 | public MaxHeightJScrollPane(Component comp, int vsbPolicy, int hsbPolicy) { | |
| 62 | 6 | super(comp, vsbPolicy, hsbPolicy); |
| 63 | 6 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Constructs an empty MaxJScrollPane with the specified vertical | |
| 67 | * and horizontal scrollbar policies | |
| 68 | * | |
| 69 | * @param vsbPolicy | |
| 70 | * The integer that defines the vertical scrollbar policy | |
| 71 | * @param hsbPolicy | |
| 72 | * The integer that defines the horizontal scrollbar policy | |
| 73 | */ | |
| 74 | public MaxHeightJScrollPane(int vsbPolicy, int hsbPolicy) { | |
| 75 | 1 | super(vsbPolicy, hsbPolicy); |
| 76 | 1 | } |
| 77 | ||
| 78 | /** | |
| 79 | * Sets the maximum height of this JScrollPane to the current height | |
| 80 | */ | |
| 81 | public void lockHeight() { | |
| 82 | 4 | maxHeight = super.getPreferredSize().height; |
| 83 | 4 | } |
| 84 | ||
| 85 | /** | |
| 86 | * Set the maximum height of this JScrollPane to the supplied value. If the | |
| 87 | * value is less than the minimum height, the minimum height is used for the | |
| 88 | * maximum. | |
| 89 | * | |
| 90 | * @param height | |
| 91 | * Maximum height for the scroll pane | |
| 92 | */ | |
| 93 | public void lockHeight(int height) { | |
| 94 | 2 | if (height < MIN_HEIGHT) { |
| 95 | 1 | maxHeight = MIN_HEIGHT; |
| 96 | } else { | |
| 97 | 1 | maxHeight = height; |
| 98 | } | |
| 99 | 2 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Sets the maximum height of this JScrollpane | |
| 103 | * | |
| 104 | * @param height | |
| 105 | * Value of the maximum height of this JScrollPane | |
| 106 | */ | |
| 107 | public void setHeight(int height) { | |
| 108 | 1 | maxHeight = height; |
| 109 | 1 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Gets the width and height of the dimension object | |
| 113 | * | |
| 114 | * @return dimension A dimension object with the maximum height | |
| 115 | */ | |
| 116 | public Dimension getPreferredSize() { | |
| 117 | 10 | if (maxHeight != 0.0) { |
| 118 | 9 | return new Dimension(super.getPreferredSize().width, maxHeight); |
| 119 | } else { | |
| 120 | 1 | return super.getPreferredSize(); |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Returns the maximum size of this component | |
| 126 | * | |
| 127 | * @return Dimension | |
| 128 | */ | |
| 129 | public Dimension getMaximumSize() { | |
| 130 | 2 | return getPreferredSize(); |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Returns the minimum size of this component | |
| 135 | * | |
| 136 | * @return Dimension | |
| 137 | */ | |
| 138 | public Dimension getMinimumSize() { | |
| 139 | 2 | return getPreferredSize(); |
| 140 | } | |
| 141 | } |