synthscrollbarui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,375 行 · 第 1/3 页
JAVA
1,375 行
* minimum width of the (non <code>null</code>) increment/decrement buttons, * and the minimum width of the thumb. The minimum height is the * sum of the minimum heights of the same parts. The basis for * the preferred size of a horizontal scrollbar is similar. * <p> * The <code>minimumSize</code> is only computed once, subsequent * calls to this method just return a cached size. * * @param c the <code>JScrollBar</code> that's delegating this method to us * @return the minimum size of a basic <code>JScrollBar</code> * @see #getMaximumSize * @see #getPreferredSize */ public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } /** * @param c The JScrollBar that's delegating this method to us. * @return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); * @see #getMinimumSize * @see #getPreferredSize */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } protected JButton createDecreaseButton(int orientation) { SynthArrowButton synthArrowButton = new SynthArrowButton(orientation); synthArrowButton.setName("ScrollBar.button"); return synthArrowButton; } protected JButton createIncreaseButton(int orientation) { SynthArrowButton synthArrowButton = new SynthArrowButton(orientation); synthArrowButton.setName("ScrollBar.button"); return synthArrowButton; } /** * Return the smallest acceptable size for the thumb. If the scrollbar * becomes so small that this size isn't available, the thumb will be * hidden. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a shared static constant. * * @return The smallest acceptable size for the thumb. * @see #getMaximumThumbSize */ protected Dimension getMinimumThumbSize() { return minimumThumbSize; } /** * Return the largest acceptable size for the thumb. To create a fixed * size thumb one make this method and <code>getMinimumThumbSize</code> * return the same value. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a shared static constant. * * @return The largest acceptable size for the thumb. * @see #getMinimumThumbSize */ protected Dimension getMaximumThumbSize() { return maximumThumbSize; } /* * LayoutManager Implementation */ public void addLayoutComponent(String name, Component child) {} public void removeLayoutComponent(Component child) {} public Dimension preferredLayoutSize(Container scrollbarContainer) { return getPreferredSize((JComponent)scrollbarContainer); } public Dimension minimumLayoutSize(Container scrollbarContainer) { return getMinimumSize((JComponent)scrollbarContainer); } protected void layoutVScrollbar(JScrollBar sb) { Dimension sbSize = sb.getSize(); Insets sbInsets = sb.getInsets(); /* * Width and left edge of the buttons and thumb. */ // Same size as the thumb. int itemW = sbSize.width - (sbInsets.left + sbInsets.right); int itemX = sbInsets.left; /* Nominal locations of the buttons, assuming their preferred * size will fit. */ int decrButtonH = itemW; int decrButtonY = sbInsets.top; int incrButtonH = itemW; int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); /* The thumb must fit within the height left over after we * subtract the preferredSize of the buttons and the insets. */ int sbInsetsH = sbInsets.top + sbInsets.bottom; int sbButtonsH = decrButtonH + incrButtonH; float trackH = sbSize.height - (sbInsetsH + sbButtonsH); /* Compute the height and origin of the thumb. The case * where the thumb is at the bottom edge is handled specially * to avoid numerical problems in computing thumbY. Enforce * the thumbs min/max dimensions. If the thumb doesn't * fit in the track (trackH) we'll hide it later. */ float min = sb.getMinimum(); float extent = sb.getVisibleAmount(); float range = sb.getMaximum() - min; float value = sb.getValue(); int thumbH = (range <= 0) ? getMaximumThumbSize().height : (int)(trackH * (extent / range)); thumbH = Math.max(thumbH, getMinimumThumbSize().height); thumbH = Math.min(thumbH, getMaximumThumbSize().height); int thumbY = incrButtonY - thumbH; if (sb.getValue() < (sb.getMaximum() - sb.getVisibleAmount())) { float thumbRange = trackH - thumbH; thumbY = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); thumbY += decrButtonY + decrButtonH; } /* If the buttons don't fit, allocate half of the available * space to each and move the lower one (incrButton) down. */ int sbAvailButtonH = (sbSize.height - sbInsetsH); if (sbAvailButtonH < sbButtonsH) { incrButtonH = decrButtonH = sbAvailButtonH / 2; incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); } decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH); incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH); /* Update the trackRect field. */ int itrackY = decrButtonY + decrButtonH; int itrackH = incrButtonY - itrackY; trackRect.setBounds(itemX, itrackY, itemW, itrackH); /* If the thumb isn't going to fit, zero it's bounds. Otherwise * make sure it fits between the buttons. Note that setting the * thumbs bounds will cause a repaint. */ if(thumbH >= (int)trackH) { setThumbBounds(0, 0, 0, 0); } else { if ((thumbY + thumbH) > incrButtonY) { thumbY = incrButtonY - thumbH; } if (thumbY < (decrButtonY + decrButtonH)) { thumbY = decrButtonY + decrButtonH + 1; } setThumbBounds(itemX, thumbY, itemW, thumbH); } } protected void layoutHScrollbar(JScrollBar sb) { Dimension sbSize = sb.getSize(); Insets sbInsets = sb.getInsets(); /* Height and top edge of the buttons and thumb. */ int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom); int itemY = sbInsets.top; boolean ltr = sb.getComponentOrientation().isLeftToRight(); /* Nominal locations of the buttons, assuming their preferred * size will fit. */ int leftButtonW = itemH; int leftButtonX = sbInsets.left; int rightButtonW = itemH; int rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); /* The thumb must fit within the width left over after we * subtract the preferredSize of the buttons and the insets. */ int sbInsetsW = sbInsets.left + sbInsets.right; int sbButtonsW = leftButtonW + rightButtonW; float trackW = sbSize.width - (sbInsetsW + sbButtonsW); /* Compute the width and origin of the thumb. Enforce * the thumbs min/max dimensions. The case where the thumb * is at the right edge is handled specially to avoid numerical * problems in computing thumbX. If the thumb doesn't * fit in the track (trackH) we'll hide it later. */ float min = sb.getMinimum(); float max = sb.getMaximum(); float extent = sb.getVisibleAmount(); float range = max - min; float value = sb.getValue(); int thumbW = (range <= 0) ? getMaximumThumbSize().width : (int)(trackW * (extent / range)); thumbW = Math.max(thumbW, getMinimumThumbSize().width); thumbW = Math.min(thumbW, getMaximumThumbSize().width); int thumbX = ltr ? rightButtonX - thumbW : leftButtonX + leftButtonW; if (sb.getValue() < (max - sb.getVisibleAmount())) { float thumbRange = trackW - thumbW; if( ltr ) { thumbX = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); } else { thumbX = (int)(0.5f + (thumbRange * ((max - extent - value) / (range - extent)))); } thumbX += leftButtonX + leftButtonW; } /* If the buttons don't fit, allocate half of the available * space to each and move the right one over. */ int sbAvailButtonW = (sbSize.width - sbInsetsW); if (sbAvailButtonW < sbButtonsW) { rightButtonW = leftButtonW = sbAvailButtonW / 2; rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); } (ltr ? decrButton : incrButton).setBounds(leftButtonX, itemY, leftButtonW, itemH); (ltr ? incrButton : decrButton).setBounds(rightButtonX, itemY, rightButtonW, itemH); /* Update the trackRect field. */ int itrackX = leftButtonX + leftButtonW; int itrackW = rightButtonX - itrackX; trackRect.setBounds(itrackX, itemY, itrackW, itemH); /* Make sure the thumb fits between the buttons. Note * that setting the thumbs bounds causes a repaint. */ if (thumbW >= (int)trackW) { setThumbBounds(0, 0, 0, 0); } else { if (thumbX + thumbW > rightButtonX) { thumbX = rightButtonX - thumbW; } if (thumbX < leftButtonX + leftButtonW) { thumbX = leftButtonX + leftButtonW + 1; } setThumbBounds(thumbX, itemY, thumbW, itemH); } } public void layoutContainer(Container scrollbarContainer) { /* If the user is dragging the value, we'll assume that the * scrollbars layout is OK modulo the thumb which is being * handled by the dragging code. */ if (isDragging) { return; } JScrollBar scrollbar = (JScrollBar)scrollbarContainer; switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: layoutVScrollbar(scrollbar); break; case JScrollBar.HORIZONTAL: layoutHScrollbar(scrollbar); break; } } /** * Set the bounds of the thumb and force a repaint that includes * the old thumbBounds and the new one. * * @see #getThumbBounds */ protected void setThumbBounds(int x, int y, int width, int height) { /* If the thumbs bounds haven't changed, we're done. */ if ((thumbRect.x == x) && (thumbRect.y == y) && (thumbRect.width == width) && (thumbRect.height == height)) { return; } /* Update thumbRect, and repaint the union of x,y,w,h and * the old thumbRect. */ int minX = Math.min(x, thumbRect.x); int minY = Math.min(y, thumbRect.y); int maxX = Math.max(x + width, thumbRect.x + thumbRect.width); int maxY = Math.max(y + height, thumbRect.y + thumbRect.height); thumbRect.setBounds(x, y, width, height); scrollbar.repaint(minX, minY, maxX - minX, maxY - minY); // PENDING: Is there a way to get the current mouse location? setThumbActive(false); } /** * Return the current size/location of the thumb. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a reference to the actual rectangle, not a copy. * * @return The current size/location of the thumb. * @see #setThumbBounds */ protected Rectangle getThumbBounds() { return thumbRect; } /** * Returns the current bounds of the track, i.e. the space in between * the increment and decrement buttons, less the insets. The value * returned by this method is updated each time the scrollbar is * laid out (validated). * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a reference to the actual rectangle, not a copy. * * @return the current bounds of the scrollbar track * @see #layoutContainer */ protected Rectangle getTrackBounds() { return trackRect; } /* * Method for scrolling by a block increment. * Added for mouse wheel scrolling support, RFE 4202656. */ static void scrollByBlock(JScrollBar scrollbar, int direction) { // This method is called from BasicScrollPaneUI to implement wheel // scrolling, and also from scrollByBlock(). int oldValue = scrollbar.getValue(); int blockIncrement = scrollbar.getBlockIncrement(direction); int delta = blockIncrement * ((direction > 0) ? +1 : -1); scrollbar.setValue(oldValue + delta); } protected void scrollByBlock(int direction) { scrollByBlock(scrollbar, direction); trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT; Rectangle dirtyRect = getTrackBounds(); scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height); } /* * Method for scrolling by a unit increment. * Added for mouse wheel scrolling support, RFE 4202656. */ static void scrollByUnits(JScrollBar scrollbar, int direction, int units) { // This method is called from BasicScrollPaneUI to implement wheel // scrolling, as well as from scrollByUnit(). int delta = units; if (direction > 0) { delta *= scrollbar.getUnitIncrement(direction); } else { delta *= -scrollbar.getUnitIncrement(direction); } int oldValue = scrollbar.getValue(); int newValue = oldValue + delta; // Check for overflow. if (delta > 0 && newValue < oldValue) { newValue = scrollbar.getMaximum(); } else if (delta < 0 && newValue > oldValue) { newValue = scrollbar.getMinimum(); } scrollbar.setValue(newValue); } protected void scrollByUnit(int direction) { scrollByUnits(scrollbar, direction, 1); } /** * Indicates whether the user can absolutely position the offset with * a mouse click (usually the middle mouse button). * <p>The return value is determined from the UIManager property * ScrollBar.allowsAbsolutePositioning. */ private boolean getSupportsAbsolutePositioning() { return supportsAbsolutePositioning; } private void updateThumbState(int x, int y) { Rectangle rect = getThumbBounds(); setThumbActive(rect.contains(x, y)); } private void setThumbActive(boolean active) { if (thumbActive != active) { thumbActive = active; scrollbar.repaint(getThumbBounds()); } } public boolean isThumbActive() { return thumbActive; } /** * A listener to listen for model changes. * */ protected class ModelListener implements ChangeListener { public void stateChanged(ChangeEvent e) { layoutContainer(scrollbar); } } // // FocusListener // public void focusGained(FocusEvent e) { scrollbar.repaint(); } public void focusLost(FocusEvent e) { scrollbar.repaint(); } /** * Track mouse drags. */ protected class TrackListener
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?