001: package com.opensymphony.workflow.designer;
002:
003: import java.awt.Point;
004: import java.awt.geom.Point2D;
005: import java.io.*;
006: import java.util.*;
007: import javax.xml.parsers.DocumentBuilder;
008: import javax.xml.parsers.DocumentBuilderFactory;
009:
010: import com.opensymphony.workflow.loader.XMLUtil;
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013: import org.w3c.dom.NodeList;
014:
015: public class Layout {
016: private String url;
017:
018: private Collection entries = new ArrayList();
019: private Map allCells = new HashMap();
020: private Map results = new HashMap();
021:
022: /**
023: * A Map of Maps, with keys being type names.
024: * Values are a map of id/bounds
025: */
026: private Map cellsByType = new HashMap();
027:
028: private static class ResultLayout {
029: float lineWidth;
030: int color;
031: int from;
032: int to;
033: Point2D labelPosition;
034: java.util.List routingPoints = new ArrayList();
035:
036: ResultLayout(Point2D labelPosition, float lineWidth, int color,
037: int from, int to) {
038: if (labelPosition != null)
039: this .labelPosition = labelPosition;
040: this .lineWidth = lineWidth;
041: this .color = color;
042: this .from = from;
043: this .to = to;
044: }
045: }
046:
047: public Layout() {
048: }
049:
050: public void setAllEntries(Collection entries) {
051: this .entries = entries;
052: }
053:
054: public String getUrl() {
055: return url;
056: }
057:
058: public void setUrl(String url) {
059: this .url = url;
060: }
061:
062: public Layout(InputStream in) {
063: DocumentBuilderFactory dbf = DocumentBuilderFactory
064: .newInstance();
065: dbf.setNamespaceAware(true);
066: DocumentBuilder db;
067: try {
068: db = dbf.newDocumentBuilder();
069: Document doc;
070: doc = db.parse(in);
071:
072: NodeList mActivitycell = doc.getElementsByTagName("cell");
073: for (int k = 0; k < mActivitycell.getLength(); k++) {
074: Element element = (Element) mActivitycell.item(k);
075: CellPosition pos = new CellPosition(element);
076: double[] bound = pos.getBounds2D();
077: allCells.put(new Integer(pos.getId()), bound);
078: Map map = (Map) cellsByType.get(pos.getType());
079: if (map == null) {
080: map = new HashMap();
081: cellsByType.put(pos.getType(), map);
082: }
083: map.put(new Integer(pos.getId()), bound);
084: }
085: NodeList list = doc.getElementsByTagName("connector");
086: for (int k = 0; k < list.getLength(); k++) {
087: Element element = (Element) list.item(k);
088: ResultPosition pos = new ResultPosition(element);
089: ResultLayout r = new ResultLayout(pos
090: .getLabelPosition(), pos.getLineWidth(), pos
091: .getColor(), pos.getFrom(), pos.getTo());
092: results.put(new Integer(pos.getId()), r);
093: }
094: NodeList routing = doc.getElementsByTagName("routing");
095: for (int k = 0; k < routing.getLength(); k++) {
096: Element element = (Element) routing.item(k);
097: try {
098: int id = Integer.parseInt(element
099: .getAttribute("id"));
100: int x = Integer.parseInt(element.getAttribute("x"));
101: int y = Integer.parseInt(element.getAttribute("y"));
102: ResultLayout result = (ResultLayout) results
103: .get(new Integer(id));
104: if (result != null) {
105: result.routingPoints.add(new Point(x, y));
106: }
107: } catch (Exception e) {
108: System.out
109: .println("Error parsing routing position:"
110: + e);
111: }
112: }
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: public Layout(String in) {
119: this (new ByteArrayInputStream(in.getBytes()));
120: }
121:
122: public void writeXML(PrintWriter out, int indent,
123: WorkflowGraph graph) {
124: out.println("<?xml version=\"1.0\"?>");
125: XMLUtil.printIndent(out, indent++);
126: out.println("<layout>");
127:
128: Iterator it = entries.iterator();
129: while (it.hasNext()) {
130: Object next = it.next();
131: if (next instanceof WorkflowCell) {
132: WorkflowCell cell = (WorkflowCell) next;
133: CellPosition pos = new CellPosition(cell);
134: pos.writeXML(out, indent);
135: } else {
136: ResultEdge edge = (ResultEdge) next;
137: ResultPosition pos = new ResultPosition(graph, edge);
138: pos.writeXML(out, indent);
139: }
140: }
141: XMLUtil.printIndent(out, --indent);
142: out.println("</layout>");
143: out.flush();
144: out.close();
145: }
146:
147: /**
148: * Get the boounds for the specified key/type.
149: * @param key
150: * @param type
151: * @return double[] an array of 4 ints containing x, y, width, and height.
152: */
153: public double[] getBounds(int key, String type) {
154: if (type == null)
155: return (double[]) allCells.get(new Integer(key));
156: Map typeMap = (Map) cellsByType.get(type);
157: if (typeMap == null) {
158: return null;
159: }
160: double[] bounds = (double[]) typeMap.get(new Integer(key));
161: return bounds;
162: }
163:
164: public int getColor(int resultKey) {
165: ResultLayout rl = ((ResultLayout) results.get(new Integer(
166: resultKey)));
167: return rl != null ? rl.color : 0;
168: }
169:
170: public int getFromPort(int resultKey) {
171: ResultLayout rl = ((ResultLayout) results.get(new Integer(
172: resultKey)));
173: return rl != null ? rl.from : 0;
174: }
175:
176: public int getToPort(int resultKey) {
177: ResultLayout rl = ((ResultLayout) results.get(new Integer(
178: resultKey)));
179: return rl != null ? rl.to : 0;
180: }
181:
182: public float getLineWidth(int resultKey) {
183: ResultLayout resultLayout = (ResultLayout) results
184: .get(new Integer(resultKey));
185: return resultLayout != null ? resultLayout.lineWidth : 2;
186: }
187:
188: public Point2D getLabelPosition(int resultKey) {
189: ResultLayout rl = ((ResultLayout) results.get(new Integer(
190: resultKey)));
191: return rl != null && rl.labelPosition != null ? rl.labelPosition
192: : null;
193: }
194:
195: public java.util.List getRoutingPoints(int resultKey) {
196: ResultLayout rl = ((ResultLayout) results.get(new Integer(
197: resultKey)));
198: return rl != null && rl.routingPoints != null ? rl.routingPoints
199: : Collections.EMPTY_LIST;
200: }
201: }
|