📄 basictreeui.java
字号:
* Returns the row that the last item identified in path is visible * at. Will return -1 if any of the elements in path are not * currently visible. */ public int getRowForPath(JTree tree, TreePath path) { return (treeState != null) ? treeState.getRowForPath(path) : -1; } /** * Returns the number of rows that are being displayed. */ public int getRowCount(JTree tree) { return (treeState != null) ? treeState.getRowCount() : 0; } /** * Returns the path to the node that is closest to x,y. If * there is nothing currently visible this will return null, otherwise * it'll always return a valid path. If you need to test if the * returned object is exactly at x, y you should get the bounds for * the returned path and test x, y against that. */ public TreePath getClosestPathForLocation(JTree tree, int x, int y) { if(tree != null && treeState != null) { Insets i = tree.getInsets(); if(i == null) i = EMPTY_INSETS; return treeState.getPathClosestTo(x - i.left, y - i.top); } return null; } /** * Returns true if the tree is being edited. The item that is being * edited can be returned by getEditingPath(). */ public boolean isEditing(JTree tree) { return (editingComponent != null); } /** * Stops the current editing session. This has no effect if the * tree isn't being edited. Returns true if the editor allows the * editing session to stop. */ public boolean stopEditing(JTree tree) { if(editingComponent != null && cellEditor.stopCellEditing()) { completeEditing(false, false, true); return true; } return false; } /** * Cancels the current editing session. */ public void cancelEditing(JTree tree) { if(editingComponent != null) { completeEditing(false, true, false); } } /** * Selects the last item in path and tries to edit it. Editing will * fail if the CellEditor won't allow it for the selected item. */ public void startEditingAtPath(JTree tree, TreePath path) { tree.scrollPathToVisible(path); if(path != null && tree.isVisible(path)) startEditing(path, null); } /** * Returns the path to the element that is being edited. */ public TreePath getEditingPath(JTree tree) { return editingPath; } // // Install methods // public void installUI(JComponent c) { if ( c == null ) { throw new NullPointerException( "null component passed to BasicTreeUI.installUI()" ); } tree = (JTree)c; prepareForUIInstall(); // Boilerplate install block installDefaults(); installListeners(); installKeyboardActions(); installComponents(); completeUIInstall(); } /** * Invoked after the <code>tree</code> instance variable has been * set, but before any defaults/listeners have been installed. */ protected void prepareForUIInstall() { drawingCache = new Hashtable(7); // Data member initializations leftToRight = BasicGraphicsUtils.isLeftToRight(tree); lastWidth = tree.getWidth(); stopEditingInCompleteEditing = true; lastSelectedRow = -1; leadRow = -1; preferredSize = new Dimension(); tree.setRowHeight(UIManager.getInt("Tree.rowHeight")); Object b = UIManager.get("Tree.scrollsOnExpand"); if(b != null) tree.setScrollsOnExpand(((Boolean)b).booleanValue()); largeModel = tree.isLargeModel(); if(getRowHeight() <= 0) largeModel = false; setModel(tree.getModel()); } /** * Invoked from installUI after all the defaults/listeners have been * installed. */ protected void completeUIInstall() { // Custom install code this.setShowsRootHandles(tree.getShowsRootHandles()); updateRenderer(); updateDepthOffset(); setSelectionModel(tree.getSelectionModel()); // Create, if necessary, the TreeState instance. treeState = createLayoutCache(); configureLayoutCache(); updateSize(); } protected void installDefaults() { if(tree.getBackground() == null || tree.getBackground() instanceof UIResource) { tree.setBackground(UIManager.getColor("Tree.background")); } if(getHashColor() == null || getHashColor() instanceof UIResource) { setHashColor(UIManager.getColor("Tree.hash")); } if (tree.getFont() == null || tree.getFont() instanceof UIResource) tree.setFont( UIManager.getFont("Tree.font") ); setExpandedIcon( (Icon)UIManager.get( "Tree.expandedIcon" ) ); setCollapsedIcon( (Icon)UIManager.get( "Tree.collapsedIcon" ) ); setLeftChildIndent(((Integer)UIManager.get("Tree.leftChildIndent")). intValue()); setRightChildIndent(((Integer)UIManager.get("Tree.rightChildIndent")). intValue()); TransferHandler th = tree.getTransferHandler(); if (th == null || th instanceof UIResource) { tree.setTransferHandler(defaultTransferHandler); } DropTarget dropTarget = tree.getDropTarget(); if (dropTarget instanceof UIResource) { if (defaultDropTargetListener == null) { defaultDropTargetListener = new TreeDropTargetListener(); } try { dropTarget.addDropTargetListener(defaultDropTargetListener); } catch (TooManyListenersException tmle) { // should not happen... swing drop target is multicast } } } protected void installListeners() { if ( (propertyChangeListener = createPropertyChangeListener()) != null ) { tree.addPropertyChangeListener(propertyChangeListener); } tree.addMouseListener(defaultDragRecognizer); tree.addMouseMotionListener(defaultDragRecognizer); if ( (mouseListener = createMouseListener()) != null ) { tree.addMouseListener(mouseListener); if (mouseListener instanceof MouseMotionListener) { tree.addMouseMotionListener((MouseMotionListener)mouseListener); } } if ((focusListener = createFocusListener()) != null ) { tree.addFocusListener(focusListener); } if ((keyListener = createKeyListener()) != null) { tree.addKeyListener(keyListener); } if((treeExpansionListener = createTreeExpansionListener()) != null) { tree.addTreeExpansionListener(treeExpansionListener); } if((treeModelListener = createTreeModelListener()) != null && treeModel != null) { treeModel.addTreeModelListener(treeModelListener); } if((selectionModelPropertyChangeListener = createSelectionModelPropertyChangeListener()) != null && treeSelectionModel != null) { treeSelectionModel.addPropertyChangeListener (selectionModelPropertyChangeListener); } if((treeSelectionListener = createTreeSelectionListener()) != null && treeSelectionModel != null) { treeSelectionModel.addTreeSelectionListener(treeSelectionListener); } } protected void installKeyboardActions() { InputMap km = getInputMap(JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); SwingUtilities.replaceUIInputMap(tree, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, km); km = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, km); ActionMap am = getActionMap(); SwingUtilities.replaceUIActionMap(tree, am); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { return (InputMap)UIManager.get("Tree.ancestorInputMap"); } else if (condition == JComponent.WHEN_FOCUSED) { InputMap keyMap = (InputMap)UIManager.get("Tree.focusInputMap"); InputMap rtlKeyMap; if (tree.getComponentOrientation().isLeftToRight() || ((rtlKeyMap = (InputMap)UIManager.get("Tree.focusInputMap.RightToLeft")) == null)) { return keyMap; } else { rtlKeyMap.setParent(keyMap); return rtlKeyMap; } } return null; } ActionMap getActionMap() { return createActionMap(); } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("selectPrevious", new TreeIncrementAction(-1, "selectPrevious", false, true)); map.put("selectPreviousChangeLead", new TreeIncrementAction (-1, "selectPreviousLead", false, false)); map.put("selectPreviousExtendSelection", new TreeIncrementAction (-1, "selectPreviousExtendSelection", true, true)); map.put("selectNext", new TreeIncrementAction (1, "selectNext", false, true)); map.put("selectNextChangeLead", new TreeIncrementAction (1, "selectNextLead", false, false)); map.put("selectNextExtendSelection", new TreeIncrementAction (1, "selectNextExtendSelection", true, true)); map.put("selectChild", new TreeTraverseAction (1, "selectChild", true)); map.put("selectChildChangeLead", new TreeTraverseAction (1, "selectChildLead", false)); map.put("selectParent", new TreeTraverseAction (-1, "selectParent", true)); map.put("selectParentChangeLead", new TreeTraverseAction (-1, "selectParentLead", false)); map.put("scrollUpChangeSelection", new TreePageAction (-1, "scrollUpChangeSelection", false, true)); map.put("scrollUpChangeLead", new TreePageAction (-1, "scrollUpChangeLead", false, false)); map.put("scrollUpExtendSelection", new TreePageAction (-1, "scrollUpExtendSelection", true, true)); map.put("scrollDownChangeSelection", new TreePageAction (1, "scrollDownChangeSelection", false, true)); map.put("scrollDownExtendSelection", new TreePageAction (1, "scrollDownExtendSelection", true, true)); map.put("scrollDownChangeLead", new TreePageAction (1, "scrollDownChangeLead", false, false)); map.put("selectFirst", new TreeHomeAction (-1, "selectFirst", false, true)); map.put("selectFirstChangeLead", new TreeHomeAction (-1, "selectFirst", false, false)); map.put("selectFirstExtendSelection",new TreeHomeAction (-1, "selectFirstExtendSelection", true, true)); map.put("selectLast", new TreeHomeAction (1, "selectLast", false, true)); map.put("selectLastChangeLead", new TreeHomeAction (1, "selectLast", false, false)); map.put("selectLastExtendSelection", new TreeHomeAction (1, "selectLastExtendSelection", true, true)); map.put("toggle", new TreeToggleAction("toggle")); map.put("cancel", new TreeCancelEditingAction("cancel")); map.put("startEditing", new TreeEditAction("startEditing")); map.put("selectAll", new TreeSelectAllAction("selectAll", true)); map.put("clearSelection", new TreeSelectAllAction ("clearSelection", false)); map.put("toggleSelectionPreserveAnchor", new TreeAddSelectionAction("toggleSelectionPreserveAnchor", false)); map.put("toggleSelection", new TreeAddSelectionAction("toggleSelection", true)); map.put("extendSelection", new TreeExtendSelectionAction ("extendSelection")); map.put("scrollLeft", new ScrollAction (tree, SwingConstants.HORIZONTAL, -10)); map.put("scrollLeftExtendSelection", new TreeScrollLRAction (-1, "scrollLeftExtendSelection", true, true)); map.put("scrollRight", new ScrollAction (tree, SwingConstants.HORIZONTAL, 10)); map.put("scrollRightExtendSelection", new TreeScrollLRAction (1, "scrollRightExtendSelection", true, true)); map.put("scrollRightChangeLead", new TreeScrollLRAction (1, "scrollRightChangeLead", false, false)); map.put("scrollLeftChangeLead", new TreeScrollLRAction (-1, "scrollLeftChangeLead", false, false)); map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction()); map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction()); map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction()); return map; } /** * Intalls the subcomponents of the tree, which is the renderer pane. */ protected void installComponents() { if ((rendererPane = createCellRendererPane()) != null) { tree.add( rendererPane ); } } // // Create methods. // /** * Creates an instance of NodeDimensions that is able to determine * the size of a given node in the tree. */ protected AbstractLayoutCache.NodeDimensions createNodeDimensions() { return new NodeDimensionsHandler(); } /** * Creates a listener that is responsible that updates the UI based on * how the tree changes. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * Creates the listener responsible for updating the selection based on * mouse events. */ protected MouseListener createMouseListener() { return new MouseHandler(); } /** * Creates a listener that is responsible for updating the display * when focus is lost/gained. */ protected FocusListener createFocusListener() { return new FocusHandler(); } /** * Creates the listener reponsible for getting key events from * the tree. */ protected KeyListener createKeyListener() { return new KeyHandler(); } /** * Creates the listener responsible for getting property change * events from the selection model. */ protected PropertyChangeListener createSelectionModelPropertyChangeListener() { return new SelectionModelPropertyChangeHandler(); } /** * Creates the listener that updates the display based on selection change * methods. */ protected TreeSelectionListener createTreeSelectionListener() { return new TreeSelectionHandler(); } /** * Creates a listener to handle events from the current editor. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -