edu.gwu.jword
Class MyTreeCellEditor

java.lang.Object
  extended byedu.gwu.jword.MyTreeCellEditor
All Implemented Interfaces:
java.awt.event.ActionListener, javax.swing.CellEditor, java.util.EventListener, modelListener, javax.swing.tree.TreeCellEditor, javax.swing.event.TreeSelectionListener

public class MyTreeCellEditor
extends java.lang.Object
implements modelListener, java.awt.event.ActionListener, javax.swing.tree.TreeCellEditor, javax.swing.event.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 XMLEncoder.

See Also:
JTree

Nested Class Summary
 class MyTreeCellEditor.DefaultTextField
          TextField used when no editor is supplied.
 class MyTreeCellEditor.EditorContainer
          Container responsible for placing the editingComponent.
 
Field Summary
protected  java.awt.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  java.awt.Component editingComponent
          Component used in editing, obtained from the editingContainer.
protected  java.awt.Container editingContainer
          Editing container, will contain the editorComponent.
protected  javax.swing.Icon editingIcon
          Icon to use when editing.
protected  java.awt.Font font
          Font to paint with, null indicates font of renderer is to be used.
protected  javax.swing.tree.TreePath lastPath
          Last path that was selected.
protected  int lastRow
          Row that was last passed into getTreeCellEditorComponent.
protected  int offset
          Used in editing.
protected  javax.swing.tree.TreeCellEditor realEditor
          Editor handling the editing.
protected  javax.swing.tree.DefaultTreeCellRenderer renderer
          Renderer, used to get border and offsets from.
protected  javax.swing.Timer timer
          Used before starting the editing session.
protected  javax.swing.JTree tree
          JTree instance listening too.
 
Constructor Summary
MyTreeCellEditor(javax.swing.JTree tree, javax.swing.tree.DefaultTreeCellRenderer renderer)
          Constructs a MyTreeCellEditor object for a JTree using the specified renderer and a default editor.
MyTreeCellEditor(javax.swing.JTree tree, javax.swing.tree.DefaultTreeCellRenderer renderer, javax.swing.tree.TreeCellEditor editor, modelListener oneClickListener, modelListener twoClicksListener)
          Constructs a MyTreeCellEditor object for a JTree using the specified renderer and the specified editor.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Messaged when the timer fires, this will start the editing session.
 void addCellEditorListener(javax.swing.event.CellEditorListener l)
          Adds the CellEditorListener.
 void cancelCellEditing()
          Messages cancelCellEditing to the realEditor and removes it from this instance.
protected  boolean canEditImmediately(java.util.EventObject event)
          Returns true if event is null, or it is a MouseEvent with a click count > 2 and inHitRegion returns true.
protected  java.awt.Container createContainer()
          Creates the container to manage placement of editingComponent.
protected  javax.swing.tree.TreeCellEditor createTreeCellEditor()
          This is invoked if a TreeCellEditor is not supplied in the constructor.
protected  void determineOffset(javax.swing.JTree tree, java.lang.Object value, boolean isSelected, boolean expanded, boolean leaf, int row)
           
 java.awt.Color getBorderSelectionColor()
          Returns the color the border is drawn.
 javax.swing.event.CellEditorListener[] getCellEditorListeners()
          Returns an array of all the CellEditorListeners added to this MyTreeCellEditor with addCellEditorListener().
 java.lang.Object getCellEditorValue()
          Returns the value currently being edited.
 java.awt.Font getFont()
          Gets the font used for editing.
 java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree, java.lang.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(java.util.EventObject event)
          If the realEditor returns true to this message, prepareForEditing is messaged and true is returned.
 void oneClick(ModelEvent e)
           
protected  void prepareForEditing()
          Invoked just before editing is to start.
 void removeCellEditorListener(javax.swing.event.CellEditorListener l)
          Removes the previously added CellEditorListener.
 void setBorderSelectionColor(java.awt.Color newColor)
          Sets the color to use for the border.
 void setFont(java.awt.Font font)
          Sets the font to edit with.
 void setOneClickListener(modelListener l)
           
protected  void setTree(javax.swing.JTree newTree)
          Sets the tree currently editing for.
 void setTwoClicksListener(modelListener l)
           
 boolean shouldSelectCell(java.util.EventObject event)
          Messages the realEditor for the return value.
