KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > webFlow > model > SubGraph


1 package com.opensymphony.webwork.webFlow.model;
2
3 import java.io.IOException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 /**
9  * User: plightbo
10  * Date: Jun 26, 2005
11  * Time: 4:53:57 PM
12  */

13 public class SubGraph implements Render {
14     protected String JavaDoc name;
15     protected SubGraph parent;
16     protected List JavaDoc subGraphs;
17     protected List JavaDoc nodes;
18
19     public SubGraph(String JavaDoc name) {
20         this.name = name;
21         this.subGraphs = new ArrayList JavaDoc();
22         this.nodes = new ArrayList JavaDoc();
23     }
24
25     public String JavaDoc getName() {
26         return name;
27     }
28
29     public void addSubGraph(SubGraph subGraph) {
30         subGraph.setParent(this);
31         subGraphs.add(subGraph);
32     }
33
34     public void setParent(SubGraph parent) {
35         this.parent = parent;
36     }
37
38     public void addNode(WebFlowNode node) {
39         node.setParent(this);
40         Graph.nodeMap.put(node.getFullName(), node);
41         nodes.add(node);
42     }
43
44     public void render(IndentWriter writer) throws IOException JavaDoc {
45         // write the header
46
writer.write("subgraph cluster_" + getPrefix() + " {", true);
47         writer.write("color=grey;");
48         writer.write("fontcolor=grey;");
49         writer.write("label=\"" + name + "\";");
50
51         // write out the subgraphs
52
for (Iterator JavaDoc iterator = subGraphs.iterator(); iterator.hasNext();) {
53             SubGraph subGraph = (SubGraph) iterator.next();
54             subGraph.render(new IndentWriter(writer));
55         }
56
57         // write out the actions
58
for (Iterator JavaDoc iterator = nodes.iterator(); iterator.hasNext();) {
59             WebFlowNode webFlowNode = (WebFlowNode) iterator.next();
60             webFlowNode.render(writer);
61         }
62
63         // .. footer
64
writer.write("}", true);
65     }
66
67     public String JavaDoc getPrefix() {
68         if (parent == null) {
69             return name;
70         } else {
71             String JavaDoc prefix = parent.getPrefix();
72             if (prefix.equals("")) {
73                 return name;
74             } else {
75                 return prefix + "_" + name;
76             }
77         }
78     }
79
80     public SubGraph create(String JavaDoc namespace) {
81         if (namespace.equals("")) {
82             return this;
83         }
84
85         String JavaDoc[] parts = namespace.split("\\/");
86         SubGraph last = this;
87         for (int i = 0; i < parts.length; i++) {
88             String JavaDoc part = parts[i];
89             if (part.equals("")) {
90                 continue;
91             }
92
93             SubGraph subGraph = findSubGraph(part);
94             if (subGraph == null) {
95                 subGraph = new SubGraph(part);
96                 last.addSubGraph(subGraph);
97             }
98
99             last = subGraph;
100         }
101
102         return last;
103     }
104
105     private SubGraph findSubGraph(String JavaDoc name) {
106         for (Iterator JavaDoc iterator = subGraphs.iterator(); iterator.hasNext();) {
107             SubGraph subGraph = (SubGraph) iterator.next();
108             if (subGraph.getName().equals(name)) {
109                 return subGraph;
110             }
111         }
112
113         return null;
114     }
115 }
116
Popular Tags