Class TreeNodeChooser

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, TreeSelectionListener
Direct Known Subclasses:
TableNodeChooser

public class TreeNodeChooser extends JPanel implements TreeSelectionListener
A component which presents a tree of nodes and allows navigation around it to locate a node of interest. One node can be selected. This is intended to be used in a similar way to the JFileChooser component.

The general visual appearance of this component should be mostly self-explanatory. Some buttons appear near the top:

  • Up: Changes the root of the tree to the parent object of the current root. In the typical case in which the current root is a directory, this means the root of the tree becomes the parent directory of the current root.
  • Down: This takes the currently selected node (if there is one) and makes it into the new root node
  • Home: Sets the root as the user's home directory.

The protected isChoosable(uk.ac.starlink.datanode.nodes.DataNode) method provides a way for subclasses to restrict which nodes can be chosen from the component or dialog. Subclasses may override this method to indicate which nodes are eligible, and node eligibility will be reflected visually by the component (node names written in bold for choosable nodes, that sort of thing), as well as controlling the enabled status of the selection button etc. The getSearch*Action methods provide actions which may be useful in this context; they will search through the tree recursively and expand it so that any choosable nodes are visible to the user. Note this may result in some non-choosable nodes being visible (parents and siblings of choosable ones) too, but branches which contain no choosable nodes will not be displayed expanded in the GUI.

The chooser initially contains a default set of root items, and a suitable default (the current directory) is selected.

Author:
Mark Taylor (Starlink)
See Also:
  • Constructor Details

    • TreeNodeChooser

      public TreeNodeChooser()
      Constructs a new chooser widget with no content.
  • Method Details

    • setRootObject

      public void setRootObject(Object obj) throws NoSuchDataException
      Sets the root of the displayed tree to one made from a given object. This attempts to create a DataNode from obj by feeding it to the DataNodeFactory.
      Parameters:
      obj - object from which to form new root node.
      Throws:
      NoSuchDataException
    • getRootSelector

      public NodeRootComboBox getRootSelector()
      Returns the component which allows the user to select the current root of the tree.
      Returns:
      root combo box;
    • clear

      public void clear()
      Removes all roots from the root selector.
    • setRoot

      public void setRoot(DataNode node)
      Sets the root of the tree to a new node, possibly one not already in the tree.
      Parameters:
      node - new root
    • getRoot

      public DataNode getRoot()
      Returns the root of the tree displayed in this chooser.
      Returns:
      root node
    • setControlsVisible

      public void setControlsVisible(boolean visible)
      Sets whether the panel containing the OK and Cancel buttons is visible or not.
      Parameters:
      visible - true iff OK and Cancel buttons should be seen
    • getChooseAction

      public Action getChooseAction()
      Returns the action which indicated that the currently selected node is to be used.
      Returns:
      choose action
    • getButtonPanel

      public JComponent getButtonPanel()
      Returns a panel into which extra buttons can be placed.
      Returns:
      a box for buttons
    • selectNode

      protected void selectNode(DataNode node)
      Called when the user has finished interacting with this chooser. The node argument will be the DataNode which the user has selected if s/he has selected one, or null if the user pushed the cancel button. This may be overridden to do something useful as an alternative to using the chooseDataNode(java.awt.Component, java.lang.String, java.lang.String) method. The default implementation does nothing.
      Parameters:
      node - the selected node, or null
    • chooseDataNode

      public DataNode chooseDataNode(Component parent, String buttonText, String title)
      Pops up a modal dialog which asks the user for a DataNode. The return value is a node which the user selected, or null if no selection was made.
      Parameters:
      parent - the parent of the dialog
      buttonText - the text to appear on the 'choose' button (or null for default)
      title - the title of the dialog window (or null for default)
      Returns:
      the selected DataNode, or null if none was selected
    • getChosenPath

      public String getChosenPath()
      Returns the path of the currently chosen node.
      Returns:
      chosen node path, or null if none is chosen
    • getSelectedNode

      public DataNode getSelectedNode()
      Returns the datanode which is currently selected in the GUI.
      Returns:
      selected node
    • createDialog

      protected JDialog createDialog(Component parent)
      Constructs the dialog component used by the chooseDataNode(java.awt.Component, java.lang.String, java.lang.String) method. This can be overridden by subclasses to customise the dialog's appearance.
      Parameters:
      parent - the owner of the returned dialog
      Returns:
      modal dialog containing this chooser
    • showNodeDetail

      protected void showNodeDetail(DataNode node)
      Invoked when the selection is changed to update the display according to the current selection. Subclasses may override this to customise the info panel.
      Parameters:
      node - the node currently selected
    • isChoosable

      protected boolean isChoosable(DataNode node)
      Indicates whether a given node is eligable to be chosen with the Accept button. The implementation in TreeNodeChooser always returns true.
      Parameters:
      node - the node to test
      Returns:
      true iff node can be chosen by this chooser
    • valueChanged

      public void valueChanged(TreeSelectionEvent evt)
      Implements the TreeSelectionListener interface; public as an implementation detail.
      Specified by:
      valueChanged in interface TreeSelectionListener
    • getNodeMaker

      public DataNodeFactory getNodeMaker()
      Returns the DataNodeFactory which is used for turning objects into data nodes. This method is used wherever a node factory is required, so subclasses may override it to change the node creation behaviour.
      Returns:
      the data node factory
    • setNodeMaker

      public void setNodeMaker(DataNodeFactory nodeMaker)
      Sets the DataNodeFactory which is used for turning objects into data nodes.
      Parameters:
      nodeMaker - the new data node factory
    • showAllChoosable

      public Thread showAllChoosable(DataNode startNode)
      Opens up the tree recursively from a given node to display all the choosable items at any level.
      Parameters:
      startNode - the node from which to start
      Returns:
      the thread in which the search is done
    • getSearchSelectedAction

      public Action getSearchSelectedAction()
      Returns an action which will search the tree recursively for choosable nodes, starting from the selected node. The tree is opened up so that all choosable nodes thus discovered are visible.
      Returns:
      action
    • getSearchAllAction

      public Action getSearchAllAction()
      Returns an action which will search the entire tree for choosable nodes, starting from the root. The tree is opened up so that all choosable nodes thus discovered are visible.
      Returns:
      action