edu.mit.ai.psg.ui.outliner
Class OutlineModel

java.lang.Object
  |
  +--javax.swing.tree.DefaultTreeModel
        |
        +--edu.mit.ai.psg.ui.outliner.OutlineModel

public class OutlineModel
extends DefaultTreeModel

OutlineModel extends DefaultTreeModel for OutlineNodes, (1) to trigger generating children when they are accessed if they have not yet been computed (getChildCount(java.lang.Object)), and (2) to notify children after they are added to and before they are removed from parent (insertNodeInto(javax.swing.tree.MutableTreeNode, javax.swing.tree.MutableTreeNode, int), removeNodeFromParent(javax.swing.tree.MutableTreeNode)). Thread policy: The model invokes OutlineRenderers to create children, but OutlineRenderers are not called from the AWT thread, since a renderer may take arbitrarily long, freezing out other UI updates. Since Swing requires that its objects be updated only from the AWT event dispatch thread (i.e., it uses an "apartment" threading style), so changes to the model to add the created child nodes are always performed in the AWT thread.

See Also:
SwingUtilities.invokeAndWait(java.lang.Runnable), Serialized Form

Inner Class Summary
static class OutlineModel.HiddenRootNode
          class used for hidden root node in outlines (outlines have a hidden root so that every display node has a parent, and rerendering can simply replace the node in its parent).
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Constructor Summary
OutlineModel(OutlineModel.HiddenRootNode hiddenRoot)
           
OutlineModel(OutlineNode displayRoot, OutlineRenderer hiddenRootRenderer)
          creates and initializes a HiddenRootNode.
 
