01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Original code by *
09: *****************************************************************************/package org.picocontainer.gems.monitors.prefuse;
10:
11: import java.util.Collection;
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: import org.picocontainer.gems.monitors.ComponentDependencyMonitor.Dependency;
16:
17: import prefuse.data.Graph;
18: import prefuse.data.Node;
19: import prefuse.data.Schema;
20: import prefuse.data.tuple.TupleSet;
21:
22: public final class PrefuseDependencyGraph implements
23: ComponentDependencyListener {
24: private Graph graph;
25:
26: private final Map nodes;
27:
28: public PrefuseDependencyGraph() {
29: this .graph = initializeGraph();
30: this .nodes = new HashMap();
31: }
32:
33: public void addDependency(Dependency dependency) {
34: Node componentNode = addNode(dependency.getComponentType());
35: Node dependencyNode = addNode(dependency.getDependencyType());
36: if (dependencyNode != null) {
37: graph.addEdge(componentNode, dependencyNode);
38: }
39: }
40:
41: Collection getTypes() {
42: return nodes.keySet();
43: }
44:
45: Node[] getNodes() {
46: return (Node[]) nodes.values().toArray(new Node[nodes.size()]);
47: }
48:
49: private Node addNode(Class type) {
50: if (type != null && !nodes.containsKey(type)) {
51: Node node = graph.addNode();
52: node.set("type", type);
53: nodes.put(type, node);
54: }
55: return (Node) nodes.get(type);
56: }
57:
58: private Graph initializeGraph() {
59: return getGraph(getSchema());
60: }
61:
62: private Graph getGraph(Schema schema) {
63: graph = new Graph(true);
64: graph.addColumns(schema);
65: return graph;
66: }
67:
68: private Schema getSchema() {
69: Schema schema = new Schema();
70: schema.addColumn("type", Class.class, null);
71: return schema;
72: }
73:
74: public TupleSet getEdges() {
75: return graph.getEdges();
76: }
77:
78: public Graph getGraph() {
79: return graph;
80: }
81: }
|