FileDocCategorySizeDatePackage
DefaultTreeCellEditor.javaAPI DocJava SE 5 API21624Fri Aug 26 14:58:20 BST 2005javax.swing.tree

DefaultTreeCellEditor

public class DefaultTreeCellEditor extends Object implements ActionListener, TreeCellEditor, TreeSelectionListener
A TreeCellEditor. You need to supply an instance of DefaultTreeCellRenderer so that the icons can be obtained. You can optionally supply a TreeCellEditor that will be layed out according to the icon in the DefaultTreeCellRenderer. If you do not supply a TreeCellEditor, a TextField will be used. Editing is started on a triple mouse click, or after a click, pause, click and a delay of 1200 miliseconds.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

see
javax.swing.JTree
version
1.30 12/19/03
author
Scott Violet

Fields Summary
protected TreeCellEditor
realEditor
Editor handling the editing.
protected DefaultTreeCellRenderer
renderer
Renderer, used to get border and offsets from.
protected Container
editingContainer
Editing container, will contain the editorComponent.
protected transient Component
editingComponent
Component used in editing, obtained from the editingContainer.
protected boolean
canEdit
As of Java 2 platform v1.4 this field should no longer be used. If you wish to provide similar behavior you should directly override isCellEditable.
protected transient int
offset
Used in editing. Indicates x position to place editingComponent.
protected transient JTree
tree
JTree instance listening too.
protected transient TreePath
lastPath
Last path that was selected.
protected transient Timer
timer
Used before starting the editing session.
protected transient int
lastRow
Row that was last passed into getTreeCellEditorComponent.
protected Color
borderSelectionColor
True if the border selection color should be drawn.
protected transient Icon
editingIcon
Icon to use when editing.
protected Font
font
Font to paint with, null indicates font of renderer is to be used.
Constructors Summary
public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer)
Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and a default editor. (Use this constructor for normal editing.)

param
tree a JTree object
param
renderer a DefaultTreeCellRenderer object

	this(tree, renderer, null);
    
public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor)
Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and the specified editor. (Use this constructor for specialized editing.)

param
tree a JTree object
param
renderer a DefaultTreeCellRenderer object
param
editor a TreeCellEditor object

	this.renderer = renderer;
	realEditor = editor;
	if(realEditor == null)
	    realEditor = createTreeCellEditor();
	editingContainer = createContainer();
	setTree(tree);
	setBorderSelectionColor(UIManager.getColor
				("Tree.editorBorderSelectionColor"));
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)
Messaged when the timer fires, this will start the editing session.

	if(tree != null && lastPath != null) {
	    tree.startEditingAtPath(lastPath);
	}
    
public voidaddCellEditorListener(javax.swing.event.CellEditorListener l)
Adds the CellEditorListener.

param
l the listener to be added

	realEditor.addCellEditorListener(l);
    
protected booleancanEditImmediately(java.util.EventObject event)
Returns true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true.

param
event the event being studied

	if((event instanceof MouseEvent) &&
	   SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
	    MouseEvent       me = (MouseEvent)event;

	    return ((me.getClickCount() > 2) &&
		    inHitRegion(me.getX(), me.getY()));
	}
 	return (event == null);
    
public voidcancelCellEditing()
Messages cancelCellEditing to the realEditor and removes it from this instance.

	realEditor.cancelCellEditing();
        cleanupAfterEditing();
    
private voidcleanupAfterEditing()
Cleans up any state after editing has completed. Removes the editingComponent the editingContainer.

	if (editingComponent != null) {
	    editingContainer.remove(editingComponent);
        }
	editingComponent = null;
    
protected java.awt.ContainercreateContainer()
Creates the container to manage placement of editingComponent.

	return new EditorContainer();
    
protected javax.swing.tree.TreeCellEditorcreateTreeCellEditor()
This is invoked if a TreeCellEditor is not supplied in the constructor. It returns a TextField editor.

return
a new TextField editor

	Border              aBorder = UIManager.getBorder("Tree.editorBorder");
	DefaultCellEditor   editor = new DefaultCellEditor
	    (new DefaultTextField(aBorder)) {
	    public boolean shouldSelectCell(EventObject event) {
		boolean retValue = super.shouldSelectCell(event);
		return retValue;
	    }
	};

	// One click to edit.
	editor.setClickCountToStart(1);
	return editor;
    
