FileDocCategorySizeDatePackage
CategoryExplorerModel.javaAPI DocApache log4j 1.2.1510685Sat Aug 25 00:09:38 BST 2007org.apache.log4j.lf5.viewer.categoryexplorer

CategoryExplorerModel

public class CategoryExplorerModel extends DefaultTreeModel
CategoryExplorerModel
author
Michael J. Sikorsky
author
Robert Shaw
author
Brent Sprecher
author
Richard Hurst

Fields Summary
private static final long
serialVersionUID
protected boolean
_renderFatal
protected ActionListener
_listener
protected ActionEvent
_event
Constructors Summary
public CategoryExplorerModel(CategoryNode node)


  //--------------------------------------------------------------------------
  //   Private Variables:
  //--------------------------------------------------------------------------

  //--------------------------------------------------------------------------
  //   Constructors:
  //--------------------------------------------------------------------------

     
    super(node);
  
Methods Summary
public synchronized voidaddActionListener(java.awt.event.ActionListener l)

    _listener = AWTEventMulticaster.add(_listener, l);
  
public org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNodeaddCategory(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryPath path)

Method altered by Richard Hurst such that it returns the CategoryNode corresponding to the CategoryPath

param
path category path.
return
CategoryNode

    CategoryNode root = (CategoryNode) getRoot();
    CategoryNode parent = root; // Start condition.

    for (int i = 0; i < path.size(); i++) {
      CategoryElement element = path.categoryElementAt(i);

      // If the two nodes have matching titles they are considered equal.
      Enumeration children = parent.children();

      boolean categoryAlreadyExists = false;
      while (children.hasMoreElements()) {
        CategoryNode node = (CategoryNode) children.nextElement();
        String title = node.getTitle().toLowerCase();

        String pathLC = element.getTitle().toLowerCase();
        if (title.equals(pathLC)) {
          categoryAlreadyExists = true;
          // This is now the new parent node.
          parent = node;
          break;
        }
      }

      if (categoryAlreadyExists == false) {
        // We need to add the node.
        CategoryNode newNode = new CategoryNode(element.getTitle());

        //This method of adding a new node cause parent roots to be
        // collapsed.
        //parent.add( newNode );
        //reload(parent);

        // This doesn't force the nodes to collapse.
        insertNodeInto(newNode, parent, parent.getChildCount());
        refresh(newNode);

        // The newly added node is now the parent.
        parent = newNode;

      }
    }

    return parent;
  
public voidaddLogRecord(org.apache.log4j.lf5.LogRecord lr)

    CategoryPath path = new CategoryPath(lr.getCategory());
    addCategory(path); // create category path if it is new
    CategoryNode node = getCategoryNode(path);
    node.addRecord(); // update category node
    if (_renderFatal && lr.isFatal()) {
      TreeNode[] nodes = getPathToRoot(node);
      int len = nodes.length;
      CategoryNode parent;

      // i = 0 gives root node
      // skip node and root, loop through "parents" in between
      for (int i = 1; i < len - 1; i++) {
        parent = (CategoryNode) nodes[i];
        parent.setHasFatalChildren(true);
        nodeChanged(parent);
      }
      node.setHasFatalRecords(true);
      nodeChanged(node);
    }
  
public org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNodegetCategoryNode(java.lang.String category)

    CategoryPath path = new CategoryPath(category);
    return (getCategoryNode(path));
  
public org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNodegetCategoryNode(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryPath path)
returns null if no CategoryNode exists.

    CategoryNode root = (CategoryNode) getRoot();
    CategoryNode parent = root; // Start condition.

    for (int i = 0; i < path.size(); i++) {
      CategoryElement element = path.categoryElementAt(i);

      // If the two nodes have matching titles they are considered equal.
      Enumeration children = parent.children();

      boolean categoryAlreadyExists = false;
      while (children.hasMoreElements()) {
        CategoryNode node = (CategoryNode) children.nextElement();
        String title = node.getTitle().toLowerCase();

        String pathLC = element.getTitle().toLowerCase();
        if (title.equals(pathLC)) {
          categoryAlreadyExists = true;
          // This is now the new parent node.
          parent = node;
          break; // out of the while, and back to the for().
        }
      }

      if (categoryAlreadyExists == false) {
        return null; // Didn't find the Node.
      }
    }

    return (parent);
  
public org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNodegetRootCategoryNode()

    return (CategoryNode) getRoot();
  
public javax.swing.tree.TreePathgetTreePathToRoot(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node)

Returns the CategoryPath to the specified CategoryNode

param
node The target CategoryNode
return
CategoryPath

    if (node == null) {
      return null;
    }
    return (new TreePath(getPathToRoot(node)));
  
public booleanisCategoryPathActive(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryPath path)

return
true if all the nodes in the specified CategoryPath are selected.

    CategoryNode root = (CategoryNode) getRoot();
    CategoryNode parent = root; // Start condition.
    boolean active = false;

    for (int i = 0; i < path.size(); i++) {
      CategoryElement element = path.categoryElementAt(i);

      // If the two nodes have matching titles they are considered equal.
      Enumeration children = parent.children();

      boolean categoryAlreadyExists = false;
      active = false;

      while (children.hasMoreElements()) {
        CategoryNode node = (CategoryNode) children.nextElement();
        String title = node.getTitle().toLowerCase();

        String pathLC = element.getTitle().toLowerCase();
        if (title.equals(pathLC)) {
          categoryAlreadyExists = true;
          // This is now the new parent node.
          parent = node;

          if (parent.isSelected()) {
            active = true;
          }

          break; // out of the while, and back to the for().
        }
      }

      if (active == false || categoryAlreadyExists == false) {
        return false;
      }
    }

    return (active);
  
protected voidnotifyActionListeners()

    if (_listener != null) {
      _listener.actionPerformed(_event);
    }
  
protected voidrefresh(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node)
Fires a nodechanged event on the SwingThread.

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        nodeChanged(node); // remind the tree to render the new node
      }
    });
  
public synchronized voidremoveActionListener(java.awt.event.ActionListener l)

    _listener = AWTEventMulticaster.remove(_listener, l);
  
public voidresetAllNodeCounts()

    Enumeration nodes = getRootCategoryNode().depthFirstEnumeration();
    CategoryNode current;
    while (nodes.hasMoreElements()) {
      current = (CategoryNode) nodes.nextElement();
      current.resetNumberOfContainedRecords();
      nodeChanged(current);
    }
  
public voidsetDescendantSelection(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node, boolean selected)

    Enumeration descendants = node.depthFirstEnumeration();
    CategoryNode current;
    while (descendants.hasMoreElements()) {
      current = (CategoryNode) descendants.nextElement();
      // does the current node need to be changed?
      if (current.isSelected() != selected) {
        current.setSelected(selected);
        nodeChanged(current);
      }
    }
    notifyActionListeners();
  
public voidsetParentSelection(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node, boolean selected)

    TreeNode[] nodes = getPathToRoot(node);
    int len = nodes.length;
    CategoryNode parent;

    // i = 0 gives root node, i=len-1 gives this node
    // skip the root node
    for (int i = 1; i < len; i++) {
      parent = (CategoryNode) nodes[i];
      if (parent.isSelected() != selected) {
        parent.setSelected(selected);
        nodeChanged(parent);
      }
    }
    notifyActionListeners();
  
public voidupdate(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node, boolean selected)

    if (node.isSelected() == selected) {
      return; // nothing was changed, nothing to do
    }
    // select parents or deselect children
    if (selected) {
      setParentSelection(node, true);
    } else {
      setDescendantSelection(node, false);
    }