Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 187   Methods: 17
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ComponentNode.java 72.5% 82.9% 88.2% 80.3%
coverage coverage
 1   
 package abbot.editor;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.util.*;
 5   
 
 6   
 import javax.swing.*;
 7   
 import javax.swing.tree.*;
 8   
 
 9   
 import abbot.Log;
 10   
 import abbot.finder.*;
 11   
 import abbot.i18n.Strings;
 12   
 import abbot.tester.Robot;
 13   
 import abbot.util.AWT;
 14   
 
 15   
 /** Provides a JTree-compatible node model for displaying a given hierarchy. */
 16   
 public class ComponentNode extends DefaultMutableTreeNode {
 17   
 
 18   
     private Hierarchy hierarchy;
 19   
     private Map map;
 20   
     private boolean loaded;
 21   
 
 22   
     /** Constructor for the root node of a hierarchy. */
 23  11
     public ComponentNode(Hierarchy hierarchy) {
 24  11
         super(null, true);
 25  11
         this.hierarchy = hierarchy;
 26  11
         map = new WeakHashMap();
 27   
     }
 28   
 
 29  631
     protected ComponentNode(ComponentNode parent, Object obj) {
 30  631
         super(obj, (obj == null
 31   
                     || obj instanceof Container
 32   
                     || obj instanceof MenuContainer));
 33  631
         hierarchy = parent.hierarchy;
 34  631
         map = parent.map;
 35  631
         map.put(obj, this);
 36   
     }
 37   
 
 38  631
     public ComponentNode(ComponentNode parent, Component comp) {
 39  631
         this(parent, (Object)comp);
 40   
     }
 41   
 
 42  0
     public ComponentNode(ComponentNode parent, MenuComponent comp) {
 43  0
         this(parent, (Object)comp);
 44   
     }
 45   
 
 46  0
     public ComponentNode(ComponentNode parent, MenuItem comp) {
 47  0
         this(parent, (Object)comp);
 48   
     }
 49   
 
 50  2245
     public TreeNode getChildAt(int index) {
 51  2245
         load();
 52  2245
         return super.getChildAt(index);
 53   
     }
 54   
 
 55  3801
     public int getChildCount() {
 56  3801
         load();
 57  3801
         return super.getChildCount();
 58   
     }
 59   
 
 60  182
     public void reload() {
 61  182
         reload(hierarchy);
 62   
     }
 63   
 
 64  184
     public void reload(Hierarchy hierarchy) {
 65  184
         this.hierarchy = hierarchy;
 66  184
         map.clear();
 67  184
         loaded = false;
 68   
     }
 69   
 
 70  6046
     private void load() {
 71  6046
         if (loaded)
 72  5761
             return;
 73   
 
 74  285
         loaded = true;
 75  285
         removeAllChildren();
 76  285
         Object obj = getUserObject();
 77  285
         if(isRoot()) {
 78  77
             Iterator iter = hierarchy.getRoots().iterator();
 79  77
             while (iter.hasNext()) {
 80  81
                 add(new ComponentNode(this, (Component)iter.next()));
 81   
             }
 82   
         }
 83  208
         else if(obj instanceof Container) {
 84   
             // Specially handle AWT MenuBar
 85  208
             if (obj instanceof Frame) {
 86  88
                 Frame f = (Frame)obj;
 87  88
                 if (f.getMenuBar() != null) {
 88  0
                     add(new ComponentNode(this, f.getMenuBar()));
 89   
                 }
 90   
             }
 91  208
             Collection children =
 92   
                 hierarchy.getComponents(getComponent());
 93  208
             Iterator iter = children.iterator();
 94  208
             while (iter.hasNext()) {
 95  550
                 add(new ComponentNode(this, (Component)iter.next()));
 96   
             }
 97   
         }
 98   
         // Specially handle AWT menus
 99  0
         else if(obj instanceof MenuBar) {
 100  0
             MenuBar mb = (MenuBar)obj;
 101  0
             for (int i=0;i < mb.getMenuCount();i++) {
 102  0
                 add(new ComponentNode(this, mb.getMenu(i)));
 103   
             }
 104   
         }
 105  0
         else if(obj instanceof Menu) {
 106  0
             Menu menu = (Menu)obj;
 107  0
             for (int i=0;i < menu.getItemCount();i++) {
 108  0
                 add(new ComponentNode(this, menu.getItem(i)));
 109   
             }
 110   
         }
 111   
     }
 112   
 
 113   
     /** Return the component that appears as a parent in the ComponentNode
 114   
      * hierarchy.
 115   
      */
 116  699
     Component getParent(Component c) {
 117  699
         return hierarchy.getParent(c);
 118   
     }
 119   
 
 120   
     /** Returns the Component represented, or null if this is either the root
 121   
      * or a java.awt.MenuComponent.
 122   
      */
 123  2203
     public Component getComponent() {
 124  2203
         if (getUserObject() instanceof Component)
 125  2171
             return (Component)getUserObject();
 126  32
         return null;
 127   
     }
 128   
 
 129  1991
     public int hashCode() {
 130  1991
         return(isRoot() ? super.hashCode() : getUserObject().hashCode());
 131   
     }
 132   
 
 133   
     /** Return true if the represented components are the same. */
 134  1335
     public boolean equals(Object other) {
 135  1335
         return this == other
 136   
             || ((other instanceof ComponentNode) 
 137   
                 && (getUserObject() == ((ComponentNode)other).getUserObject()));
 138   
     }
 139   
 
 140  299
     public String toString() {
 141  299
         if(isRoot()) {
 142  165
             return getChildCount() == 0 
 143   
                 ? Strings.get("NoComponents")
 144   
                 : Strings.get("AllFrames");
 145   
         }
 146  134
         return Robot.toString(getUserObject());
 147   
     }
 148   
 
 149   
     /** Return the nearest node corresponding to the given component. 
 150   
         Behavior is undefined if the node is not reachable from the root
 151   
         node.  If the component is elided in the underlying hierarchy, returns
 152   
         the nearest parent node that is not elided.
 153   
      */
 154  848
     public ComponentNode getNode(Component comp) {
 155  848
         if (comp == null) {
 156  125
             return (ComponentNode)getRoot();
 157   
         }
 158  723
         ComponentNode node = (ComponentNode)map.get(comp);
 159  723
         if (node == null) {
 160  699
             Component parentComp = getParent(comp);
 161  699
             ComponentNode parent = getNode(parentComp);
 162  699
             if (parent == null) {
 163  0
                 return getNode(parentComp);
 164   
             }
 165   
             // Fall back to parent if no child matches.
 166  699
             node = parent;
 167  699
             for (int i=0;i < parent.getChildCount();i++) {
 168  1801
                 ComponentNode child = (ComponentNode)parent.getChildAt(i);
 169  1801
                 if (child.getComponent() == comp) {
 170  624
                     node = child;
 171  624
                     break;
 172   
                 }
 173   
             }
 174   
         }
 175  723
         return node;
 176   
     }
 177   
 
 178   
     /** Return the TreePath for the given Component, assuming it is in the
 179   
         same hierarchy as this node.  Returns as much of the ancestor path as
 180   
         is available in the hierarchy.
 181   
     */
 182  18
     public TreePath getPath(Component comp) {
 183  18
         ComponentNode node = getNode(comp);
 184  18
         return new TreePath(node.getPath());
 185   
     }
 186   
 }
 187