protected voiddetermineOffset(javax.swing.JTree tree, java.lang.Object value, boolean isSelected, boolean expanded, boolean leaf, int row)

	if(renderer != null) {
	    if(leaf)
		editingIcon = renderer.getLeafIcon();
	    else if(expanded)
		editingIcon = renderer.getOpenIcon();
	    else
		editingIcon = renderer.getClosedIcon();
	    if(editingIcon != null)
		offset = renderer.getIconTextGap() +
		         editingIcon.getIconWidth();
	    else
		offset = renderer.getIconTextGap();
	}
	else {
	    editingIcon = null;
	    offset = 0;
	}
    
public java.awt.ColorgetBorderSelectionColor()
Returns the color the border is drawn.

return
the border selection color

	return borderSelectionColor;
    
public javax.swing.event.CellEditorListener[]getCellEditorListeners()
Returns an array of all the CellEditorListeners added to this DefaultTreeCellEditor with addCellEditorListener().

return
all of the CellEditorListeners added or an empty array if no listeners have been added
since
1.4

        return ((DefaultCellEditor)realEditor).getCellEditorListeners();
    
public java.lang.ObjectgetCellEditorValue()
Returns the value currently being edited.

return
the value currently being edited

	return realEditor.getCellEditorValue();
    
public java.awt.FontgetFont()
Gets the font used for editing.

return
the editing Font
see
#setFont

	return font;
    
