javax.swing.tree

Class DefaultTreeCellEditor

public class DefaultTreeCellEditor extends Object implements ActionListener, TreeCellEditor, TreeSelectionListener

Participates in the tree cell editing.
Nested Class Summary
classDefaultTreeCellEditor.DefaultTextField
The default text field, used in the editing sessions.
classDefaultTreeCellEditor.EditorContainer
This container that appears on the tree during editing session.
Field Summary
protected ColorborderSelectionColor
True if the border selection color should be drawn.
protected booleancanEdit
As of Java 2 platform v1.4 this field should no longer be used.
protected ComponenteditingComponent
Component used in editing, obtained from the editingContainer.
protected ContainereditingContainer
Editing container, will contain the editorComponent.
protected IconeditingIcon
Icon to use when editing.
protected Fontfont
Font to paint with, null indicates font of renderer is to be used.
protected TreePathlastPath
Last path that was selected.
protected intlastRow
Row that was last passed into getTreeCellEditorComponent.
protected intoffset
Used in editing.
protected TreeCellEditorrealEditor
Editor handling the editing.
protected DefaultTreeCellRendererrenderer
Renderer, used to get border and offsets from.
protected Timertimer
Used before starting the editing session.
protected JTreetree
JTree instance listening too.
Constructor Summary
DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer)
Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and a default editor.
DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor)
Constructs a DefaultTreeCellEditor object for a JTree using the specified renderer and the specified editor.
Method Summary
voidactionPerformed(ActionEvent e)
Messaged when the timer fires.
voidaddCellEditorListener(CellEditorListener listener)
Adds a CellEditorListener object to this editor.
voidcancelCellEditing()
Messages cancelCellEditing to the realEditor and removes it from this instance.
protected booleancanEditImmediately(EventObject event)
Returns true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true.
protected ContainercreateContainer()
Creates the container to manage placement of editingComponent.
protected TreeCellEditorcreateTreeCellEditor()
This is invoked if a TreeCellEditor is not supplied in the constructor.
protected voiddetermineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
determineOffset
ColorgetBorderSelectionColor()
Returns the color the border is drawn.
CellEditorListener[]getCellEditorListeners()
Returns all added CellEditorListener objects to this editor.
ObjectgetCellEditorValue()
Returns the value currently being edited (requests it from the {@link #realEditor}.
FontgetFont()
Gets the font used for editing.
ComponentgetTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Configures the editor.
protected booleaninHitRegion(int x, int y)
Returns true if the passed in location is a valid mouse location to start editing from.
booleanisCellEditable(EventObject event)
If the realEditor returns true to this message, prepareForEditing is messaged and true is returned.
protected voidprepareForEditing()
Invoked just before editing is to start.
voidremoveCellEditorListener(CellEditorListener listener)
Removes a CellEditorListener object.
voidsetBorderSelectionColor(Color newColor)
Sets the color to use for the border.
voidsetFont(Font font)
Sets the font to edit with. null indicates the renderers font should be used.
protected voidsetTree(JTree newTree)
Sets the tree currently editing for.
booleanshouldSelectCell(EventObject event)
Messages the realEditor for the return value.
protected booleanshouldStartEditingTimer(EventObject event)
Returns true if event is a MouseEvent and the click count is 1.
protected voidstartEditingTimer()
Starts the editing timer (if one installed).
booleanstopCellEditing()
If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned.
voidvalueChanged(TreeSelectionEvent e)
Resets lastPath.

Field Detail

borderSelectionColor

protected Color borderSelectionColor
True if the border selection color should be drawn.

canEdit

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.

editingComponent

protected transient Component editingComponent
Component used in editing, obtained from the editingContainer.

editingContainer

protected Container editingContainer
Editing container, will contain the editorComponent.

editingIcon

protected transient Icon editingIcon
Icon to use when editing.

font

protected Font font
Font to paint with, null indicates font of renderer is to be used.

lastPath

protected transient TreePath lastPath
Last path that was selected.

lastRow

protected transient int lastRow
Row that was last passed into getTreeCellEditorComponent.

offset

protected transient int offset
Used in editing. Indicates x position to place editingComponent.

realEditor

protected TreeCellEditor realEditor
Editor handling the editing.

renderer

protected DefaultTreeCellRenderer renderer
Renderer, used to get border and offsets from.

timer

protected transient Timer timer
Used before starting the editing session.

tree

protected transient JTree tree
JTree instance listening too.

Constructor Detail

DefaultTreeCellEditor

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.)

Parameters: tree - a JTree object renderer - a DefaultTreeCellRenderer object

DefaultTreeCellEditor

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.)

