Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for Oracle 8i Internet Seminars.
Click here for Oracle 8i Internet Seminars.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Tree Paths

A path in the tree data structure defines a linear path from the root of the tree to a node. The node may or may not be a leaf in the tree. Perhaps the easiest way to think of a path is to start at a node and keep following its parent, its parent’s parent, and so on until the root of the tree is reached. Reversing this lineage defines a tree path.

The swing.tree package contains the TreePath class that defines the path from the root of the tree to a node in the tree. The TreePath class’s constructor takes an array of node objects that define the path. The class does not validate the path passed to the method. You will see the TreePath class used in JTree methods for expanding the tree. Paths are used extensively in the tree’s selection model, which is the subject of the next section.

Tree Selection Model

As you have seen earlier with the ListModel interface and JList class, the TreeModel interface and JTree class delegate selection management to a separate selection model, the TreeSelectionModel. The TreeSelectionModel is an interface that defines the selection model for a JFC tree. Looking at the interface that is shown in Listing 11.5, you see that it contains 27 method signatures, making it one of the biggest interfaces in the JFC.

The TreeSelectionModel defines three selection modes for a tree. The SINGLE_TREE_SELECTION mode allows only one path to be selected at a given time. The CONTIGUOUS_TREE_SELECTION mode allows multiple contiguous paths to be simultaneously selected. The DISCONTIGUOUS_TREE_SELECTION mode allows any paths to be simultaneously selected. The setSelectionMode and getSelectionMode methods are used to alter and query the selection mode property. The remainder of the methods contained in the interface are used to alter the selected path set. The semantics of each selection method are not defined for each selection mode. For example, the setSelectionPaths method takes an array of TreePath instances as its lone parameter. If the selection mode is single selection and this array contains more than one path, which path is selected is not defined in the TreeSelectionModel interface. It is up to the class that implements the interface to decide the behavior. The DefaultTreeSelectionModel class that is provided in the JFC only selects the first path in this case. However, an application should not assume this behavior from other classes that implement the TreeSelectionModel interface. For instance, another implementation can select the last path in the array.

Listing 11.5 The TreeSelectionModel Interface

public interface TreeSelectionModel
{
    /** Selection can only contain one path at a time. */
    public static int               SINGLE_TREE_SELECTION = 1;

    /** Selection can only be contiguous. This will only be enforced if
     * a RowMapper instance is provided. */
    public static int               CONTIGUOUS_TREE_SELECTION = 2;

/** Selection can contain any number of items that are not
     * necessarily contiguous. */
    public static int               DISCONTIGUOUS_TREE_SELECTION = 4;

    /**
* Sets the selection model, which must be one of
     * SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or
     * DISCONTIGUOUS_TREE_SELECTION.
     */
    void setSelectionMode(int mode);

    /**
     * Returns the selection mode.
     */
    int getSelectionMode();

    /**
      * Sets the selection to path.  If this represents a change, then
      * the TreeSelectionListeners are notified.
      *
      * @param path new path to select
      */
    void setSelectionPath(TreePath path);

    /**
      * Sets the selection to the paths.  If this represents a
      * change the TreeSelectionListeners are notified.
      *
      * @param paths new selection.
      */
    void setSelectionPaths(TreePath[] paths);

    /**
      * Adds path to the current selection.  If path is not currently
      * in the selection the TreeSelectionListeners are notified.
      *
      * @param path the new path to add to the current selection.
      */
    void addSelectionPath(TreePath path);

    /**
      * Adds paths to the current selection.  If any of the paths in
* paths are not currently in the selection the
      * TreeSelectionListeners are notified.
      *
      * @param path the new path to add to the current selection.
      */
    void addSelectionPaths(TreePath[] paths);

    /**
      * Removes path from the selection.  If path is in the selection
      * The TreeSelectionListeners are notified.
      *
      * @param path the path to remove from the selection.
      */
    void removeSelectionPath(TreePath path);

    /**
      * Removes paths from the selection.  If any of the paths in paths
      * are in the selection the TreeSelectionListeners are notified.
      *
      * @param path the path to remove from the selection.
      */
    void removeSelectionPaths(TreePath[] paths);

    /**
      * Returns the first path in the selection.
      */
    TreePath getSelectionPath();

    /**
      * Returns the paths in the selection.
      */
    TreePath[] getSelectionPaths();

    /**
     * Returns the number of paths that are selected.
     */
    int getSelectionCount();

    /**
      * Returns true if the path, path, is in the current selection.
      */
    boolean isPathSelected(TreePath path);

    /**
      * Returns true if the selection is currently empty.
      */
    boolean isSelectionEmpty();

    /**
      * Empties the current selection.  If this represents a change in
      * the current selection, the selection listeners are notified.
      */
    void clearSelection();

    /**
     * Sets the RowMapper instance.  This instance is used to determine
     * what row corresponds to what path.
     */
    void setRowMapper(RowMapper newMapper);

    /**
     * Returns the RowMapper instance that is able to map a path to a
     * row.
     */
    RowMapper getRowMapper();

    /**
      * Returns all of the currently selected rows.
      */
    int[] getSelectionRows();

    /**
      * Gets the first selected row.
      */
    int getMinSelectionRow();

    /**
      * Gets the last selected row.
      */
    int getMaxSelectionRow();

    /**
      * Returns true if the row identified by row is selected.
      */
    boolean isRowSelected(int row);

    /**
     * Updates what rows are selected.  This can be externally called in
     * case the location of the paths change, but not the actual paths.
     * You do not normally need to call this.
     */
    void resetRowSelection();

    /**
     * Returns the lead selection index. That is the last index that was
     * added.
     */
    int getLeadSelectionRow();

    /**
     * Returns the last path that was added.
     */
    TreePath getLeadSelectionPath();

    /**
     * Add a PropertyChangeListener to the listener list.
     * The listener is registered for all properties.
     * <p>
     * A PropertyChangeEvent will get fired in response to an
     * explicit setFont, setBackground, or SetForeground on the
     * current component.  Note that if the current component is
     * inheriting its foreground, background, or font from its
     * container, then no event will be fired in response to a
     * change in the inherited property.
     *
     * @param listener  The PropertyChangeListener to be added
     */
    void addPropertyChangeListener(PropertyChangeListener
 listener);

    /**
     * Remove a PropertyChangeListener from the listener list.
     * This removes a PropertyChangeListener that was registered
     * for all properties.
     *
     * @param listener  The PropertyChangeListener to be removed
     */
    void removePropertyChangeListener(PropertyChangeListener listener);

    /**
      * Adds x to the list of listeners that are notified each time the
      * selection changes.
      *
      * @param x the new listener to be added.
      */
    void addTreeSelectionListener(TreeSelectionListener x);

    /**
      * Removes x from the list of listeners that are notified each time
      * the selection changes.
      *
      * @param x the listener to remove.
      */
    void removeTreeSelectionListener(TreeSelectionListener x);
}


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.