1 package com.opensymphony.workflow.designer; 2 3 import java.awt.*; 4 import java.io.InputStream ; 5 import java.io.PrintWriter ; 6 import java.util.*; 7 import javax.xml.parsers.DocumentBuilder ; 8 import javax.xml.parsers.DocumentBuilderFactory ; 9 10 import com.opensymphony.workflow.loader.XMLUtil; 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.NodeList ; 14 15 public class Layout 16 { 17 private String url; 18 19 private Collection entries = new ArrayList(); 20 private Map allCells = new HashMap(); 21 private Map results = new HashMap(); 22 23 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 getUrl() 39 { 40 return url; 41 } 42 43 public void setUrl(String url) 44 { 45 this.url = url; 46 } 47 48 public Layout(InputStream in) 49 { 50 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 51 dbf.setNamespaceAware(true); 52 DocumentBuilder db; 53 try 54 { 55 db = dbf.newDocumentBuilder(); 56 Document doc; 57 doc = db.parse(in); 58 59 NodeList mActivitycell = doc.getElementsByTagName("cell"); 60 for(int k = 0; k < mActivitycell.getLength(); k++) 61 { 62 Element element = (Element )mActivitycell.item(k); 63 CellPosition pos = new CellPosition(element); 64 Rectangle bound = pos.getBounds(); 65 allCells.put(new Integer (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 (pos.getId()), bound); 73 } 74 NodeList list = doc.getElementsByTagName("connector"); 75 for(int k = 0; k < list.getLength(); k++) 76 { 77 Element element = (Element )list.item(k); 78 ResultPosition pos = new ResultPosition(element); 79 Point p = pos.getLabelPosition(); 80 results.put(new Integer (pos.getId()), p); 81 } 82 } 83 catch(Exception e) 84 { 85 e.printStackTrace(); 86 } 87 } 88 89 public void writeXML(PrintWriter 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 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 type) 119 { 120 if(type==null) 121 return (Rectangle)allCells.get(new Integer (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 (key)); 128 return bounds; 129 } 130 131 public Point getLabelPosition(int resultKey) 132 { 133 return (Point)results.get(new Integer (resultKey)); 134 } 135 } 136 | Popular Tags |