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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package com.explodingpixels.widgets.plaf;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
/**
* An orientation corresponding to a {@link javax.swing.JScrollBar}. The methods in this enumeration
* allow for orentation-agnostic calculations, which can be used when laying out a scroll bar. A
* scroll bar, regargless of it's orientation, has a length and thickness. These values correspond
* to different dimensions (x or y) depending on the orientation.
*/
public enum ScrollBarOrientation {
HORIZONTAL {
int getThickness(Dimension size) {
return size.height;
}
int getLength(Dimension size) {
return size.width;
}
int getPosition(Point point) {
return point.x;
}
Rectangle updateBoundsPosition(Rectangle bounds, int newPosition) {
bounds.setLocation(newPosition, bounds.y);
return bounds;
}
Rectangle createBounds(Component container, int position, int length) {
return new Rectangle(position, 0, length, container.getHeight());
}
Rectangle createCenteredBounds(Component container, int position, int thickness, int length) {
int y = container.getHeight() / 2 - thickness / 2;
return new Rectangle(position, y, length, thickness);
}
},
VERTICAL {
int getThickness(Dimension size) {
return size.width;
}
int getLength(Dimension size) {
return size.height;
}
int getPosition(Point point) {
return point.y;
}
Rectangle updateBoundsPosition(Rectangle bounds, int newPosition) {
bounds.setLocation(bounds.x, newPosition);
return bounds;
}
Rectangle createBounds(Component container, int position, int length) {
return new Rectangle(0, position, container.getWidth(), length);
}
Rectangle createCenteredBounds(Component container, int position, int thickness, int length) {
int x = container.getWidth() / 2 - thickness / 2;
return new Rectangle(x, position, thickness, length);
}
};
/**
* Converts a Swing scroll bar orientation (either {@link SwingConstants#HORIZONTAL} or
* {@link SwingConstants#VERTICAL} to a {@code ScrollBarOrientation}.
*
* @param swingScrollBarOrientation the Swing scroll bar orientation, either
* {@link SwingConstants#HORIZONTAL} or {@link SwingConstants#VERTICAL}
* @return the {@code ScrollBarOrientation} to the corresponding Swing scroll bar orientation.
* @throws IllegalArgumentException if the given Swing scroll bar orientation is not valid.
*/
public static ScrollBarOrientation getOrientation(int swingScrollBarOrientation) {
if (swingScrollBarOrientation != SwingConstants.HORIZONTAL
&& swingScrollBarOrientation != SwingConstants.VERTICAL) {
throw new IllegalArgumentException("The given value is not a valid scroll bar orientation.");
}
return swingScrollBarOrientation == SwingConstants.HORIZONTAL ? HORIZONTAL : VERTICAL;
}
/**
* Get's the thickness of the given size. Thickness corresponds to the dimension that does not
* vary in size. That is, a horizontal scroll bar's thickness corresponds to the y dimension,
* while a vertical scroll bar's thickness corresponds to the x dimension.
*
* @param size the 2-dimensional size to extract the thickness from.
* @return the thickness of the given size.
*/
abstract int getThickness(Dimension size);
/**
* Get's the length of the given size. Length corresponds to the dimension that varies in size.
* That is, a horizontal scroll bar's length corresponds to the x dimension, while a vertical
* scroll bar's length corresponds to the y dimension.
*
* @param size the 2-dimensional size to extract the length from.
* @return the length of the given size.
*/
abstract int getLength(Dimension size);
/**
* Get's the position from the given {@link Point}. Position refers to the dimension of a point
* on which the scroll bar scrolls. That is, a horiztonal scroll bar's position corresponds to
* the x dimension, while a vertical scroll bar's position corresponds to the y dimension.
*
* @param point the {@code Point} from which to extrac the position from.
* @return the position value of the given {@code Point}.
*/
abstract int getPosition(Point point);
/**
* Moves the given bounds to the given position. For a horiztonal scroll bar this translates
* into {@code bounds.x = newPosition}, while for a vertical scroll bar this translates into
* {@code bounds.y = newPosition}.
*
* @param bounds the bounds to update with the new position.
* @param newPosition the new position to set the bounds to.
* @return the updated bounds.
*/
abstract Rectangle updateBoundsPosition(Rectangle bounds, int newPosition);
/**
* <p>
* Creates bounds based on the given {@link Component}, position and length. The supplied
* component will be used to determine the thickness of the bounds. The position will be used
* to locate the bounds along the scrollable axis. The length will be used to determine the
* length of the bounds along the scrollable axis.
* </p><p>
* Horizontal scroll bars, the bounds will be derived like this:
* <pre>new Rectangle(position, 0, length, container.getHeigth())</pre>
* </p><p>
* Vertical scroll bar bounds will be derived like this:
* <pre>new Rectangle(0, container.getWidth(), position, length)</pre>
* </p>
*
* @param container the {@code Component} to use to determine the thickness of the bounds.
* @param position the position of the bounds.
* @param length the length of the bounds.
* @return the created bounds.
*/
abstract Rectangle createBounds(Component container, int position, int length);
/**
* <p>
* Creates bounds centered in the given {@link Component} located at the given position, with
* the given thickness and length.
* </p><p>
* Horizontal scroll bars, the bounds will be derived like this:
* <pre>new Rectangle(position, container.getHeight()/2 - thickness/2, length, thickness)</pre>
* </p><p>
* Vertical scroll bars, the bounds will be derived like this:
* <pre>new Rectangle(container.getWidth()/2 - thickness/2, position, thickness, length)</pre>
*
* @param container the {@code Component} to use to determine the thickness of the bounds.
* @param position the position of the bounds.
* @param thickness the thickness of the given bounds.
* @param length the length of the bounds.
* @return the created bounds.
*/
abstract Rectangle createCenteredBounds(Component container, int position, int thickness, int length);
}
|