public java.awt.ComponentgetTreeCellEditorComponent(javax.swing.JTree tree, java.lang.Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Configures the editor. Passed onto the realEditor.

	setTree(tree);
	lastRow = row;
	determineOffset(tree, value, isSelected, expanded, leaf, row);

        if (editingComponent != null) {
            editingContainer.remove(editingComponent);
        }
	editingComponent = realEditor.getTreeCellEditorComponent(tree, value,
					isSelected, expanded,leaf, row);


        // this is kept for backwards compatability but isn't really needed
        // with the current BasicTreeUI implementation.
	TreePath        newPath = tree.getPathForRow(row);

	canEdit = (lastPath != null && newPath != null &&
		   lastPath.equals(newPath));

	Font            font = getFont();

	if(font == null) {
	    if(renderer != null)
		font = renderer.getFont();
	    if(font == null)
		font = tree.getFont();
	}
	editingContainer.setFont(font);
        prepareForEditing();
	return editingContainer;
    
protected booleaninHitRegion(int x, int y)
Returns true if the passed in location is a valid mouse location to start editing from. This is implemented to return false if x is <= the width of the icon and icon gap displayed by the renderer. In other words this returns true if the user clicks over the text part displayed by the renderer, and false otherwise.

param
x the x-coordinate of the point
param
y the y-coordinate of the point
return
true if the passed in location is a valid mouse location

	if(lastRow != -1 && tree != null) {
	    Rectangle bounds = tree.getRowBounds(lastRow);
	    ComponentOrientation treeOrientation = tree.getComponentOrientation();
	    
	    if ( treeOrientation.isLeftToRight() ) {
		if (bounds != null && x <= (bounds.x + offset) &&
		    offset < (bounds.width - 5)) {
		    return false;
		}
	    } else if ( bounds != null &&
			( x >= (bounds.x+bounds.width-offset+5) ||
			  x <= (bounds.x + 5) ) &&
			offset < (bounds.width - 5) ) {
		return false;
	    }
	}
	return true;
    
public booleanisCellEditable(java.util.EventObject event)
If the realEditor returns true to this message, prepareForEditing is messaged and true is returned.

	boolean            retValue = false;
        boolean            editable = false;

        if (event != null) {
            if (event.getSource() instanceof JTree) {
                setTree((JTree)event.getSource());
                if (event instanceof MouseEvent) {
                    TreePath path = tree.getPathForLocation(
                                         ((MouseEvent)event).getX(),
                                         ((MouseEvent)event).getY());
                    editable = (lastPath != null && path != null &&
                               lastPath.equals(path));
		    if (path!=null) {
			lastRow = tree.getRowForPath(path);
			Object value = path.getLastPathComponent();
			boolean isSelected = tree.isRowSelected(lastRow);
			boolean expanded = tree.isExpanded(path);
			TreeModel treeModel = tree.getModel();
			boolean leaf = treeModel.isLeaf(value);
			determineOffset(tree, value, isSelected,
					expanded, leaf, lastRow);
		    }
                }
            }
        }
	if(!realEditor.isCellEditable(event))
	    return false;
	if(canEditImmediately(event))
	    retValue = true;
	else if(editable && shouldStartEditingTimer(event)) {
	    startEditingTimer();
	}
	else if(timer != null && timer.isRunning())
	    timer.stop();
	if(retValue)
	    prepareForEditing();
	return retValue;
    
protected voidprepareForEditing()
Invoked just before editing is to start. Will add the editingComponent to the editingContainer.

        if (editingComponent != null) {
            editingContainer.add(editingComponent);
        }
    
private voidreadObject(java.io.ObjectInputStream s)

	s.defaultReadObject();

	Vector          values = (Vector)s.readObject();
	int             indexCounter = 0;
	int             maxCounter = values.size();

	if(indexCounter < maxCounter && values.elementAt(indexCounter).
	   equals("realEditor")) {
	    realEditor = (TreeCellEditor)values.elementAt(++indexCounter);
	    indexCounter++;
	}
    
public voidremoveCellEditorListener(javax.swing.event.CellEditorListener l)
Removes the previously added CellEditorListener.

param
l the listener to be removed

	realEditor.removeCellEditorListener(l);
    
public voidsetBorderSelectionColor(java.awt.Color newColor)
Sets the color to use for the border.

param
newColor the new border color

	borderSelectionColor = newColor;
    
public voidsetFont(java.awt.Font font)
Sets the font to edit with. null indicates the renderers font should be used. This will NOT override any font you have set in the editor the receiver was instantied with. If null for an editor was passed in a default editor will be created that will pick up this font.

param
font the editing Font
see
#getFont

	this.font = font;
    
protected voidsetTree(javax.swing.JTree newTree)
Sets the tree currently editing for. This is needed to add a selection listener.

param
newTree the new tree to be edited

	if(tree != newTree) {
	    if(tree != null)
		tree.removeTreeSelectionListener(this);
	    tree = newTree;
	    if(tree != null)
		tree.addTreeSelectionListener(this);
	    if(timer != null) {
		timer.stop();
	    }
	}
    
public booleanshouldSelectCell(java.util.EventObject event)
Messages the realEditor for the return value.

	return realEditor.shouldSelectCell(event);
    
protected booleanshouldStartEditingTimer(java.util.EventObject event)
Returns true if event is a MouseEvent and the click count is 1.

param
event the event being studied

	if((event instanceof MouseEvent) &&
	    SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
	    MouseEvent        me = (MouseEvent)event;

	    return (me.getClickCount() == 1 &&
		    inHitRegion(me.getX(), me.getY()));
	}
	return false;
    
protected voidstartEditingTimer()
Starts the editing timer.

	if(timer == null) {
	    timer = new Timer(1200, this);
	    timer.setRepeats(false);
	}
	timer.start();
    
public booleanstopCellEditing()
If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned.

	if(realEditor.stopCellEditing()) {
            cleanupAfterEditing();
	    return true;
	}
	return false;
    
public voidvalueChanged(javax.swing.event.TreeSelectionEvent e)
Resets lastPath.

	if(tree != null) {
	    if(tree.getSelectionCount() == 1)
		lastPath = tree.getSelectionPath();
	    else
		lastPath = null;
	}
	if(timer != null) {
	    timer.stop();
	}
    
private voidwriteObject(java.io.ObjectOutputStream s)

	Vector      values = new Vector();

	s.defaultWriteObject();
	// Save the realEditor, if its Serializable.
	if(realEditor != null && realEditor instanceof Serializable) {
	    values.addElement("realEditor");
	    values.addElement(realEditor);
	}
	s.writeObject(values);