Parameters: tree - a JTree object renderer - a DefaultTreeCellRenderer object editor - a TreeCellEditor object

Method Detail

actionPerformed

public void actionPerformed(ActionEvent e)
Messaged when the timer fires.

Parameters: e the event that characterizes the action.

addCellEditorListener

public void addCellEditorListener(CellEditorListener listener)
Adds a CellEditorListener object to this editor.

Parameters: listener the listener to add

cancelCellEditing

public void cancelCellEditing()
Messages cancelCellEditing to the realEditor and removes it from this instance.

canEditImmediately

protected boolean canEditImmediately(EventObject event)
Returns true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true.

Parameters: event - the event being studied

Returns: true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true

createContainer

protected Container createContainer()
Creates the container to manage placement of editingComponent.

Returns: the container to manage the placement of the editingComponent.

createTreeCellEditor

protected TreeCellEditor createTreeCellEditor()
This is invoked if a TreeCellEditor is not supplied in the constructor. It returns a TextField editor.

Returns: a new TextField editor

determineOffset

protected void determineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
determineOffset

Parameters: tree - value - isSelected - expanded - leaf - row -

getBorderSelectionColor

public Color getBorderSelectionColor()
Returns the color the border is drawn.

Returns: Color

getCellEditorListeners

public CellEditorListener[] getCellEditorListeners()
Returns all added CellEditorListener objects to this editor.

Returns: an array of listeners

Since: 1.4

getCellEditorValue

public Object getCellEditorValue()
Returns the value currently being edited (requests it from the {@link #realEditor}.

Returns: the value currently being edited

getFont

public Font getFont()
Gets the font used for editing.

Returns: the editing font

getTreeCellEditorComponent

public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Configures the editor. Passed onto the realEditor. Sets an initial value for the editor. This will cause the editor to stopEditing and lose any partially edited value if the editor is editing when this method is called. Returns the component that should be added to the client's Component hierarchy. Once installed in the client's hierarchy this component will then be able to draw and receive user input.

Parameters: tree - the JTree that is asking the editor to edit; this parameter can be null value - the value of the cell to be edited isSelected - true is the cell is to be rendered with selection highlighting expanded - true if the node is expanded leaf - true if the node is a leaf node row - the row index of the node being edited

Returns: the component for editing

inHitRegion

protected boolean inHitRegion(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 less than or equal to 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.

Parameters: x - the x-coordinate of the point y - the y-coordinate of the point

Returns: true if the passed in location is a valid mouse location

isCellEditable

public boolean isCellEditable(EventObject event)
If the realEditor returns true to this message, prepareForEditing is messaged and true is returned.

Parameters: event - the event the editor should use to consider whether to begin editing or not

Returns: true if editing can be started

prepareForEditing

protected void prepareForEditing()
Invoked just before editing is to start. Will add the editingComponent to the editingContainer.

removeCellEditorListener

public void removeCellEditorListener(CellEditorListener listener)
Removes a CellEditorListener object.

Parameters: listener the listener to remove

setBorderSelectionColor

public void setBorderSelectionColor(Color newColor)
Sets the color to use for the border.

Parameters: newColor - the new border color

setFont

public void setFont(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.

Parameters: font - the editing Font

setTree

protected void setTree(JTree newTree)
Sets the tree currently editing for. This is needed to add a selection listener.

Parameters: newTree - the new tree to be edited

shouldSelectCell

public boolean shouldSelectCell(EventObject event)
Messages the realEditor for the return value.

Parameters: event - the event the editor should use to start editing

Returns: true if the editor would like the editing cell to be selected; otherwise returns false

shouldStartEditingTimer

protected boolean shouldStartEditingTimer(EventObject event)
Returns true if event is a MouseEvent and the click count is 1.

Parameters: event - the event being studied

Returns: true if editing should start

startEditingTimer

protected void startEditingTimer()
Starts the editing timer (if one installed).

stopCellEditing

public boolean stopCellEditing()
If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned.

Returns: true if editing was stopped; false otherwise

valueChanged

public void valueChanged(TreeSelectionEvent e)
Resets lastPath.

Parameters: e - the event that characterizes the change.