📄 basicsplitpanedivider.java
字号:
{ if (splitPaneUI != null) splitPaneUI.startDragging(); } /** * Drags the divider to a given location by calling the * <code>dragDividerTo</code> method of the UI delegate of the enclosing * <code>JSplitPane</code>. * * @param location the new location of the divider. * * @see BasicSplitPaneUI#dragDividerTo(int location) */ protected void dragDividerTo(int location) { if (splitPaneUI != null) splitPaneUI.dragDividerTo(location); } /** * Finishes a dragging gesture by calling the <code>finishDraggingTo</code> * method of the UI delegate of the enclosing <code>JSplitPane</code>. * * @param location the new, final location of the divider. * * @see BasicSplitPaneUI#finishDraggingTo(int location) */ protected void finishDraggingTo(int location) { if (splitPaneUI != null) splitPaneUI.finishDraggingTo(location); } /** * This helper method moves the divider to one of the three locations when * using one touch expand buttons. Location 0 is the left (or top) most * location. Location 1 is the middle. Location 2 is the right (or bottom) * most location. * This is package-private to avoid an accessor method. * * @param locationIndex The location to move to. */ void moveDividerTo(int locationIndex) { Insets insets = splitPane.getInsets(); switch (locationIndex) { case 1: splitPane.setDividerLocation(splitPane.getLastDividerLocation()); break; case 0: int top = (orientation == JSplitPane.HORIZONTAL_SPLIT) ? insets.left : insets.top; splitPane.setDividerLocation(top); break; case 2: int bottom; if (orientation == JSplitPane.HORIZONTAL_SPLIT) bottom = splitPane.getBounds().width - insets.right - dividerSize; else bottom = splitPane.getBounds().height - insets.bottom - dividerSize; splitPane.setDividerLocation(bottom); break; } } /** * The listener for handling mouse events from both the divider and the * containing <code>JSplitPane</code>. * * <p> * The reason for also handling MouseEvents from the containing * <code>JSplitPane</code> is that users should be able to start a drag * gesture from inside the JSplitPane, but slightly outisde the divider. * </p> * * @author Sascha Brawer (brawer_AT_dandelis.ch) */ protected class MouseHandler extends MouseAdapter implements MouseMotionListener { /** Keeps track of whether a drag is occurring. */ private transient boolean isDragging; /** * This method is called when the mouse is pressed. * * @param e The MouseEvent. */ public void mousePressed(MouseEvent e) { if (splitPane.isOneTouchExpandable()) { if (e.getSource() == leftButton) { currentDividerLocation--; if (currentDividerLocation < 0) currentDividerLocation = 0; moveDividerTo(currentDividerLocation); return; } else if (e.getSource() == rightButton) { currentDividerLocation++; if (currentDividerLocation > 2) currentDividerLocation = 2; moveDividerTo(currentDividerLocation); return; } } isDragging = true; currentDividerLocation = 1; if (orientation == JSplitPane.HORIZONTAL_SPLIT) dragger = new DragController(e); else dragger = new VerticalDragController(e); prepareForDragging(); } /** * This method is called when the mouse is released. * * @param e The MouseEvent. */ public void mouseReleased(MouseEvent e) { if (isDragging) dragger.completeDrag(e); isDragging = false; } /** * Repeatedly invoked when the user is dragging the mouse cursor while * having pressed a mouse button. * * @param e The MouseEvent. */ public void mouseDragged(MouseEvent e) { if (dragger != null) dragger.continueDrag(e); } /** * Repeatedly invoked when the user is dragging the mouse cursor without * having pressed a mouse button. * * @param e The MouseEvent. */ public void mouseMoved(MouseEvent e) { // Do nothing. } } /** * Performs the tasks associated with an ongoing drag operation. * * @author Sascha Brawer (brawer_AT_dandelis.ch) */ protected class DragController { /** * The difference between where the mouse is clicked and the initial * divider location. */ transient int offset; /** * Creates a new DragController object. * * @param e The MouseEvent to initialize with. */ protected DragController(MouseEvent e) { offset = e.getX(); } /** * This method returns true if the divider can move. * * @return True if dragging is allowed. */ protected boolean isValid() { // Views can always be resized? return true; } /** * Returns a position for the divider given the MouseEvent. * * @param e MouseEvent. * * @return The position for the divider to move to. */ protected int positionForMouseEvent(MouseEvent e) { return e.getX() + getX() - offset; } /** * This method returns one of the two paramters for the orientation. In * this case, it returns x. * * @param x The x coordinate. * @param y The y coordinate. * * @return The x coordinate. */ protected int getNeededLocation(int x, int y) { return x; } /** * This method is called to pass on the drag information to the UI through * dragDividerTo. * * @param newX The x coordinate of the MouseEvent. * @param newY The y coordinate of the MouseEvent. */ protected void continueDrag(int newX, int newY) { if (isValid()) dragDividerTo(adjust(newX, newY)); } /** * This method is called to pass on the drag information to the UI * through dragDividerTo. * * @param e The MouseEvent. */ protected void continueDrag(MouseEvent e) { if (isValid()) dragDividerTo(positionForMouseEvent(e)); } /** * This method is called to finish the drag session by calling * finishDraggingTo. * * @param x The x coordinate of the MouseEvent. * @param y The y coordinate of the MouseEvent. */ protected void completeDrag(int x, int y) { finishDraggingTo(adjust(x, y)); } /** * This method is called to finish the drag session by calling * finishDraggingTo. * * @param e The MouseEvent. */ protected void completeDrag(MouseEvent e) { finishDraggingTo(positionForMouseEvent(e)); } /** * This is a helper method that includes the offset in the needed * location. * * @param x The x coordinate of the MouseEvent. * @param y The y coordinate of the MouseEvent. * * @return The needed location adjusted by the offsets. */ int adjust(int x, int y) { return getNeededLocation(x, y) + getX() - offset; } } /** * This is a helper class that controls dragging when the orientation is * VERTICAL_SPLIT. */ protected class VerticalDragController extends DragController { /** * Creates a new VerticalDragController object. * * @param e The MouseEvent to initialize with. */ protected VerticalDragController(MouseEvent e) { super(e); offset = e.getY(); } /** * This method returns one of the two parameters given the orientation. In * this case, it returns y. * * @param x The x coordinate of the MouseEvent. * @param y The y coordinate of the MouseEvent. * * @return The y coordinate. */ protected int getNeededLocation(int x, int y) { return y; } /** * This method returns the new location of the divider given a MouseEvent. * * @param e The MouseEvent. * * @return The new location of the divider. */ protected int positionForMouseEvent(MouseEvent e) { return e.getY() + getY() - offset; } /** * This is a helper method that includes the offset in the needed * location. * * @param x The x coordinate of the MouseEvent. * @param y The y coordinate of the MouseEvent. * * @return The needed location adjusted by the offsets. */ int adjust(int x, int y) { return getNeededLocation(x, y) + getY() - offset; } } /** * This helper class acts as the Layout Manager for the divider. */ protected class DividerLayout implements LayoutManager { /** * Creates a new DividerLayout object. */ protected DividerLayout() { // Nothing to do here. } /** * This method is called when a Component is added. * * @param string The constraints string. * @param c The Component to add. */ public void addLayoutComponent(String string, Component c) { // Do nothing. } /** * This method is called to lay out the container. * * @param c The container to lay out. */ public void layoutContainer(Container c) { if (splitPane.isOneTouchExpandable()) { changeButtonOrientation(); positionButtons(); } } /** * This method returns the minimum layout size. * * @param c The container to calculate for. * * @return The minimum layout size. */ public Dimension minimumLayoutSize(Container c) { return preferredLayoutSize(c); } /** * This method returns the preferred layout size. * * @param c The container to calculate for. * * @return The preferred layout size. */ public Dimension preferredLayoutSize(Container c) { return new Dimension(dividerSize, dividerSize); } /** * This method is called when a component is removed. * * @param c The component to remove. */ public void removeLayoutComponent(Component c) { // Do nothing. } /** * This method changes the button orientation when the orientation of the * SplitPane changes. */ private void changeButtonOrientation() { if (orientation == JSplitPane.HORIZONTAL_SPLIT) { ((BasicArrowButton) rightButton).setDirection(SwingConstants.EAST); ((BasicArrowButton) leftButton).setDirection(SwingConstants.WEST); } else { ((BasicArrowButton) rightButton).setDirection(SwingConstants.SOUTH); ((BasicArrowButton) leftButton).setDirection(SwingConstants.NORTH); } } /** * This method sizes and positions the buttons. */ private void positionButtons() { int w = 0; int h = 0; if (orientation == JSplitPane.HORIZONTAL_SPLIT) { rightButton.setLocation(ONE_TOUCH_OFFSET, ONE_TOUCH_OFFSET); leftButton.setLocation(ONE_TOUCH_OFFSET, ONE_TOUCH_OFFSET + 2 * ONE_TOUCH_SIZE); w = dividerSize - 2 * ONE_TOUCH_OFFSET; h = 2 * ONE_TOUCH_SIZE; } else { leftButton.setLocation(ONE_TOUCH_OFFSET, ONE_TOUCH_OFFSET); rightButton.setLocation(ONE_TOUCH_OFFSET + 2 * ONE_TOUCH_SIZE, ONE_TOUCH_OFFSET); h = dividerSize - 2 * ONE_TOUCH_OFFSET; w = 2 * ONE_TOUCH_SIZE; } Dimension dims = new Dimension(w, h); leftButton.setSize(dims); rightButton.setSize(dims); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -