KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > Layout


1 package com.opensymphony.workflow.designer;
2
3 import java.awt.*;
4 import java.io.InputStream JavaDoc;
5 import java.io.PrintWriter JavaDoc;
6 import java.util.*;
7 import javax.xml.parsers.DocumentBuilder JavaDoc;
8 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
9
10 import com.opensymphony.workflow.loader.XMLUtil;
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.NodeList JavaDoc;
14
15 public class Layout
16 {
17   private String JavaDoc url;
18
19   private Collection entries = new ArrayList();
20   private Map allCells = new HashMap();
21   private Map results = new HashMap();
22
23     /**
24      * A Map of Maps, with keys being type names.
25      * Values are a map of id/bounds
26      */

27     private Map cellsByType = new HashMap();
28
29   public Layout()
30   {
31   }
32
33   public void setAllEntries(Collection entries)
34   {
35     this.entries = entries;
36   }
37
38   public String JavaDoc getUrl()
39   {
40     return url;
41   }
42
43   public void setUrl(String JavaDoc url)
44   {
45     this.url = url;
46   }
47
48   public Layout(InputStream JavaDoc in)
49   {
50     DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
51     dbf.setNamespaceAware(true);
52     DocumentBuilder JavaDoc db;
53     try
54     {
55       db = dbf.newDocumentBuilder();
56       Document JavaDoc doc;
57       doc = db.parse(in);
58
59       NodeList JavaDoc mActivitycell = doc.getElementsByTagName("cell");
60       for(int k = 0; k < mActivitycell.getLength(); k++)
61       {
62         Element JavaDoc element = (Element JavaDoc)mActivitycell.item(k);
63         CellPosition pos = new CellPosition(element);
64         Rectangle bound = pos.getBounds();
65         allCells.put(new Integer JavaDoc(pos.getId()), bound);
66           Map map = (Map)cellsByType.get(pos.getType());
67           if(map==null)
68           {
69               map = new HashMap();
70               cellsByType.put(pos.getType(), map);
71           }
72           map.put(new Integer JavaDoc(pos.getId()), bound);
73       }
74       NodeList JavaDoc list = doc.getElementsByTagName("connector");
75       for(int k = 0; k < list.getLength(); k++)
76       {
77         Element JavaDoc element = (Element JavaDoc)list.item(k);
78         ResultPosition pos = new ResultPosition(element);
79         Point p = pos.getLabelPosition();
80         results.put(new Integer JavaDoc(pos.getId()), p);
81       }
82     }
83     catch(Exception JavaDoc e)
84     {
85       e.printStackTrace();
86     }
87   }
88
89   public void writeXML(PrintWriter JavaDoc out, int indent)
90   {
91     out.println("<?xml version=\"1.0\"?>");
92     XMLUtil.printIndent(out, indent++);
93     out.println("<layout>");
94
95     Iterator it = entries.iterator();
96     while(it.hasNext())
97     {
98       Object JavaDoc next = it.next();
99       if(next instanceof WorkflowCell)
100       {
101         WorkflowCell cell = (WorkflowCell)next;
102         CellPosition pos = new CellPosition(cell);
103         pos.writeXML(out, indent);
104       }
105       else
106       {
107         ResultEdge edge = (ResultEdge)next;
108         ResultPosition pos = new ResultPosition(edge);
109         pos.writeXML(out, indent);
110       }
111     }
112     XMLUtil.printIndent(out, --indent);
113     out.println("</layout>");
114     out.flush();
115     out.close();
116   }
117
118     public Rectangle getBounds(int key, String JavaDoc type)
119     {
120         if(type==null)
121           return (Rectangle)allCells.get(new Integer JavaDoc(key));
122         Map typeMap = (Map)cellsByType.get(type);
123         if(typeMap==null)
124         {
125             return null;
126         }
127         Rectangle bounds = (Rectangle)typeMap.get(new Integer JavaDoc(key));
128         return bounds;
129     }
130
131   public Point getLabelPosition(int resultKey)
132   {
133     return (Point)results.get(new Integer JavaDoc(resultKey));
134   }
135 }
136
Popular Tags