Method Summary
 void addChildrenSafely(OutlineNode outlineNode, List children)
          adds child outline nodes to outlineNode safely (by using the AWT event dispatch thread so it won't interfere with display operations -- see SwingUtilities.invokeAndWait(java.lang.Runnable))
 void addNodeChild(OutlineNode parent, OutlineNode newChild)
          convenience method to insert child at end of children.
 void ensureChildrenAreComputed(OutlineNode outlineNode)
          If children haven't been computed, nor are they being computed by this thread or another thread, then computeChildren.
 Object getChild(Object parent, int index)
          extended to first triggerComputingChildren(edu.mit.ai.psg.ui.outliner.OutlineNode) so that children are added after node is first expanded, whether by JTree or someone else.
 int getChildCount(Object parent)
          extended to first triggerComputingChildren(edu.mit.ai.psg.ui.outliner.OutlineNode) so that children are added after node is first expanded, whether by JTree or someone else.
static OutlineModel getModel(OutlineNode outlineNode)
          find model using an outline node through hidden root node of outline.
 void insertNodeInto(MutableTreeNode newChild, MutableTreeNode parent, int index)
          extends MutableTreeModel to inform child after it is successfully inserted by calling OutlineRenderer.notifyInserted(edu.mit.ai.psg.ui.outliner.OutlineModel, edu.mit.ai.psg.ui.outliner.OutlineNode, int).
 OutlineNode regenerateNode(OutlineNode outlineNode, OutlineRenderer renderer)
          replaces outlineNode in parent with new node created using renderer, notifies model listeners, and returns new outlineNode.
 void removeNodeFromParent(MutableTreeNode child)
          extends MutableTreeModel to inform child before it is removed by calling OutlineRenderer.notifyRemoving(edu.mit.ai.psg.ui.outliner.OutlineModel, edu.mit.ai.psg.ui.outliner.OutlineNode), and to set child's parent to null afterward so it is no longer part of this outline.
protected  void triggerComputingChildren(OutlineNode outlineNode)
          If children haven't been computed (OutlineNode.childrenAreComputed()), nor are they being computed, then triggers creation of another thread to call ensureChildrenAreComputed(edu.mit.ai.psg.ui.outliner.OutlineNode).
 
Methods inherited from class javax.swing.tree.DefaultTreeModel
addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getIndexOfChild, getPathToRoot, getPathToRoot, getRoot, isLeaf, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, reload, removeTreeModelListener, setAsksAllowsChildren, setRoot, valueForPathChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OutlineModel

public OutlineModel(OutlineModel.HiddenRootNode hiddenRoot)

OutlineModel

public OutlineModel(OutlineNode displayRoot,
                    OutlineRenderer hiddenRootRenderer)
creates and initializes a HiddenRootNode. Initialization includes
hiddenRoot = new HiddenRootNode(hiddenRootRenderer);
hiddenRoot.setModel(this);
this.addNodeChild(hiddenRoot, displayRoot);
hiddenRoot.setChildrenAreComputed(true);
Method Detail

getModel

public static OutlineModel getModel(OutlineNode outlineNode)
find model using an outline node through hidden root node of outline. returns null if not part of a model

getChildCount

public int getChildCount(Object parent)
extended to first triggerComputingChildren(edu.mit.ai.psg.ui.outliner.OutlineNode) so that children are added after node is first expanded, whether by JTree or someone else. If need to guarantee that children are computed, first call ensureChildrenAreComputed(edu.mit.ai.psg.ui.outliner.OutlineNode)
Overrides:
getChildCount in class DefaultTreeModel

getChild

public Object getChild(Object parent,
                       int index)
extended to first triggerComputingChildren(edu.mit.ai.psg.ui.outliner.OutlineNode) so that children are added after node is first expanded, whether by JTree or someone else. If need to guarantee that children are computed, first call ensureChildrenAreComputed(edu.mit.ai.psg.ui.outliner.OutlineNode)
Overrides:
getChild in class DefaultTreeModel

triggerComputingChildren

protected void triggerComputingChildren(OutlineNode outlineNode)
If children haven't been computed (OutlineNode.childrenAreComputed()), nor are they being computed, then triggers creation of another thread to call ensureChildrenAreComputed(edu.mit.ai.psg.ui.outliner.OutlineNode). This avoids holding up AWT as user code may take indefinitely long, and display will be updated when children are added.

ensureChildrenAreComputed

public void ensureChildrenAreComputed(OutlineNode outlineNode)
If children haven't been computed, nor are they being computed by this thread or another thread, then computeChildren.
Throws:
IllegalStateException - if called from AWT Event dispatch thread.

addChildrenSafely

public void addChildrenSafely(OutlineNode outlineNode,
                              List children)
adds child outline nodes to outlineNode safely (by using the AWT event dispatch thread so it won't interfere with display operations -- see SwingUtilities.invokeAndWait(java.lang.Runnable))

addNodeChild

public void addNodeChild(OutlineNode parent,
                         OutlineNode newChild)
convenience method to insert child at end of children. Not thread safe once parent is displayed.
See Also:
addChildrenSafely(edu.mit.ai.psg.ui.outliner.OutlineNode, java.util.List)

insertNodeInto

public void insertNodeInto(MutableTreeNode newChild,
                           MutableTreeNode parent,
                           int index)
extends MutableTreeModel to inform child after it is successfully inserted by calling OutlineRenderer.notifyInserted(edu.mit.ai.psg.ui.outliner.OutlineModel, edu.mit.ai.psg.ui.outliner.OutlineNode, int).
Overrides:
insertNodeInto in class DefaultTreeModel

removeNodeFromParent

public void removeNodeFromParent(MutableTreeNode child)
extends MutableTreeModel to inform child before it is removed by calling OutlineRenderer.notifyRemoving(edu.mit.ai.psg.ui.outliner.OutlineModel, edu.mit.ai.psg.ui.outliner.OutlineNode), and to set child's parent to null afterward so it is no longer part of this outline.
Overrides:
removeNodeFromParent in class DefaultTreeModel

regenerateNode

public OutlineNode regenerateNode(OutlineNode outlineNode,
                                  OutlineRenderer renderer)
replaces outlineNode in parent with new node created using renderer, notifies model listeners, and returns new outlineNode.

Copyright (c) 1996-1999
Massachusetts Institute of Technology

Feedback: jeva-feedback@ai.mit.edu