Package com.sun.demo

Class ElementTreePanel

All Implemented Interfaces:
ImageObserver, MenuContainer, PropertyChangeListener, Serializable, EventListener, Accessible, CaretListener, DocumentListener, TreeSelectionListener

public class ElementTreePanel extends JPanel implements CaretListener, DocumentListener, PropertyChangeListener, TreeSelectionListener
Displays a tree showing all the elements in a text Document. Selecting a node will result in reseting the selection of the JTextComponent. This also becomes a CaretListener to know when the selection has changed in the text to update the selected item in the tree.
See Also:
  • Field Details

    • tree

      protected JTree tree
      Tree showing the documents element structure.
    • editor

      protected JTextComponent editor
      Text component showing elemenst for.
    • treeModel

      protected ElementTreePanel.ElementTreeModel treeModel
      Model for the tree.
    • updatingSelection

      protected boolean updatingSelection
      Set to true when updatin the selection.
  • Constructor Details

  • Method Details

    • setEditor

      public void setEditor(JTextComponent editor)
      Resets the JTextComponent to editor. This will update the tree accordingly.
    • propertyChange

      public void propertyChange(PropertyChangeEvent e)
      Invoked when a property changes. We are only interested in when the Document changes to reset the DocumentListener.
      Specified by:
      propertyChange in interface PropertyChangeListener
    • insertUpdate

      public void insertUpdate(DocumentEvent e)
      Gives notification that there was an insert into the document. The given range bounds the freshly inserted region.
      Specified by:
      insertUpdate in interface DocumentListener
      Parameters:
      e - the document event
    • removeUpdate

      public void removeUpdate(DocumentEvent e)
      Gives notification that a portion of the document has been removed. The range is given in terms of what the view last saw (that is, before updating sticky positions).
      Specified by:
      removeUpdate in interface DocumentListener
      Parameters:
      e - the document event
    • changedUpdate

      public void changedUpdate(DocumentEvent e)
      Gives notification that an attribute or set of attributes changed.
      Specified by:
      changedUpdate in interface DocumentListener
      Parameters:
      e - the document event
    • caretUpdate

      public void caretUpdate(CaretEvent e)
      Messaged when the selection in the editor has changed. Will update the selection in the tree.
      Specified by:
      caretUpdate in interface CaretListener
    • valueChanged

      public void valueChanged(TreeSelectionEvent e)
      Called whenever the value of the selection changes.
      Specified by:
      valueChanged in interface TreeSelectionListener
      Parameters:
      e - the event that characterizes the change.
    • getTree

      protected JTree getTree()
      Returns:
      tree showing elements.
    • getEditor

      protected JTextComponent getEditor()
      Returns:
      JTextComponent showing elements for.
    • getTreeModel

      public DefaultTreeModel getTreeModel()
      Returns:
      TreeModel implementation used to represent the elements.
    • updateTree

      protected void updateTree(DocumentEvent event)
      Updates the tree based on the event type. This will invoke either updateTree with the root element, or handleChange.
    • updateTree

      protected void updateTree(DocumentEvent event, Element element)
      Creates TreeModelEvents based on the DocumentEvent and messages the treemodel. This recursively invokes this method with children elements.
      Parameters:
      event - indicates what elements in the tree hierarchy have changed.
      element - Current element to check for changes against.
    • getPathForIndex

      protected TreePath getPathForIndex(int position, Object root, Element rootElement)
      Returns a TreePath to the element at position.