synthtreeui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,971 行 · 第 1/5 页

JAVA
1,971
字号
        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 createCellEditor() {        TreeCellRenderer renderer = tree.getCellRenderer();        DefaultTreeCellEditor editor;	if(renderer != null && (renderer instanceof DefaultTreeCellRenderer)) {	    editor = new SynthTreeCellEditor(tree, (DefaultTreeCellRenderer)                                             renderer);	}        else {            editor = new SynthTreeCellEditor(tree, null);        }        SynthContext context = getContext(tree, ENABLED);        context.setComponentState(ENABLED | SELECTED);        context.dispose();        return editor;    }    /**      * Returns the default cell renderer that is used to do the      * stamping of each node.      */    protected TreeCellRenderer createCellRenderer() {        return new SynthTreeCellRenderer();    }    /**     * 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();	uninstallListeners();	uninstallComponents();	uninstallKeyboardActions();	uninstallDefaults();    }    protected void uninstallDefaults() {        SynthContext context = getContext(tree, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;        context = getContext(tree, Region.TREE_CELL, ENABLED);        cellStyle.uninstallDefaults(context);        context.dispose();        cellStyle = null;	if (tree.getTransferHandler() instanceof UIResource) {	    tree.setTransferHandler(null);	}        if (tree.getCellRenderer() instanceof UIResource) {	    tree.setCellRenderer(null);	}        if (tree.getCellEditor() instanceof UIResource) {	    tree.setCellEditor(null);	}        tree = null;    }    protected void uninstallListeners() {	if (componentListener != null) {	    tree.removeComponentListener(componentListener);	}        tree.removePropertyChangeListener(this);	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);	}        TreeModel treeModel = tree.getModel();	if (treeModel != null && treeModelListener != null) {	    treeModel.removeTreeModelListener(treeModelListener);	}        TreeSelectionModel selectionModel = tree.getSelectionModel();	if (treeSelectionListener != null) {	    tree.getSelectionModel().removeTreeSelectionListener		               (treeSelectionListener);	}        TreeCellEditor editor = tree.getCellEditor();        if (editor != null && cellEditorListener != null) {            editor.removeCellEditorListener(cellEditorListener);        }    }    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);	}    }    //    // Painting routines.    //    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {	Rectangle paintBounds = g.getClipBounds();	Insets insets = tree.getInsets();	TreePath initialPath = getClosestPathForLocation(tree, 0,                                                         paintBounds.y);	Enumeration paintingEnumerator = treeState.getVisiblePathsFrom	                                      (initialPath);	int row = treeState.getRowForPath(initialPath);	int endY = paintBounds.y + paintBounds.height;        TreeModel treeModel = tree.getModel();        SynthContext cellContext = getContext(tree, Region.TREE_CELL);	drawingCache.clear();	if (paintingEnumerator != null) {            // First pass, draw the rows	    boolean done = false;	    boolean isExpanded;	    boolean hasBeenExpanded;	    boolean isLeaf;	    Rectangle boundsBuffer = new Rectangle();            Rectangle rowBounds = new Rectangle(0, 0, tree.getWidth(),0);	    Rectangle bounds;	    TreePath path;            TreeCellRenderer renderer = tree.getCellRenderer();            DefaultTreeCellRenderer dtcr = (renderer instanceof                       DefaultTreeCellRenderer) ? (DefaultTreeCellRenderer)                       renderer : null;            configureRenderer(cellContext);	    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);                    adjustCellBounds(tree, bounds, insets);                    rowBounds.y = bounds.y;                    rowBounds.height = bounds.height;		    paintRow(renderer, dtcr, context, cellContext, g,                             paintBounds, insets, bounds, rowBounds, path,                             row, isExpanded, hasBeenExpanded, isLeaf);		    if ((bounds.y + bounds.height) >= endY) {			done = true;                    }		}		else {		    done = true;		}		row++;	    }	    // Draw the connecting lines and controls.	    // Find each parent and have them draw a line to their last child            boolean rootVisible = tree.isRootVisible();            TreePath parentPath = initialPath;	    parentPath = parentPath.getParentPath();	    while (parentPath != null) {                paintVerticalPartOfLeg(context, g, paintBounds,                                       insets, parentPath);		drawingCache.put(parentPath, Boolean.TRUE);		parentPath = parentPath.getParentPath();	    }	    done = false;            paintingEnumerator = treeState.getVisiblePathsFrom(initialPath);	    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);                    adjustCellBounds(tree, bounds, insets);		    // See if the vertical line to the parent has been drawn.		    parentPath = path.getParentPath();		    if (parentPath != null) {			if (drawingCache.get(parentPath) == null) {                            paintVerticalPartOfLeg(context, g,                                                   paintBounds, insets,                                                   parentPath);			    drawingCache.put(parentPath, Boolean.TRUE);			}			paintHorizontalPartOfLeg(context, g,                                                 paintBounds, insets, bounds,                                                 path, row, isExpanded,						 hasBeenExpanded, isLeaf);		    }		    else if (rootVisible && row == 0) {			paintHorizontalPartOfLeg(context, g,                                                 paintBounds, insets, bounds,                                                 path, row, isExpanded,						 hasBeenExpanded, isLeaf);		    }		    if (shouldPaintExpandControl(path, row, isExpanded,                                                 hasBeenExpanded, isLeaf)) {			paintExpandControl(context, g, paintBounds,                                           insets, bounds, path, row,                                           isExpanded, hasBeenExpanded,isLeaf);		    }		    if ((bounds.y + bounds.height) >= endY) {			done = true;                    }		}		else {		    done = true;		}		row++;	    }	}        cellContext.dispose();	// Empty out the renderer pane, allowing renderers to be gc'ed.	rendererPane.removeAll();    }    private void configureRenderer(SynthContext context) {        TreeCellRenderer renderer = tree.getCellRenderer();        if (renderer instanceof DefaultTreeCellRenderer) {            DefaultTreeCellRenderer r = (DefaultTreeCellRenderer)renderer;            SynthStyle style = context.getStyle();            context.setComponentState(ENABLED | SELECTED);            Color color = r.getTextSelectionColor();            if (color == null || (color instanceof UIResource)) {                r.setTextSelectionColor(style.getColor(                                     context, ColorType.TEXT_FOREGROUND));            }            color = r.getBackgroundSelectionColor();            if (color == null || (color instanceof UIResource)) {                r.setBackgroundSelectionColor(style.getColor(                                        context, ColorType.TEXT_BACKGROUND));            }            context.setComponentState(ENABLED);            color = r.getTextNonSelectionColor();            if (color == null || color instanceof UIResource) {                r.setTextNonSelectionColor(style.getColor(                                        context, ColorType.TEXT_FOREGROUND));            }            color = r.getBackgroundNonSelectionColor();            if (color instanceof UIResource) {                r.setBackgroundNonSelectionColor(style.getColor(                                  context, ColorType.TEXT_BACKGROUND));            }        }    }    /**     * 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(SynthContext context, Graphics g,                                          Rectangle clipBounds, Insets insets,                                          Rectangle bounds, TreePath path,                                          int row, boolean isExpanded,                                          boolean hasBeenExpanded,                                          boolean isLeaf) {        if (!drawHorizontalLegs) {            return;        }        int depth = path.getPathCount() - 1;	if ((depth == 0 || (depth == 1 && !tree.isRootVisible())) &&            !tree.getShowsRootHandles()) {	    return;        }	int clipLeft = clipBounds.x;	int clipRight = clipBounds.x + (clipBounds.width - 1);	int clipBottom = clipBounds.y + (clipBounds.height - 1);	int y = bounds.y + bounds.height / 2;        int x0;        int x1;        if (leftToRight) {            x0 = bounds.x - getIndent() - getTrailingControlOffset();            x1 = bounds.x - trailingLegBufferOffset;        }        else {            x0 = bounds.x + bounds.width + trailingLegBufferOffset;            x1 = bounds.x + bounds.width + getIndent() +                            getTrailingControlOffset();        }        if (y >= clipBounds.y && y <= clipBottom && x1 >= clipLeft &&	                                            x0 <= clipRight ) {            x0 = Math.max(x0, clipLeft);            x1 = Math.min(x1, clipRight);            if (x0 < x1) {                SynthGraphics engine = context.getStyle().                                               getSynthGraphics(context);                g.setColor(context.getStyle().getColor(context,                                                       ColorType.FOREGROUND));                engine.drawLine(context, "Tree.horizontalLine", g, x0, y,                                x1, y);	    }	}    }    /**     * Paints the vertical part of the leg. The receiver should     * NOT modify <code>clipBounds</code>, <code>insets</code>.<p>     */    protected void paintVerticalPartOfLeg(SynthContext context, Graphics g,                                          Rectangle clipBounds, Insets insets,                                          TreePath path) {        if (!drawVerticalLegs) {            return;        }        int depth = path.getPathCount() - 1;	if (depth == 0 && !tree.getShowsRootHandles() &&                          !tree.isRootVisible()) {	    return;        }	int lineX;        int offset = getRowX(-1, depth) - getTrailingControlOffset();	if (leftToRight) {	    lineX = offset + insets.left;	}	else {

⌨️ 快捷键说明

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