001: package com.opensymphony.workflow.designer;
002:
003: import java.awt.Point;
004: import java.awt.geom.Point2D;
005: import java.io.PrintWriter;
006: import java.util.ArrayList;
007:
008: import com.opensymphony.workflow.loader.XMLUtil;
009: import com.opensymphony.workflow.util.XMLizable;
010: import org.jgraph.graph.GraphConstants;
011: import org.jgraph.graph.EdgeView;
012: import org.w3c.dom.Element;
013:
014: /**
015: * @author Hani Suleiman (hani@formicary.net)
016: * Date: Dec 1 2003
017: * Time: 10:09:13 PM
018: */
019: public class ResultPosition implements XMLizable {
020: private Point2D labelPos;
021: private int id;
022: private float lineWidth = 1;
023: private int color = 0;
024: private int from;
025: private int to;
026: private java.util.List routingPoints = new ArrayList();
027:
028: public ResultPosition(WorkflowGraph graph, ResultEdge edge) {
029: id = edge.getDescriptor().getId();
030: EdgeView view = (EdgeView) graph.getGraphLayoutCache()
031: .getMapping(edge, false);
032: labelPos = view.getLabelPosition();
033: from = ((WorkflowPort) ((ResultEdge) view.getCell())
034: .getSource()).getIndex();
035: to = ((WorkflowPort) ((ResultEdge) view.getCell()).getTarget())
036: .getIndex();
037: lineWidth = GraphConstants.getLineWidth(edge.getAttributes());
038: color = GraphConstants.getForeground(edge.getAttributes())
039: .getRGB();
040: for (int i = 0; i < (view.getPointCount() - 2); i++) {
041: routingPoints.add(new Point((int) view.getPoint(i + 1)
042: .getX(), (int) view.getPoint(i + 1).getY()));
043: }
044: }
045:
046: public ResultPosition(Element edge) {
047: try {
048: String attr = edge.getAttribute("id");
049: if (attr != null && attr.length() > 0)
050: id = Integer.parseInt(attr);
051:
052: attr = edge.getAttribute("linewidth");
053: if (attr != null && attr.length() > 0)
054: lineWidth = Float.parseFloat(attr);
055:
056: attr = edge.getAttribute("color");
057: if (attr != null && attr.length() > 0)
058: color = Integer.parseInt(attr);
059:
060: labelPos = new Point2D.Double();
061: attr = edge.getAttribute("labelx");
062: double x = 0;
063: double y = 0;
064: if (attr != null && attr.length() > 0)
065: x = Double.parseDouble(attr);
066:
067: attr = edge.getAttribute("labely");
068: if (attr != null && attr.length() > 0)
069: y = Double.parseDouble(attr);
070: if (x != 0 && y != 0) {
071: labelPos.setLocation(x, y);
072: }
073:
074: attr = edge.getAttribute("from");
075: if (attr != null && attr.length() > 0)
076: from = Integer.parseInt(attr);
077:
078: attr = edge.getAttribute("to");
079: if (attr != null && attr.length() > 0)
080: to = Integer.parseInt(attr);
081: } catch (Exception e) {
082: System.out.println("Error parsing result position:" + e);
083: }
084: }
085:
086: public void writeXML(PrintWriter writer, int indent) {
087: XMLUtil.printIndent(writer, indent);
088: StringBuffer buf = new StringBuffer();
089: buf.append("<connector ");
090: buf.append("id=\"").append(id).append('\"');
091: buf.append(" linewidth=\"").append(lineWidth).append('\"');
092: buf.append(" color=\"").append(color).append('\"');
093: buf.append(" labelx=\"").append(labelPos.getX()).append('\"');
094: buf.append(" labely=\"").append(labelPos.getY()).append('\"');
095: buf.append(" from=\"").append(from).append('\"');
096: buf.append(" to=\"").append(to).append('\"');
097: writer.print(buf.toString());
098: if (routingPoints.size() > 0) {
099: writer.println('>');
100: for (int i = 0; i < routingPoints.size(); i++) {
101: StringBuffer pointbuf = new StringBuffer();
102: XMLUtil.printIndent(writer, indent + 1);
103: pointbuf.append("<routing");
104: pointbuf.append(" id=\"").append(id).append('\"');
105: pointbuf.append(" x=\"").append(
106: ((Point) routingPoints.get(i)).x).append('\"');
107: pointbuf.append(" y=\"").append(
108: ((Point) routingPoints.get(i)).y).append('\"');
109: pointbuf.append("/>");
110: writer.println(pointbuf.toString());
111: }
112: XMLUtil.printIndent(writer, indent++);
113: writer.println("</connector>");
114: } else {
115: writer.println("/>");
116: }
117: }
118:
119: public Point2D getLabelPosition() {
120: return labelPos;
121: }
122:
123: public float getLineWidth() {
124: return lineWidth;
125: }
126:
127: public int getColor() {
128: return color;
129: }
130:
131: public int getId() {
132: return id;
133: }
134:
135: public int getFrom() {
136: return from;
137: }
138:
139: public int getTo() {
140: return to;
141: }
142: }
|