protected  boolean shouldStartEditingTimer(java.util.EventObject event)
          Returns true if event is a MouseEvent and the click count is 1.
protected  void startEditingTimer()
          Starts the editing timer.
 boolean stopCellEditing()
          If the realEditor will allow editing to stop, the realEditor is removed and true is returned, otherwise false is returned.
 void twoClicks(ModelEvent e)
           
 void valueChanged(javax.swing.event.TreeSelectionEvent e)
          Resets lastPath.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

realEditor

protected javax.swing.tree.TreeCellEditor realEditor
Editor handling the editing.


renderer

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


editingContainer

protected java.awt.Container editingContainer
Editing container, will contain the editorComponent.


editingComponent

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


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.


offset

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


tree

protected transient javax.swing.JTree tree
JTree instance listening too.


lastPath

protected transient javax.swing.tree.TreePath lastPath
Last path that was selected.


timer

protected transient javax.swing.Timer timer
Used before starting the editing session.


lastRow

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


borderSelectionColor

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


editingIcon

protected transient javax.swing.Icon editingIcon
Icon to use when editing.


font

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

Constructor Detail

MyTreeCellEditor

public MyTreeCellEditor(javax.swing.JTree tree,
                        javax.swing.tree.DefaultTreeCellRenderer renderer)
Constructs a MyTreeCellEditor 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

MyTreeCellEditor

public MyTreeCellEditor(javax.swing.JTree tree,
                        javax.swing.tree.DefaultTreeCellRenderer renderer,
                        javax.swing.tree.TreeCellEditor editor,
                        modelListener oneClickListener,
                        modelListener twoClicksListener)
Constructs a MyTreeCellEditor 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

setBorderSelectionColor

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

Parameters:
newColor - the new border color

getBorderSelectionColor

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


setFont

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

Parameters:
font - the editing Font
See Also:
getFont()

getFont

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

Returns:
the editing Font
See Also:
setFont(java.awt.Font)

getTreeCellEditorComponent

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

Specified by:
getTreeCellEditorComponent in interface javax.swing.tree.TreeCellEditor

getCellEditorValue

public java.lang.Object getCellEditorValue()
Returns the value currently being edited.

Specified by:
getCellEditorValue in interface javax.swing.CellEditor
Returns:
the value currently being edited

isCellEditable

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

Specified by:
isCellEditable in interface javax.swing.CellEditor

shouldSelectCell

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

Specified by:
shouldSelectCell in interface javax.swing.CellEditor

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 javax.swing.CellEditor

cancelCellEditing

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

Specified by:
cancelCellEditing in interface javax.swing.CellEditor

addCellEditorListener

public void addCellEditorListener(javax.swing.event.CellEditorListener l)
Adds the CellEditorListener.

Specified by:
addCellEditorListener in interface javax.swing.CellEditor
Parameters:
l - the listener to be added

removeCellEditorListener

public void removeCellEditorListener(javax.swing.event.CellEditorListener l)
Removes the previously added CellEditorListener.

Specified by:
removeCellEditorListener in interface javax.swing.CellEditor

getCellEditorListeners

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

Returns:
all of the CellEditorListeners added or an empty array if no listeners have been added
Since:
1.4

valueChanged

public void valueChanged(javax.swing.event.TreeSelectionEvent e)
Resets lastPath.

Specified by:
valueChanged in interface javax.swing.event.TreeSelectionListener

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Messaged when the timer fires, this will start the editing session.

Specified by:
actionPerformed in interface java.awt.event.ActionListener

setTree

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

Parameters:
newTree - the new tree to be edited

shouldStartEditingTimer

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

Parameters:
event - the event being studied

startEditingTimer

protected void startEditingTimer()
Starts the editing timer.


canEditImmediately

protected boolean canEditImmediately(java.util.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

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 <= 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

determineOffset

protected void determineOffset(javax.swing.JTree tree,
                               java.lang.Object value,
                               boolean isSelected,
                               boolean expanded,
                               boolean leaf,
                               int row)

prepareForEditing

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


createContainer

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


createTreeCellEditor

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

Returns:
a new TextField editor

oneClick

public void oneClick(ModelEvent e)
Specified by:
oneClick in interface modelListener

twoClicks

public void twoClicks(ModelEvent e)
Specified by:
twoClicks in interface modelListener

setOneClickListener

public void setOneClickListener(modelListener l)

setTwoClicksListener

public void setTwoClicksListener(modelListener l)