KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jdepend > swingui > PackageNode


1 package jdepend.swingui;
2
3 import java.text.NumberFormat JavaDoc;
4 import java.util.*;
5
6 import jdepend.framework.*;
7
8 /**
9  * The <code>PackageNode</code> class defines the default behavior for tree
10  * nodes representing Java packages.
11  *
12  * @author <b>Mike Clark</b>
13  * @author Clarkware Consulting, Inc.
14  */

15
16 public abstract class PackageNode {
17
18     private PackageNode parent;
19
20     private JavaPackage jPackage;
21
22     private ArrayList children;
23
24     private static NumberFormat JavaDoc formatter;
25     static {
26         formatter = NumberFormat.getInstance();
27         formatter.setMaximumFractionDigits(2);
28     }
29
30     /**
31      * Constructs a <code>PackageNode</code> with the specified package and
32      * its collection of dependent packages.
33      *
34      * @param parent Parent package node.
35      * @param jPackage Java package.
36      */

37     public PackageNode(PackageNode parent, JavaPackage jPackage) {
38         this.parent = parent;
39         this.jPackage = jPackage;
40         children = null;
41     }
42
43     /**
44      * Returns the Java package represented in this node.
45      *
46      * @return Java package.
47      */

48     public JavaPackage getPackage() {
49         return jPackage;
50     }
51
52     /**
53      * Returns the parent of this package node.
54      *
55      * @return Parent package node.
56      */

57     public PackageNode getParent() {
58         return parent;
59     }
60
61     /**
62      * Indicates whether this node is a leaf node.
63      *
64      * @return <code>true</code> if this node is a leaf; <code>false</code>
65      * otherwise.
66      */

67     public boolean isLeaf() {
68         if (getCoupledPackages().size() > 0) {
69             return false;
70         }
71
72         return true;
73     }
74
75     /**
76      * Creates and returns a <code>PackageNode</code> with the specified
77      * parent node and Java package.
78      *
79      * @param parent Parent package node.
80      * @param jPackage Java package.
81      * @return A non-null <code>PackageNode</code.
82      */

83     protected abstract PackageNode makeNode(PackageNode parent,
84             JavaPackage jPackage);
85
86     /**
87      * Returns the collection of Java packages coupled to the package
88      * represented in this node.
89      *
90      * @return Collection of coupled packages.
91      */

92     protected abstract Collection getCoupledPackages();
93
94     /**
95      * Indicates whether the specified package should be displayed as a child of
96      * this node.
97      *
98      * @param jPackage Package to test.
99      * @return <code>true</code> to display the package; <code>false</code>
100      * otherwise.
101      */

102     public boolean isChild(JavaPackage jPackage) {
103         return true;
104     }
105
106     /**
107      * Returns the child package nodes of this node.
108      *
109      * @return Collection of child package nodes.
110      */

111     public ArrayList getChildren() {
112
113         if (children == null) {
114
115             children = new ArrayList();
116             ArrayList packages = new ArrayList(getCoupledPackages());
117             Collections.sort(packages, new PackageComparator(PackageComparator
118                     .byName()));
119             Iterator i = packages.iterator();
120             while (i.hasNext()) {
121                 JavaPackage jPackage = (JavaPackage) i.next();
122                 if (isChild(jPackage)) {
123                     PackageNode childNode = makeNode(this, jPackage);
124                     children.add(childNode);
125                 }
126             }
127         }
128
129         return children;
130     }
131
132     /**
133      * Returns the string representation of this node's metrics.
134      *
135      * @return Metrics string.
136      */

137     public String JavaDoc toMetricsString() {
138         StringBuffer JavaDoc label = new StringBuffer JavaDoc();
139         label.append(getPackage().getName());
140         label.append(" (");
141         label.append("CC: " + getPackage().getConcreteClassCount() + " ");
142         label.append("AC: " + getPackage().getAbstractClassCount() + " ");
143         label.append("Ca: " + getPackage().afferentCoupling() + " ");
144         label.append("Ce: " + getPackage().efferentCoupling() + " ");
145         label.append("A: " + format(getPackage().abstractness()) + " ");
146         label.append("I: " + format(getPackage().instability()) + " ");
147         label.append("D: " + format(getPackage().distance()) + " ");
148         label.append("V: " + getPackage().getVolatility());
149         if (getPackage().containsCycle()) {
150             label.append(" Cyclic");
151         }
152
153         label.append(")");
154
155         return label.toString();
156     }
157
158     /**
159      * Returns the string representation of this node in it's current tree
160      * context.
161      *
162      * @return Node label.
163      */

164     public String JavaDoc toString() {
165
166         if (getParent().getParent() == null) {
167             return toMetricsString();
168         }
169
170         return getPackage().getName();
171     }
172
173     /*
174      * Returns the specified number in a displayable format. @param number
175      * Number to format. @return Formatted number.
176      */

177     private static String JavaDoc format(float f) {
178         return formatter.format(f);
179     }
180 }
181
182
Popular Tags