synthsplitpaneui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,043 行 · 第 1/5 页
JAVA
2,043 行
* Creates a PropertyChangeListener for the JSplitPane UI. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyHandler(); } /** * Creates a FocusListener for the JSplitPane UI. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * Returns the orientation for the JSplitPane. */ public int getOrientation() { return orientation; } /** * Set the orientation for the JSplitPane. */ public void setOrientation(int orientation) { this.orientation = orientation; } /** * Determines wether the JSplitPane is set to use a continuous layout. */ public boolean isContinuousLayout() { return continuousLayout; } /** * Turn continuous layout on/off. */ public void setContinuousLayout(boolean b) { continuousLayout = b; } /** * Returns the last drag location of the JSplitPane. */ public int getLastDragLocation() { return lastDragLocation; } /** * Set the last drag location of the JSplitPane. */ public void setLastDragLocation(int l) { lastDragLocation = l; } /** * @return increment via keyboard methods. */ int getKeyboardMoveIncrement() { return KEYBOARD_DIVIDER_MOVE_OFFSET; } /** * Implementation of the PropertyChangeListener * that the JSplitPane UI uses. */ class PropertyHandler implements PropertyChangeListener { /** * Messaged from the <code>JSplitPane</code> the receiver is * contained in. May potentially reset the layout manager and cause a * <code>validate</code> to be sent. */ public void propertyChange(PropertyChangeEvent e) { if(e.getSource() == splitPane) { String changeName = e.getPropertyName(); if (SynthLookAndFeel.shouldUpdateStyle(e)) { fetchStyle((JSplitPane)e.getSource()); } if(changeName.equals(JSplitPane.ORIENTATION_PROPERTY)) { orientation = splitPane.getOrientation(); resetLayoutManager(); } else if(changeName.equals( JSplitPane.CONTINUOUS_LAYOUT_PROPERTY)) { setContinuousLayout(splitPane.isContinuousLayout()); if(!isContinuousLayout()) { if(nonContinuousLayoutDivider == null) { setNonContinuousLayoutDivider( createDefaultNonContinuousLayoutDivider(), true); } else if(nonContinuousLayoutDivider.getParent() == null) { setNonContinuousLayoutDivider( nonContinuousLayoutDivider, true); } } } else if(changeName.equals(JSplitPane.DIVIDER_SIZE_PROPERTY)){ divider.setDividerSize(splitPane.getDividerSize()); dividerSize = divider.getDividerSize(); splitPane.revalidate(); splitPane.repaint(); } } } } /** * Implementation of the FocusListener that the JSplitPane UI uses. */ class FocusHandler extends FocusAdapter { public void focusGained(FocusEvent ev) { dividerKeyboardResize = true; splitPane.repaint(); } public void focusLost(FocusEvent ev) { dividerKeyboardResize = false; splitPane.repaint(); } } static class KeyboardUpLeftAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { JSplitPane splitPane = (JSplitPane)ev.getSource(); SynthSplitPaneUI ui = (SynthSplitPaneUI)splitPane.getUI(); if (ui.dividerKeyboardResize) { splitPane.setDividerLocation(Math.max(0,ui.getDividerLocation (splitPane) - ui.getKeyboardMoveIncrement())); } } } static class KeyboardDownRightAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { JSplitPane splitPane = (JSplitPane)ev.getSource(); SynthSplitPaneUI ui = (SynthSplitPaneUI)splitPane.getUI(); if (ui.dividerKeyboardResize) { splitPane.setDividerLocation(ui.getDividerLocation(splitPane) + ui.getKeyboardMoveIncrement()); } } } static class KeyboardHomeAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { JSplitPane splitPane = (JSplitPane)ev.getSource(); SynthSplitPaneUI ui = (SynthSplitPaneUI)splitPane.getUI(); if (ui.dividerKeyboardResize) { splitPane.setDividerLocation(0); } } } static class KeyboardEndAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { JSplitPane splitPane = (JSplitPane)ev.getSource(); SynthSplitPaneUI ui = (SynthSplitPaneUI)splitPane.getUI(); if (ui.dividerKeyboardResize) { Insets insets = splitPane.getInsets(); int bottomI = (insets != null) ? insets.bottom : 0; int rightI = (insets != null) ? insets.right : 0; if (ui.orientation == JSplitPane.VERTICAL_SPLIT) { splitPane.setDividerLocation(splitPane.getHeight() - bottomI); } else { splitPane.setDividerLocation(splitPane.getWidth() - rightI); } } } } static class KeyboardResizeToggleAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { JSplitPane splitPane = (JSplitPane)ev.getSource(); SynthSplitPaneUI ui = (SynthSplitPaneUI)splitPane.getUI(); if (!ui.dividerKeyboardResize) { splitPane.requestFocus(); } else { JSplitPane parentSplitPane = (JSplitPane)SwingUtilities.getAncestorOfClass(JSplitPane.class, splitPane); if (parentSplitPane!=null) { parentSplitPane.requestFocus(); } } } } /** * ActionListener that will focus on the opposite component that * has focus. EG if the left side has focus, this will transfer focus * to the right component. */ static class ToggleSideFocusAction extends AbstractAction { public void actionPerformed(ActionEvent ae) { JSplitPane splitPane = (JSplitPane)ae.getSource(); Component left = splitPane.getLeftComponent(); Component right = splitPane.getRightComponent(); KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Component focus = manager.getFocusOwner(); Component focusOn = getNextSide(splitPane, focus); if (focusOn != null) { // don't change the focus if the new focused component belongs // to the same splitpane and the same side if ( focus!=null && ( (SwingUtilities.isDescendingFrom(focus, left) && SwingUtilities.isDescendingFrom(focusOn, left)) || (SwingUtilities.isDescendingFrom(focus, right) && SwingUtilities.isDescendingFrom(focusOn, right)) ) ) { return; } SynthLookAndFeel.compositeRequestFocus(focusOn); } } private Component getNextSide(JSplitPane splitPane, Component focus) { Component left = splitPane.getLeftComponent(); Component right = splitPane.getRightComponent(); Component next = null; if (focus!=null && SwingUtilities.isDescendingFrom(focus, left) && right!=null) { next = getFirstAvailableComponent(right); if (next != null) { return next; } } JSplitPane parentSplitPane = (JSplitPane)SwingUtilities.getAncestorOfClass(JSplitPane.class, splitPane); if (parentSplitPane!=null) { // focus next side of the parent split pane next = getNextSide(parentSplitPane, focus); } else { next = getFirstAvailableComponent(left); if (next == null) { next = getFirstAvailableComponent(right); } } return next; } private Component getFirstAvailableComponent(Component c) { if (c!=null && c instanceof JSplitPane) { JSplitPane sp = (JSplitPane)c; Component left = getFirstAvailableComponent(sp.getLeftComponent()); if (left != null) { c = left; } else { c = getFirstAvailableComponent(sp.getRightComponent()); } } return c; } } /** * Action that will move focus out of the splitpane to the next * component (if present) if direction > 0 or to the previous * component (if present) if direction < 0 */ static class MoveFocusOutAction extends AbstractAction { private int direction; public MoveFocusOutAction(int newDirection) { direction = newDirection; } public void actionPerformed(ActionEvent ae) { JSplitPane splitPane = (JSplitPane)ae.getSource(); Container rootAncestor = splitPane.getFocusCycleRootAncestor(); FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component focusOn = (direction > 0) ? policy.getComponentAfter(rootAncestor, splitPane) : policy.getComponentBefore(rootAncestor, splitPane); HashSet focusFrom = new HashSet(); if (splitPane.isAncestorOf(focusOn)) { do { focusFrom.add(focusOn); rootAncestor = focusOn.getFocusCycleRootAncestor(); policy = rootAncestor.getFocusTraversalPolicy(); focusOn = (direction > 0) ? policy.getComponentAfter(rootAncestor, focusOn) : policy.getComponentBefore(rootAncestor, focusOn); } while (splitPane.isAncestorOf(focusOn) && !focusFrom.contains(focusOn)); } if ( focusOn!=null && !splitPane.isAncestorOf(focusOn) ) { focusOn.requestFocus(); } } } /** * Returns the default non continuous layout divider, which is an * instanceof Canvas that fills the background in dark gray. */ protected Component createDefaultNonContinuousLayoutDivider() { // PENDING: make this lightweight. return new Canvas() { public void paint(Graphics g) { paintDragDivider(g, 0, 0, getWidth(), getHeight());/* if(!isContinuousLayout() && getLastDragLocation() != -1) { Dimension size = splitPane.getSize(); g.setColor(Color.darkGray); if(orientation == JSplitPane.HORIZONTAL_SPLIT) { g.fillRect(0, 0, dividerSize - 1, size.height - 1); } else { g.fillRect(0, 0, size.width - 1, dividerSize - 1); } }*/ } }; } /** * Sets the divider to use when the splitPane is configured to * not continuously layout. This divider will only be used during a * dragging session. It is recommended that the passed in component * be a heavy weight. */ protected void setNonContinuousLayoutDivider(Component newDivider) { setNonContinuousLayoutDivider(newDivider, true); } /** * Sets the divider to use. */ protected void setNonContinuousLayoutDivider(Component newDivider, boolean rememberSizes) { rememberPaneSizes = rememberSizes; if(nonContinuousLayoutDivider != null && splitPane != null) { splitPane.remove(nonContinuousLayoutDivider); } nonContinuousLayoutDivider = newDivider; } private void addHeavyweightDivider() { // PENDING: nuke this. if(nonContinuousLayoutDivider != null && splitPane != null) { /* Needs to remove all the components and re-add them! YECK! */ // This is all done so that the nonContinuousLayoutDivider will // be drawn on top of the other components, without this, one // of the heavyweights will draw over the divider! Component leftC = splitPane.getLeftComponent(); Component rightC = splitPane.getRightComponent(); int lastLocation = splitPane. getDividerLocation(); if(leftC != null) splitPane.setLeftComponent(null); if(rightC != null) splitPane.setRightComponent(null); splitPane.remove(divider); splitPane.add(nonContinuousLayoutDivider, SynthSplitPaneUI. NON_CONTINUOUS_DIVIDER, splitPane.getComponentCount()); splitPane.setLeftComponent(leftC); splitPane.setRightComponent(rightC); splitPane.add(divider, JSplitPane.DIVIDER); if(rememberPaneSizes) { splitPane.setDividerLocation(lastLocation); } } } /** * Returns the divider to use when the splitPane is configured to * not continuously layout. This divider will only be used during a * dragging session. */ public Component getNonContinuousLayoutDivider() { return nonContinuousLayoutDivider; } /** * Returns the splitpane this instance is currently contained * in. */ public JSplitPane getSplitPane() { return splitPane; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?