⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basictreeui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    protected CellEditorListener createCellEditorListener() {	return new CellEditorHandler();    }    /**     * Creates and returns a new ComponentHandler. This is used for     * the large model to mark the validCachedPreferredSize as invalid     * when the component moves.     */    protected ComponentListener createComponentListener() {	return new ComponentHandler();    }    /**     * Creates and returns the object responsible for updating the treestate     * when nodes expanded state changes.     */    protected TreeExpansionListener createTreeExpansionListener() {	return new TreeExpansionHandler();    }    /**     * Creates the object responsible for managing what is expanded, as     * well as the size of nodes.     */    protected AbstractLayoutCache createLayoutCache() {	if(isLargeModel() && getRowHeight() > 0) {	    return new FixedHeightLayoutCache();	}	return new VariableHeightLayoutCache();    }    /**     * Returns the renderer pane that renderer components are placed in.     */    protected CellRendererPane createCellRendererPane() {        return new CellRendererPane();    }    /**      * Creates a default cell editor.      */    protected TreeCellEditor createDefaultCellEditor() {	if(currentCellRenderer != null &&	   (currentCellRenderer instanceof DefaultTreeCellRenderer)) {	    DefaultTreeCellEditor editor = new DefaultTreeCellEditor		        (tree, (DefaultTreeCellRenderer)currentCellRenderer);	    return editor;	}	return new DefaultTreeCellEditor(tree, null);    }    /**      * Returns the default cell renderer that is used to do the      * stamping of each node.      */    protected TreeCellRenderer createDefaultCellRenderer() {	return new DefaultTreeCellRenderer();    }    /**     * Returns a listener that can update the tree when the model changes.     */    protected TreeModelListener createTreeModelListener() {	return new TreeModelHandler();    }    //    // Uninstall methods    //    public void uninstallUI(JComponent c) {	completeEditing();	prepareForUIUninstall();	uninstallDefaults();	uninstallListeners();	uninstallKeyboardActions();	uninstallComponents();	completeUIUninstall();    }    protected void prepareForUIUninstall() {    }    protected void completeUIUninstall() {	if(createdRenderer) {	    tree.setCellRenderer(null);	}	if(createdCellEditor) {	    tree.setCellEditor(null);	}	cellEditor = null;	currentCellRenderer = null;	rendererPane = null;        componentListener = null;	propertyChangeListener = null;	mouseListener = null;	focusListener = null;	keyListener = null;	setSelectionModel(null);	treeState = null;	drawingCache = null;	selectionModelPropertyChangeListener = null;	tree = null;	treeModel = null;	treeSelectionModel = null;	treeSelectionListener = null;	treeExpansionListener = null;    }    protected void uninstallDefaults() {	if (tree.getTransferHandler() instanceof UIResource) {	    tree.setTransferHandler(null);	}    }    protected void uninstallListeners() {	if(componentListener != null) {	    tree.removeComponentListener(componentListener);	}        if (propertyChangeListener != null) {	    tree.removePropertyChangeListener(propertyChangeListener);	}	tree.removeMouseListener(defaultDragRecognizer);	tree.removeMouseMotionListener(defaultDragRecognizer);        if (mouseListener != null) {	    tree.removeMouseListener(mouseListener);	    if (mouseListener instanceof MouseMotionListener) {		tree.removeMouseMotionListener((MouseMotionListener)mouseListener);	    }	}        if (focusListener != null) {	    tree.removeFocusListener(focusListener);	}        if (keyListener != null) {	    tree.removeKeyListener(keyListener);	}	if(treeExpansionListener != null) {	    tree.removeTreeExpansionListener(treeExpansionListener);	}	if(treeModel != null && treeModelListener != null) {	    treeModel.removeTreeModelListener(treeModelListener);	}	if(selectionModelPropertyChangeListener != null &&	   treeSelectionModel != null) {	    treeSelectionModel.removePropertyChangeListener		(selectionModelPropertyChangeListener);	}	if(treeSelectionListener != null && treeSelectionModel != null) {	    treeSelectionModel.removeTreeSelectionListener		               (treeSelectionListener);	}    }    protected void uninstallKeyboardActions() {	SwingUtilities.replaceUIActionMap(tree, null);	SwingUtilities.replaceUIInputMap(tree, JComponent.					 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,					 null);	SwingUtilities.replaceUIInputMap(tree, JComponent.WHEN_FOCUSED, null);    }    /**     * Uninstalls the renderer pane.     */    protected void uninstallComponents() {	if(rendererPane != null) {	    tree.remove(rendererPane);	}    }    /**     * Recomputes the right margin, and invalidates any tree states     */    private void redoTheLayout() {	if (treeState != null) {	    treeState.invalidateSizes();	}    }    //    // Painting routines.    //    public void paint(Graphics g, JComponent c) {	if (tree != c) {	    throw new InternalError("incorrect component");	}	// Should never happen if installed for a UI	if(treeState == null) {	    return;	}	// Update the lastWidth if necessary.	// This should really come from a ComponentListener installed on	// the JTree, but for the time being it is here.	int              width = tree.getWidth();	if (width != lastWidth) {	    lastWidth = width;	    if (!leftToRight) {		// For RTL when the size changes, we have to refresh the		// cache as the X position is based off the width.		redoTheLayout();		updateSize();	    }	}	Rectangle        paintBounds = g.getClipBounds();	Insets           insets = tree.getInsets();	if(insets == null)	    insets = EMPTY_INSETS;	TreePath         initialPath = getClosestPathForLocation	                               (tree, 0, paintBounds.y);	Enumeration      paintingEnumerator = treeState.getVisiblePathsFrom	                                      (initialPath);	int              row = treeState.getRowForPath(initialPath);	int              endY = paintBounds.y + paintBounds.height;	drawingCache.clear();	if(initialPath != null && paintingEnumerator != null) {	    TreePath   parentPath = initialPath;	    // Draw the lines, knobs, and rows	    // Find each parent and have them draw a line to their last child	    parentPath = parentPath.getParentPath();	    while(parentPath != null) {		paintVerticalPartOfLeg(g, paintBounds, insets, parentPath);		drawingCache.put(parentPath, Boolean.TRUE);		parentPath = parentPath.getParentPath();	    }	    boolean         done = false;	    // Information for the node being rendered.	    boolean         isExpanded;	    boolean         hasBeenExpanded;	    boolean         isLeaf;	    Rectangle       boundsBuffer = new Rectangle();	    Rectangle       bounds;	    TreePath        path;	    boolean         rootVisible = isRootVisible();	    while(!done && paintingEnumerator.hasMoreElements()) {		path = (TreePath)paintingEnumerator.nextElement();		if(path != null) {		    isLeaf = treeModel.isLeaf(path.getLastPathComponent());		    if(isLeaf)			isExpanded = hasBeenExpanded = false;		    else {			isExpanded = treeState.getExpandedState(path);			hasBeenExpanded = tree.hasBeenExpanded(path);		    }		    bounds = treeState.getBounds(path, boundsBuffer);		    if(bounds == null)			// This will only happen if the model changes out			// from under us (usually in another thread).			// Swing isn't multithreaded, but I'll put this			// check in anyway.			return;		    bounds.x += insets.left;		    bounds.y += insets.top;		    // See if the vertical line to the parent has been drawn.		    parentPath = path.getParentPath();		    if(parentPath != null) {			if(drawingCache.get(parentPath) == null) {			    paintVerticalPartOfLeg(g, paintBounds,						   insets, parentPath);			    drawingCache.put(parentPath, Boolean.TRUE);			}			paintHorizontalPartOfLeg(g, paintBounds, insets,						 bounds, path, row,						 isExpanded,						 hasBeenExpanded, isLeaf);		    }		    else if(rootVisible && row == 0) {			paintHorizontalPartOfLeg(g, paintBounds, insets,						 bounds, path, row,						 isExpanded,						 hasBeenExpanded, isLeaf);		    }		    if(shouldPaintExpandControl(path, row, isExpanded,						hasBeenExpanded, isLeaf)) {			paintExpandControl(g, paintBounds, insets, bounds,					   path, row, isExpanded,					   hasBeenExpanded, isLeaf);		    }                    //This is the quick fix for bug 4259260.  Somewhere we                    //are out by 4 pixels in the RTL layout.  Its probably                    //due to built in right-side padding in some icons.  Rather                    //than ferret out problem at the source, this compensates.             	    if (!leftToRight) {                        bounds.x +=4;                    }		    paintRow(g, paintBounds, insets, bounds, path,				 row, isExpanded, hasBeenExpanded, isLeaf);		    if((bounds.y + bounds.height) >= endY)			done = true;		}		else {		    done = true;		}		row++;	    }	}	// Empty out the renderer pane, allowing renderers to be gc'ed.	rendererPane.removeAll();    }    /**     * Paints the horizontal part of the leg. The receiver should     * NOT modify <code>clipBounds</code>, or <code>insets</code>.<p>     * NOTE: <code>parentRow</code> can be -1 if the root is not visible.     */    protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,					    Insets insets, Rectangle bounds,					    TreePath path, int row,					    boolean isExpanded,					    boolean hasBeenExpanded, boolean					    isLeaf) {        // Don't paint the legs for the root'ish node if the        int depth = path.getPathCount() - 1;	if((depth == 0 || (depth == 1 && !isRootVisible())) &&	   !getShowsRootHandles()) {	    return;        }	int clipLeft = clipBounds.x;	int clipRight = clipBounds.x + (clipBounds.width - 1);	int clipTop = clipBounds.y;	int clipBottom = clipBounds.y + (clipBounds.height - 1);	int lineY = bounds.y + bounds.height / 2;	// Offset leftX from parents indent.	if (leftToRight) {	    int leftX = bounds.x - getRightChildIndent();	    int nodeX = bounds.x - getHorizontalLegBuffer();		    if(lineY >= clipTop && lineY <= clipBottom && nodeX >= clipLeft &&	                                                 leftX <= clipRight ) {	        leftX = Math.max(Math.max(insets.left, leftX), clipLeft);		nodeX = Math.min(Math.max(insets.left, nodeX), clipRight);                if (leftX != nodeX) {                    g.setColor(getHashColor());                    paintHorizontalLine(g, tree, lineY, leftX, nodeX);                }	    }	}	else {	    int leftX = bounds.x + bounds.width + getRightChildIndent();	    int nodeX = bounds.x + bounds.width + 	                                  getHorizontalLegBuffer() - 1;	    if(lineY >= clipTop && lineY <= clipBottom &&	       leftX >= clipLeft && nodeX <= clipRight) {	        leftX = Math.min(leftX, clipRight);		nodeX = Math.max(nodeX, clipLeft);		g.setColor(getHashColor());		paintHorizontalLine(g, tree, lineY, nodeX, leftX);	    }	}    }    /**     * Paints the vertical part of the leg. The receiver should     * NOT modify <code>clipBounds</code>, <code>insets</code>.<p>     */    protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,					  Insets insets, TreePath path) {        int depth = path.getPathCount() - 1;	if (depth == 0 && !getShowsRootHandles() && !isRootVisible()) {	    return;        }	int lineX;	if (leftToRight) {	    lineX = ((depth + 1 + depthOffset) *		     totalChildIndent) - getRightChildIndent() + insets.left;	}	else {	    lineX = lastWidth - ((depth + depthOffset) *				                       totalChildIndent) - 9;	}	int clipLeft = clipBounds.x;	int clipRight = clipBounds.x + (clipBounds.width - 1);	if (lineX >= clipLeft && lineX <= clipRight) {	    int clipTop = clipBounds.y;	    int clipBottom = clipBounds.y + clipBounds.height;	    Rectangle parentBounds = getPathBounds(tree, path);	    Rectangle lastChildBounds = getPathBounds(tree,						     getLastChildPath(path));	    if(lastChildBounds == null)		// This shouldn't happen, but if the model is modified		// in another thread it is possible for this to happen.		// Swing isn't multithreaded, but I'll add this check in		// anyway.		return;	    int       top;	    if(parentBounds == null) {		top = Math.max(insets.top + getVerticalLegBuffer(),			       clipTop);	    }	    else		top = Math.max(parentBounds.y + parentBounds.height +			       getVerticalLegBuffer(), clipTop);	    if(depth == 0 && !isRootVisible()) {		TreeModel      model = getModel();		if(model != null) {		    Object        root = model.getRoot();		    if(model.getChildCount(root) > 0) {			parentBounds = getPathBounds(tree, path.				  pathByAddingChild(model.getChild(root, 0)));			if(parentBounds != null)			    top = Math.max(insets.top + getVerticalLegBuffer(),					   parentBounds.y +					   parentBounds.height / 2);		    }		}	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -