|
|
The DefaultTreeSelectionModel class in the swing.tree package implements the TreeSelectionModel interface and is used by instances of the JTree class for selection management unless the selection model is explicitly set to a different class. The class contains one bound property, the SELECTION_MODE_PROPERTY. (The SELECTION_MODE_PROPERTY constant is defined in the DefaultTreeSelectionModel class.) This property change event is fired when the selection mode changes in the model.
Rendering and Editing
You saw in Chapter 10, "JList, JComboBox, and Bound Controls," how the JList class delegates drawing of its elements to a rendering object. The JTree class employs the same strategy. The TreeCellRenderer interface defines the methods that must be implemented by a renderer that can be used with a JTree instance. This interface contains a single method, getTreeCellRendererComponent. When called, the implementing class must return a Component configured properly for rendering the value passed to the method. Listing 11.6 shows the TreeCellRenderer interface.
Listing 11.6 The TreeCellRenderer Interface
public interface TreeCellRenderer {
/**
* Sets the value of the current tree cell to <code>value</code>.
* If <code>selected</code> is true, the cell will be drawn as if
* selected. If <code>expanded</code> is true the node is currently
* expanded and if <code>leaf</code> is true the node represents a
* leaf and if <code>hasFocus</code> is true the node currently has
* focus. <code>tree</code> is the JTree the receiver is being
* configured for.
* Returns the Component that the renderer uses to draw the value.
*
* @return Component that the renderer uses to draw the value.
*/
Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus);
}
The JTree instance also allows editing in place. The gesture that begins editing is look-and-feel specific, but a single click will start editing for the standard look-and-feel implementations contained in the JFC. The strategy for editing is analogous to that used for rendering. One cell editor is used for the entire tree. It is moved to the proper location and sized to the required size when editing begins. The editor manages the editing process. When the editing is complete, the data in the editor is copied to the data model.
The TreeCellEditor interface defines a single method: getTreeCellEditorComponent. However, the interface extends the CellEditor interface. This interface is contained in the java.awt.swing package. The CellEditor interface defines the methods used to mediate the editing and data transfer between the model and the editor. You will see this interface again in the next Chapter 12, "Table Component." The interface that defines the editor for the table component also extends the CellEditor interface. Listing 11.7 shows the CellEditor and TreeCellEditor interfaces.
Listing 11.7 The CellEditor and TreeCellEditor Interfaces
public interface CellEditor {
/** Returns the value contained in the editor**/
public Object getCellEditorValue();
/**
* Ask the editor if it can start editing using <I>anEvent</I>.
* <I>anEvent</I> is in the invoking component coordinate system.
* The editor cannot assume the Component returned by
* getCellEditorComponent() is installed. This method is intended
* for the use of client to avoid the cost of setting up and
* installing the editor component if editing is not possible.
* If editing can be started this method returns true.
*
* @param anEvent the event the editor should use to consider
* whether to begin editing or not.
* @return true if editing can be started.
* @see #shouldSelectCell()
*/
public boolean isCellEditable(EventObject anEvent);
/**
* Tell the editor to start editing using <I>anEvent</I>. It is
* up to the editor if it wants to start editing in different states
* depending on the exact type of <I>anEvent</I>. For example, with
* a text field editor, if the event is a mouse event the editor
* might start editing with the cursor at the clicked point. If
* the event is a keyboard event, it might want to replace the value
* of the text field with that first key, etc. <I>anEvent</I>
* is in the invoking components coordinate system. A null value
* is a valid parameter for <I>anEvent</I>, and it is up to the
* editor to determine what is the default starting state. For
* example, a text field editor might want to select all the text and
* start editing if <I>anEvent</I> is null. The editor can assume
* the Component returned by getCellEditorComponent() is properly
* installed in the clients Component hierarchy before this method is
* called. <p>
*
* The return value of shouldSelectCell() is a boolean indicating
* whether the editing cell should be selected or not. Typically,
* the return value is true, because is most cases the editing cell
* should be selected. However, it is useful to return false to keep
* the selection from changing for some types of edits. eg. A table
* that contains a column of check boxes, the user might want
* to change those check boxes without altering the selection.
* (See Netscape Communicator for just such an example.) Of course,
* it is up to the client of the editor to use the return value, but
* it doesnt need to if it doesnt want to.
*
* @param anEvent the event the editor should use to start
* editing.
* @return true if the editor would like the editing cell to be
* selected
* @see #isCellEditable()
*/
public boolean shouldSelectCell(EventObject anEvent);
/**
* Tell the editor to stop editing and accept any partially edited
* value as the value of the editor. The editor returns false if
* editing was not stopped, useful for editors that validate and
* cannot accept invalid entries.
*
* @return true if editing was stopped
*/
public boolean stopCellEditing();
/**
* Tell the editor to cancel editing and not accept any partially
* edited value.
*/
public void cancelCellEditing();
/**
* Add a listener to the list thats notified when the editor starts,
* stops, or cancels editing.
*
* @param l the CellEditorListener
*/
public void addCellEditorListener(CellEditorListener l);
/**
* Remove a listener from the list thats notified
*
* @param l the CellEditorListener
*/
public void removeCellEditorListener(CellEditorListener l);
}
public interface TreeCellEditor extends CellEditor
{
/**
* Sets an initial <I>value</I> for the editor. This will cause
* the editor to stop editing and lose any partially edited value
* if the editor is editing when this method is called. <p>
*
* Returns the component that should be added to the clients
* Component hierarchy. Once installed in the clients hierarchy
* this component will then be able to draw and receive user input.
*
* @param table the JTree that is asking the editor to edit
* This parameter can be null.
* @param value the value of the cell to be edited.
* @param isSelected true is the cell to be rendered with
* selection highlighting
* @param expanded true if the node is expanded
* @param leaf true if the node is a leaf node
* @param row the row index of the node being edited
* @return the component for editing
*/
Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, booleanexpanded,
boolean leaf, int row);
}
|