javax.swing.tree

Class DefaultTreeCellEditor

Implemented Interfaces:
ActionListener, CellEditor, EventListener, TreeCellEditor, TreeSelectionListener

public class DefaultTreeCellEditor
extends Object
implements ActionListener, TreeCellEditor, TreeSelectionListener

Participates in the tree cell editing.

Nested Class Summary

class
DefaultTreeCellEditor.DefaultTextField
The default text field, used in the editing sessions.
class
DefaultTreeCellEditor.EditorContainer
This container that appears on the tree during editing session.

Field Summary

protected Color
borderSelectionColor
True if the border selection color should be drawn.
protected boolean
canEdit
As of Java 2 platform v1.4 this field should no longer be used.
protected Component
editingComponent
Component used in editing, obtained from the editingContainer.
protected Container
editingContainer
Editing container, will contain the editorComponent.
protected Icon
editingIcon
Icon to use when editing.
protected Font
font
Font to paint with, null indicates font of renderer is to be used.
protected TreePath
lastPath
Last path that was selected.
protected int
lastRow
Row that was last passed into getTreeCellEditorComponent.
protected int
offset
Used in editing.
protected TreeCellEditor
realEditor
Editor handling the editing.
protected DefaultTreeCellRenderer
renderer
Renderer, used to get border and offsets from.
protected Timer
timer
Used before starting the editing session.
protected JTree
tree
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

void
actionPerformed(ActionEvent e)
Messaged when the timer fires.
void
addCellEditorListener(CellEditorListener listener)
Adds a CellEditorListener object to this editor.
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.
void
cancelCellEditing()
Messages cancelCellEditing to the realEditor and removes it from this instance.
protected Container
createContainer()
Creates the container to manage placement of editingComponent.
protected TreeCellEditor
createTreeCellEditor()
This is invoked if a TreeCellEditor is not supplied in the constructor.
protected void
determineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
determineOffset
Color
getBorderSelectionColor()
Returns the color the border is drawn.
CellEditorListener[]
getCellEditorListeners()
Returns all added CellEditorListener objects to this editor.
Object
getCellEditorValue()
Returns the value currently being edited (requests it from the realEditor.
Font
getFont()
Gets the font used for editing.
Component
getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
Configures the editor.
protected boolean
inHitRegion(int x, int y)
Returns true if the passed in location is a valid mouse location to start editing from.
boolean
isCellEditable(EventObject event)
If the realEditor returns true to this message, prepareForEditing is messaged and true is returned.
protected void
prepareForEditing()
Invoked just before editing is to start.
void
removeCellEditorListener(CellEditorListener listener)
Removes a CellEditorListener object.
void
setBorderSelectionColor(Color newColor)
Sets the color to use for the border.
void
setFont(Font font)
Sets the font to edit with. null indicates the renderers font should be used.
protected void
setTree(JTree newTree)
Sets the tree currently editing for.
boolean
shouldSelectCell(EventObject event)
Messages the realEditor for the return value.
protected boolean
shouldStartEditingTimer(EventObject event)
Returns true if event is a MouseEvent and the click count is 1.
protected void
startEditingTimer()
Starts the editing timer (if one installed).
boolean
stopCellEditing()
If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned.
void
valueChanged(TreeSelectionEvent e)
Resets lastPath.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

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 Component editingComponent
Component used in editing, obtained from the editingContainer.

editingContainer

protected Container editingContainer
Editing container, will contain the editorComponent.

editingIcon

protected 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 TreePath lastPath
Last path that was selected.

lastRow

protected int lastRow
Row that was last passed into getTreeCellEditorComponent.

offset

protected 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 Timer timer
Used before starting the editing session.

tree

protected JTree tree
JTree instance listening too.

Constructor Details

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 Details

actionPerformed

public void actionPerformed(ActionEvent e)
Messaged when the timer fires.
Specified by:
actionPerformed in interface ActionListener
Parameters:
e - the event that characterizes the action.

addCellEditorListener

public void addCellEditorListener(CellEditorListener listener)
Adds a CellEditorListener object to this editor.
Specified by:
addCellEditorListener in interface CellEditor
Parameters:
listener - the listener to add

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

cancelCellEditing

public void cancelCellEditing()
Messages cancelCellEditing to the realEditor and removes it from this instance.
Specified by:
cancelCellEditing in interface CellEditor

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 realEditor.
Specified by:
getCellEditorValue in interface CellEditor
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.
Specified by:
getTreeCellEditorComponent in interface TreeCellEditor
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.
Specified by:
isCellEditable in interface CellEditor
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.
Specified by:
removeCellEditorListener in interface CellEditor
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.
Specified by:
shouldSelectCell in interface CellEditor
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.
Specified by:
stopCellEditing in interface CellEditor
Returns:
true if editing was stopped; false otherwise

valueChanged

public void valueChanged(TreeSelectionEvent e)
Resets lastPath.
Specified by:
valueChanged in interface TreeSelectionListener
Parameters:
e - - the event that characterizes the change.

DefaultTreeCellEditor.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.