| Method from javax.swing.tree.DefaultTreeCellEditor Detail: |
public void actionPerformed(ActionEvent e) {
if(tree != null && lastPath != null) {
tree.startEditingAtPath(lastPath);
}
}
Messaged when the timer fires, this will start the editing
session. |
public void addCellEditorListener(CellEditorListener l) {
realEditor.addCellEditorListener(l);
}
Adds the CellEditorListener. |
protected boolean canEditImmediately(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return ((me.getClickCount() > 2) &&
inHitRegion(me.getX(), me.getY()));
}
return (event == null);
}
Returns true if event is null,
or it is a MouseEvent with a click count > 2
and inHitRegion returns true. |
public void cancelCellEditing() {
realEditor.cancelCellEditing();
cleanupAfterEditing();
}
Messages cancelCellEditing to the
realEditor and removes it from this instance. |
protected Container createContainer() {
return new EditorContainer();
}
Creates the container to manage placement of
editingComponent. |
protected TreeCellEditor createTreeCellEditor() {
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;
}
This is invoked if a TreeCellEditor
is not supplied in the constructor.
It returns a TextField editor. |
protected void determineOffset(JTree tree,
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 Color getBorderSelectionColor() {
return borderSelectionColor;
}
Returns the color the border is drawn. |
public CellEditorListener[] getCellEditorListeners() {
return ((DefaultCellEditor)realEditor).getCellEditorListeners();
}
Returns an array of all the CellEditorListeners added
to this DefaultTreeCellEditor with addCellEditorListener(). |
public Object getCellEditorValue() {
return realEditor.getCellEditorValue();
}
Returns the value currently being edited. |
public Font getFont() {
return font;
}
Gets the font used for editing. |
public Component getTreeCellEditorComponent(JTree tree,
Object value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row) {
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;
}
Configures the editor. Passed onto the realEditor. |
protected boolean inHitRegion(int x,
int y) {
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;
}
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. |
public boolean isCellEditable(EventObject event) {
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;
}
If the realEditor returns true to this
message, prepareForEditing
is messaged and true is returned. |
protected void prepareForEditing() {
if (editingComponent != null) {
editingContainer.add(editingComponent);
}
}
Invoked just before editing is to start. Will add the
editingComponent to the
editingContainer. |
public void removeCellEditorListener(CellEditorListener l) {
realEditor.removeCellEditorListener(l);
}
Removes the previously added CellEditorListener. |
public void setBorderSelectionColor(Color newColor) {
borderSelectionColor = newColor;
}
Sets the color to use for the border. |
public void setFont(Font font) {
this.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. |
protected void setTree(JTree newTree) {
if(tree != newTree) {
if(tree != null)
tree.removeTreeSelectionListener(this);
tree = newTree;
if(tree != null)
tree.addTreeSelectionListener(this);
if(timer != null) {
timer.stop();
}
}
}
Sets the tree currently editing for. This is needed to add
a selection listener. |
public boolean shouldSelectCell(EventObject event) {
return realEditor.shouldSelectCell(event);
}
Messages the realEditor for the return value. |
protected boolean shouldStartEditingTimer(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return (me.getClickCount() == 1 &&
inHitRegion(me.getX(), me.getY()));
}
return false;
}
Returns true if event is a MouseEvent
and the click count is 1. |
protected void startEditingTimer() {
if(timer == null) {
timer = new Timer(1200, this);
timer.setRepeats(false);
}
timer.start();
}
Starts the editing timer. |
public boolean stopCellEditing() {
if(realEditor.stopCellEditing()) {
cleanupAfterEditing();
return true;
}
return false;
}
If the realEditor will allow editing to stop,
the realEditor is removed and true is returned,
otherwise false is returned. |
public void valueChanged(TreeSelectionEvent e) {
if(tree != null) {
if(tree.getSelectionCount() == 1)
lastPath = tree.getSelectionPath();
else
lastPath = null;
}
if(timer != null) {
timer.stop();
}
}
|