下面列出了怎么用java.awt.Adjustable的API类实例代码及写法,或者点击链接到github查看源代码。
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
/**
* Scrolls pane to bottom.
*
* @throws TimeoutExpiredException
*/
public void scrollToBottom() {
output.printTrace("Scroll ScrollPane to bottom\n"
+ toStringSource());
output.printGolden("Scroll ScrollPane to bottom");
produceTimeRestricted(new Action<Void, Void>() {
@Override
public Void launch(Void obj) {
driver.scrollToMaximum(ScrollPaneOperator.this, Adjustable.VERTICAL);
return null;
}
@Override
public String getDescription() {
return "Scrolling";
}
@Override
public String toString() {
return "ScrollPaneOperator.scrollToBottom.Action{description = " + getDescription() + '}';
}
}, "ScrollbarOperator.WholeScrollTimeout");
}
@Override
void initializeImpl() {
super.initializeImpl();
final Scrollbar target = getTarget();
setLineIncrement(target.getUnitIncrement());
setPageIncrement(target.getBlockIncrement());
setValues(target.getValue(), target.getVisibleAmount(),
target.getMinimum(), target.getMaximum());
final int orientation = target.getOrientation();
final JScrollBar delegate = getDelegate();
synchronized (getDelegateLock()) {
delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
? Adjustable.HORIZONTAL
: Adjustable.VERTICAL);
delegate.addAdjustmentListener(this);
}
}
/**
* Scrolls pane to right.
*
* @throws TimeoutExpiredException
*/
public void scrollToRight() {
output.printTrace("Scroll ScrollPane to right\n"
+ toStringSource());
output.printGolden("Scroll ScrollPane to right");
produceTimeRestricted(new Action<Void, Void>() {
@Override
public Void launch(Void obj) {
driver.scrollToMaximum(ScrollPaneOperator.this, Adjustable.HORIZONTAL);
return null;
}
@Override
public String getDescription() {
return "Scrolling";
}
@Override
public String toString() {
return "ScrollPaneOperator.scrollToRight.Action{description = " + getDescription() + '}';
}
}, "ScrollbarOperator.WholeScrollTimeout");
}
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
@Override
void initializeImpl() {
super.initializeImpl();
final Scrollbar target = getTarget();
setLineIncrement(target.getUnitIncrement());
setPageIncrement(target.getBlockIncrement());
setValues(target.getValue(), target.getVisibleAmount(),
target.getMinimum(), target.getMaximum());
final int orientation = target.getOrientation();
final JScrollBar delegate = getDelegate();
synchronized (getDelegateLock()) {
delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
? Adjustable.HORIZONTAL
: Adjustable.VERTICAL);
delegate.addAdjustmentListener(this);
}
}
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
int x = (int) trackBounds.getX();
int y = (int) trackBounds.getY();
int w = (int) trackBounds.getWidth();
int h = (int) trackBounds.getHeight();
g.setColor(Colors.SCROLLBAR_TRACK_BACKGROUND);
g.fillRect(x - 1, y - 1, w + 2, h + 2);
g.setColor(Colors.SCROLLBAR_TRACK_BORDER);
if (this.scrollbar.getOrientation() == Adjustable.HORIZONTAL) {
g.drawLine(x, y, x + w, y);
} else {
g.drawLine(x, y, x, y + h);
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
@Override
void initializeImpl() {
super.initializeImpl();
final Scrollbar target = getTarget();
setLineIncrement(target.getUnitIncrement());
setPageIncrement(target.getBlockIncrement());
setValues(target.getValue(), target.getVisibleAmount(),
target.getMinimum(), target.getMaximum());
final int orientation = target.getOrientation();
final JScrollBar delegate = getDelegate();
synchronized (getDelegateLock()) {
delegate.setOrientation(orientation == Scrollbar.HORIZONTAL
? Adjustable.HORIZONTAL
: Adjustable.VERTICAL);
delegate.addAdjustmentListener(this);
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static void handleWheelScrolling(ScrollPane sp, MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("x = " + e.getX() + ", y = " + e.getY() + ", src is " + e.getSource());
}
int increment = 0;
if (sp != null && e.getScrollAmount() != 0) {
Adjustable adj = getAdjustableToScroll(sp);
if (adj != null) {
increment = getIncrementFromAdjustable(adj, e);
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("increment from adjustable(" + adj.getClass() + ") : " + increment);
}
scrollAdjustable(adj, increment);
}
}
}
public static int getIncrementFromAdjustable(Adjustable adj,
MouseWheelEvent e) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
}
int increment = 0;
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
increment = e.getUnitsToScroll() * adj.getUnitIncrement();
}
else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
increment = adj.getBlockIncrement() * e.getWheelRotation();
}
return increment;
}
@Override
public void scrollToMaximum(ComponentOperator oper, final int orientation) {
final Adjustable adj
= (orientation == Scrollbar.HORIZONTAL)
? ((ScrollPaneOperator) oper).getHAdjustable()
: ((ScrollPaneOperator) oper).getVAdjustable();
scroll(oper,
new ScrollAdjuster() {
@Override
public int getScrollDirection() {
return (((adj.getMaximum() - adj.getVisibleAmount()) > adj.getValue())
? INCREASE_SCROLL_DIRECTION
: DO_NOT_TOUCH_SCROLL_DIRECTION);
}
@Override
public int getScrollOrientation() {
return orientation;
}
@Override
public String getDescription() {
return "Scroll to maximum";
}
@Override
public String toString() {
return "scrollToMaximum.ScrollAdjuster{description = " + getDescription() + '}';
}
});
}
public static void scrollAdjustable(Adjustable adj, int amount) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
if (amount == 0) {
log.fine("Assertion (amount != 0) failed");
}
}
int current = adj.getValue();
int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("doScrolling by " + amount);
}
if (amount > 0 && current < upperLimit) { // still some room to scroll
// down
if (current + amount < upperLimit) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(upperLimit);
return;
}
}
else if (amount < 0 && current > adj.getMinimum()) { // still some room
// to scroll up
if (current + amount > adj.getMinimum()) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(adj.getMinimum());
return;
}
}
}
public static void scrollAdjustable(Adjustable adj, int amount) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
if (amount == 0) {
log.fine("Assertion (amount != 0) failed");
}
}
int current = adj.getValue();
int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("doScrolling by " + amount);
}
if (amount > 0 && current < upperLimit) { // still some room to scroll
// down
if (current + amount < upperLimit) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(upperLimit);
return;
}
}
else if (amount < 0 && current > adj.getMinimum()) { // still some room
// to scroll up
if (current + amount > adj.getMinimum()) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(adj.getMinimum());
return;
}
}
}
public static void scrollAdjustable(Adjustable adj, int amount) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
if (amount == 0) {
log.fine("Assertion (amount != 0) failed");
}
}
int current = adj.getValue();
int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("doScrolling by " + amount);
}
if (amount > 0 && current < upperLimit) { // still some room to scroll
// down
if (current + amount < upperLimit) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(upperLimit);
return;
}
}
else if (amount < 0 && current > adj.getMinimum()) { // still some room
// to scroll up
if (current + amount > adj.getMinimum()) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(adj.getMinimum());
return;
}
}
}
/**
* Maps {@code ScrollPane.getVAdjustable()} through queue
*/
public Adjustable getVAdjustable() {
return (runMapping(new MapAction<Adjustable>("getVAdjustable") {
@Override
public Adjustable map() {
return ((ScrollPane) getSource()).getVAdjustable();
}
}));
}
public static void scrollAdjustable(Adjustable adj, int amount) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
if (amount == 0) {
log.fine("Assertion (amount != 0) failed");
}
}
int current = adj.getValue();
int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("doScrolling by " + amount);
}
if (amount > 0 && current < upperLimit) { // still some room to scroll
// down
if (current + amount < upperLimit) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(upperLimit);
return;
}
}
else if (amount < 0 && current > adj.getMinimum()) { // still some room
// to scroll up
if (current + amount > adj.getMinimum()) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(adj.getMinimum());
return;
}
}
}
@Override
public Dimension getPreferredSize(JComponent c) {
if (this.scrollbar.getOrientation() == Adjustable.VERTICAL) {
return new Dimension(SCROLLBAR_WIDTH, 53 + 10);
} else {
return new Dimension(100, SCROLLBAR_WIDTH);
}
}
public static void scrollAdjustable(Adjustable adj, int amount) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
if (adj == null) {
log.fine("Assertion (adj != null) failed");
}
if (amount == 0) {
log.fine("Assertion (amount != 0) failed");
}
}
int current = adj.getValue();
int upperLimit = adj.getMaximum() - adj.getVisibleAmount();
if (log.isLoggable(PlatformLogger.Level.FINER)) {
log.finer("doScrolling by " + amount);
}
if (amount > 0 && current < upperLimit) { // still some room to scroll
// down
if (current + amount < upperLimit) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(upperLimit);
return;
}
}
else if (amount < 0 && current > adj.getMinimum()) { // still some room
// to scroll up
if (current + amount > adj.getMinimum()) {
adj.setValue(current + amount);
return;
}
else {
adj.setValue(adj.getMinimum());
return;
}
}
}
/**
* Scrolls vertical scroll bar.
*
* @param proportionalValue Value to scroll vertical scroll bar to.
* @throws TimeoutExpiredException
*/
public void scrollToVerticalValue(double proportionalValue) {
output.printTrace("Scroll ScrollPane to " + Double.toString(proportionalValue) + " proportional vertical value \n"
+ toStringSource());
output.printGolden("Scroll ScrollPane to " + Double.toString(proportionalValue) + " proportional vertical value");
Adjustable adj = getVAdjustable();
scrollTo(new ValueScrollAdjuster((int) (adj.getMinimum()
+ (adj.getMaximum()
- adj.getVisibleAmount()
- adj.getMinimum()) * proportionalValue),
Adjustable.VERTICAL,
getVAdjustable()));
}