use of org.apache.pivot.wtk.ScrollPane in project pivot by apache.
the class ScrollPaneSkin method layout.
@Override
public void layout() {
ScrollPane scrollPane = (ScrollPane) getComponent();
ScrollBarPolicy horizontalPolicy = scrollPane.getHorizontalScrollBarPolicy();
ScrollBarPolicy verticalPolicy = scrollPane.getVerticalScrollBarPolicy();
boolean fillWidthToCapacity = false;
boolean fillHeightToCapacity = false;
if (horizontalPolicy == ScrollBarPolicy.FILL_TO_CAPACITY) {
horizontalPolicy = ScrollBarPolicy.AUTO;
fillWidthToCapacity = true;
}
if (verticalPolicy == ScrollBarPolicy.FILL_TO_CAPACITY) {
verticalPolicy = ScrollBarPolicy.AUTO;
fillHeightToCapacity = true;
}
layoutHelper(horizontalPolicy, verticalPolicy);
Component view = scrollPane.getView();
if (view != null && (fillWidthToCapacity || fillHeightToCapacity)) {
// We assumed AUTO. Now we check our assumption to see if we
// need to adjust it to use FILL
boolean adjustWidth = false, adjustHeight = false;
if (fillWidthToCapacity) {
Component rowHeader = scrollPane.getRowHeader();
int rowHeaderWidth = rowHeader != null ? rowHeader.getWidth() : 0;
int verticalScrollBarWidth = verticalScrollBar.isVisible() ? verticalScrollBar.getWidth() : 0;
int minViewWidth = getWidth() - rowHeaderWidth - verticalScrollBarWidth;
if (view.getWidth() < minViewWidth) {
horizontalPolicy = ScrollBarPolicy.FILL;
adjustWidth = true;
}
}
if (fillHeightToCapacity) {
Component columnHeader = scrollPane.getColumnHeader();
int columnHeaderHeight = columnHeader != null ? columnHeader.getHeight() : 0;
int horizontalScrollBarHeight = horizontalScrollBar.isVisible() ? horizontalScrollBar.getHeight() : 0;
int minViewHeight = getHeight() - columnHeaderHeight - horizontalScrollBarHeight;
if (view.getHeight() < minViewHeight) {
verticalPolicy = ScrollBarPolicy.FILL;
adjustHeight = true;
}
}
if (adjustWidth || adjustHeight) {
layoutHelper(horizontalPolicy, verticalPolicy);
}
}
cachedHorizontalScrollBarHeight = horizontalScrollBar.getHeight();
cachedVerticalScrollBarWidth = verticalScrollBar.getWidth();
}
use of org.apache.pivot.wtk.ScrollPane in project pivot by apache.
the class ScrollPaneSkin method layoutHelper.
/**
* Layout helper method that assumes that the <tt>FILL_TO_CAPACITY</tt>
* scroll policy doesn't exist.
*
* @param horizontalPolicy The assumed horizontal scroll policy; musn't be
* <tt>FILL_TO_CAPACITY</tt>
* @param verticalPolicy The assumed vertical scroll policy; musn't be
* <tt>FILL_TO_CAPACITY</tt>
*/
private void layoutHelper(ScrollBarPolicy horizontalPolicy, ScrollBarPolicy verticalPolicy) {
ScrollPane scrollPane = (ScrollPane) getComponent();
int width = getWidth();
int height = getHeight();
boolean constrainWidth = (horizontalPolicy == ScrollBarPolicy.FILL);
boolean constrainHeight = (verticalPolicy == ScrollBarPolicy.FILL);
Component view = scrollPane.getView();
Component columnHeader = scrollPane.getColumnHeader();
Component rowHeader = scrollPane.getRowHeader();
Component corner = scrollPane.getCorner();
int rowHeaderWidth = 0;
if (rowHeader != null) {
rowHeaderWidth = rowHeader.getPreferredWidth(-1);
}
int columnHeaderHeight = 0;
if (columnHeader != null) {
columnHeaderHeight = columnHeader.getPreferredHeight(-1);
}
int previousViewWidth, viewWidth = 0;
int previousViewHeight, viewHeight = 0;
int previousHorizontalScrollBarHeight, horizontalScrollBarHeight = cachedHorizontalScrollBarHeight;
int previousVerticalScrollBarWidth, verticalScrollBarWidth = cachedVerticalScrollBarWidth;
int i = 0;
do {
previousViewWidth = viewWidth;
previousViewHeight = viewHeight;
previousHorizontalScrollBarHeight = horizontalScrollBarHeight;
previousVerticalScrollBarWidth = verticalScrollBarWidth;
if (view != null) {
if (constrainWidth && constrainHeight) {
viewWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
viewHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
} else if (constrainWidth) {
viewWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
viewHeight = view.getPreferredHeight(viewWidth);
} else if (constrainHeight) {
viewHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
viewWidth = view.getPreferredWidth(viewHeight);
} else {
Dimensions viewPreferredSize = view.getPreferredSize();
viewWidth = viewPreferredSize.width;
viewHeight = viewPreferredSize.height;
}
}
if (horizontalPolicy == ScrollBarPolicy.ALWAYS || (horizontalPolicy == ScrollBarPolicy.AUTO && viewWidth > width - rowHeaderWidth - verticalScrollBarWidth)) {
horizontalScrollBarHeight = horizontalScrollBar.getPreferredHeight(-1);
} else {
horizontalScrollBarHeight = 0;
}
if (verticalPolicy == ScrollBarPolicy.ALWAYS || (verticalPolicy == ScrollBarPolicy.AUTO && viewHeight > height - columnHeaderHeight - horizontalScrollBarHeight)) {
verticalScrollBarWidth = verticalScrollBar.getPreferredWidth(-1);
} else {
verticalScrollBarWidth = 0;
}
if (++i > 4) {
// Infinite loop protection
System.err.println("Breaking out of potential infinite loop");
break;
}
} while (viewWidth != previousViewWidth || viewHeight != previousViewHeight || horizontalScrollBarHeight != previousHorizontalScrollBarHeight || verticalScrollBarWidth != previousVerticalScrollBarWidth);
int scrollTop = scrollPane.getScrollTop();
int scrollLeft = scrollPane.getScrollLeft();
if (view != null) {
view.setSize(viewWidth, viewHeight);
view.setLocation(rowHeaderWidth - scrollLeft, columnHeaderHeight - scrollTop);
}
if (columnHeader != null) {
columnHeader.setSize(viewWidth, columnHeaderHeight);
columnHeader.setLocation(rowHeaderWidth - scrollLeft, 0);
}
if (rowHeader != null) {
rowHeader.setSize(rowHeaderWidth, viewHeight);
rowHeader.setLocation(0, columnHeaderHeight - scrollTop);
}
if (horizontalScrollBarHeight > 0) {
horizontalScrollBar.setVisible(true);
int horizontalScrollBarWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
horizontalScrollBar.setSize(horizontalScrollBarWidth, horizontalScrollBarHeight);
horizontalScrollBar.setLocation(rowHeaderWidth, height - horizontalScrollBarHeight);
} else {
horizontalScrollBar.setVisible(false);
}
if (verticalScrollBarWidth > 0) {
verticalScrollBar.setVisible(true);
int verticalScrollBarHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
verticalScrollBar.setSize(verticalScrollBarWidth, verticalScrollBarHeight);
verticalScrollBar.setLocation(width - verticalScrollBarWidth, columnHeaderHeight);
} else {
verticalScrollBar.setVisible(false);
}
// Handle corner components
Color backColor = getBackgroundColor();
if (columnHeaderHeight > 0 && rowHeaderWidth > 0) {
if (corner != null) {
corner.setVisible(true);
corner.setSize(rowHeaderWidth, columnHeaderHeight);
corner.setLocation(0, 0);
topLeftCorner.setVisible(false);
} else {
topLeftCorner.setVisible(true);
topLeftCorner.setSize(rowHeaderWidth, columnHeaderHeight);
topLeftCorner.setLocation(0, 0);
topLeftCorner.getStyles().put(Style.backgroundColor, backColor);
}
} else {
if (corner != null) {
corner.setVisible(false);
}
topLeftCorner.setVisible(false);
}
if (rowHeaderWidth > 0 && horizontalScrollBarHeight > 0) {
bottomLeftCorner.setVisible(true);
bottomLeftCorner.setSize(rowHeaderWidth, horizontalScrollBarHeight);
bottomLeftCorner.setLocation(0, height - horizontalScrollBarHeight);
bottomLeftCorner.getStyles().put(Style.backgroundColor, backColor);
} else {
bottomLeftCorner.setVisible(false);
}
if (verticalScrollBarWidth > 0 && horizontalScrollBarHeight > 0) {
bottomRightCorner.setVisible(true);
bottomRightCorner.setSize(verticalScrollBarWidth, horizontalScrollBarHeight);
bottomRightCorner.setLocation(width - verticalScrollBarWidth, height - horizontalScrollBarHeight);
bottomRightCorner.getStyles().put(Style.backgroundColor, backColor);
} else {
bottomRightCorner.setVisible(false);
}
if (columnHeaderHeight > 0 && verticalScrollBarWidth > 0) {
topRightCorner.setVisible(true);
topRightCorner.setSize(verticalScrollBarWidth, columnHeaderHeight);
topRightCorner.setLocation(width - verticalScrollBarWidth, 0);
topRightCorner.getStyles().put(Style.backgroundColor, backColor);
} else {
topRightCorner.setVisible(false);
}
// Perform bounds checking on the scrollTop and scrollLeft values,
// and adjust them as necessary. Make sure to do this after we've laid
// everything out, since our ViewPortListener methods rely on valid
// sizes from our components.
int maxScrollTop = getMaxScrollTop();
if (scrollTop > maxScrollTop) {
scrollPane.setScrollTop(maxScrollTop);
}
int maxScrollLeft = getMaxScrollLeft();
if (scrollLeft > maxScrollLeft) {
scrollPane.setScrollLeft(maxScrollLeft);
}
// Adjust the structure of our scroll bars. Make sure to do this after
// we adjust the scrollTop and scrollLeft values; otherwise we might
// try to set structure values that are out of bounds.
int viewportWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
horizontalScrollBar.setScope(0, viewWidth, Math.min(viewWidth, viewportWidth));
horizontalScrollBar.setBlockIncrement(Math.max(1, viewportWidth - horizontalReveal));
int viewportHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
verticalScrollBar.setScope(0, viewHeight, Math.min(viewHeight, viewportHeight));
verticalScrollBar.setBlockIncrement(Math.max(1, viewportHeight - verticalReveal));
}
use of org.apache.pivot.wtk.ScrollPane in project pivot by apache.
the class ScrollPaneSkin method mouseWheel.
@Override
public boolean mouseWheel(Component component, Mouse.ScrollType scrollType, int scrollAmount, int wheelRotation, int x, int y) {
boolean consumed = false;
ScrollPane scrollPane = (ScrollPane) getComponent();
Component view = scrollPane.getView();
if (view != null) {
// pressed while the mouse wheel was scrolled
if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
// Treat the mouse wheel as a horizontal scroll event
int previousScrollLeft = scrollPane.getScrollLeft();
int newScrollLeft = previousScrollLeft + (scrollAmount * wheelRotation * horizontalScrollBar.getUnitIncrement());
if (wheelRotation > 0) {
int maxScrollLeft = getMaxScrollLeft();
newScrollLeft = Math.min(newScrollLeft, maxScrollLeft);
if (previousScrollLeft < maxScrollLeft) {
consumed = true;
}
} else {
newScrollLeft = Math.max(newScrollLeft, 0);
if (previousScrollLeft > 0) {
consumed = true;
}
}
scrollPane.setScrollLeft(newScrollLeft);
} else {
// Treat the mouse wheel as a vertical scroll event
int previousScrollTop = scrollPane.getScrollTop();
int newScrollTop = previousScrollTop + (scrollAmount * wheelRotation * verticalScrollBar.getUnitIncrement());
if (wheelRotation > 0) {
int maxScrollTop = getMaxScrollTop();
newScrollTop = Math.min(newScrollTop, maxScrollTop);
if (previousScrollTop < maxScrollTop) {
consumed = true;
}
} else {
newScrollTop = Math.max(newScrollTop, 0);
if (previousScrollTop > 0) {
consumed = true;
}
}
scrollPane.setScrollTop(newScrollTop);
}
}
return consumed;
}
use of org.apache.pivot.wtk.ScrollPane in project pivot by apache.
the class ScrollPaneSkin method keyPressed.
/**
* Key presses have no effect if the event has already been consumed.<p>
* {@link KeyCode#UP UP} Scroll up a single scroll unit.<br>
* {@link KeyCode#DOWN DOWN} Scroll down a single scroll unit.<br>
* {@link KeyCode#LEFT LEFT} Scroll left a single scroll unit.<br>
* {@link KeyCode#RIGHT RIGHT} Scroll right a single scroll unit.<br>
* {@link KeyCode#PAGE_UP PAGE_UP} Scroll up a single scroll block.<br>
* {@link KeyCode#PAGE_DOWN PAGE_DOWN} Scroll down a single scroll block.
*
* @see ScrollBar#getBlockIncrement()
* @see ScrollBar#getUnitIncrement()
*/
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = super.keyPressed(component, keyCode, keyLocation);
if (!consumed) {
ScrollPane scrollPane = (ScrollPane) getComponent();
int scrollTop = scrollPane.getScrollTop();
int scrollLeft = scrollPane.getScrollLeft();
if (keyCode == Keyboard.KeyCode.UP) {
int newScrollTop = Math.max(scrollTop - verticalScrollBar.getUnitIncrement(), 0);
scrollPane.setScrollTop(newScrollTop);
consumed = (newScrollTop != scrollTop);
} else if (keyCode == Keyboard.KeyCode.DOWN) {
int newScrollTop = Math.min(scrollTop + verticalScrollBar.getUnitIncrement(), getMaxScrollTop());
scrollPane.setScrollTop(newScrollTop);
consumed = (newScrollTop != scrollTop);
} else if (keyCode == Keyboard.KeyCode.LEFT) {
int newScrollLeft = Math.max(scrollLeft - horizontalScrollBar.getUnitIncrement(), 0);
scrollPane.setScrollLeft(newScrollLeft);
consumed = (newScrollLeft != scrollLeft);
} else if (keyCode == Keyboard.KeyCode.RIGHT) {
int newScrollLeft = Math.min(scrollLeft + horizontalScrollBar.getUnitIncrement(), getMaxScrollLeft());
scrollPane.setScrollLeft(newScrollLeft);
consumed = (newScrollLeft != scrollLeft);
} else if (keyCode == Keyboard.KeyCode.PAGE_UP) {
int increment = verticalScrollBar.getBlockIncrement();
int newScrollTop = Math.max(scrollTop - increment, 0);
scrollPane.setScrollTop(newScrollTop);
consumed = (newScrollTop != scrollTop);
} else if (keyCode == Keyboard.KeyCode.PAGE_DOWN) {
int increment = verticalScrollBar.getBlockIncrement();
int newScrollTop = Math.min(scrollTop + increment, getMaxScrollTop());
scrollPane.setScrollTop(newScrollTop);
consumed = (newScrollTop != scrollTop);
}
}
return consumed;
}
use of org.apache.pivot.wtk.ScrollPane in project pivot by apache.
the class ScrollPaneSkin method getMaxScrollLeft.
/**
* Gets the maximum legal <tt>scrollLeft</tt> value this this skin imposes.
* This is the largest value possible that still shows as much of the view
* component as it can.
*
* @return The maximum scrollLeft value
*/
private int getMaxScrollLeft() {
int maxScrollLeft = 0;
ScrollPane scrollPane = (ScrollPane) getComponent();
Component view = scrollPane.getView();
if (view != null) {
int viewWidth = view.getWidth();
int rowHeaderWidth = 0;
int verticalScrollBarWidth = 0;
int width = getWidth();
Component rowHeader = scrollPane.getRowHeader();
if (rowHeader != null) {
rowHeaderWidth = rowHeader.getWidth();
}
if (verticalScrollBar.isVisible()) {
verticalScrollBarWidth = verticalScrollBar.getWidth();
}
maxScrollLeft = Math.max(viewWidth + rowHeaderWidth + verticalScrollBarWidth - width, 0);
}
return maxScrollLeft;
}
Aggregations