KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > workfloweditor > WorkflowLayoutStore


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.workfloweditor;
20
21 import java.awt.geom.*;
22 import java.awt.geom.Point2D.Float;
23 import java.io.*;
24 import java.util.*;
25
26 import javax.xml.parsers.*;
27
28 import org.jaxen.dom.*;
29 import org.openharmonise.commons.xml.*;
30 import org.openharmonise.vfs.context.*;
31 import org.w3c.dom.*;
32
33
34 /**
35  * Manages the local storage of workflow diagram layouts.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class WorkflowLayoutStore implements ContextListener {
42
43     /**
44      * Instance, following the singleton pattern.
45      */

46     private static WorkflowLayoutStore m_instance = null;
47
48     /**
49      * Map of workflow path to map of workflow stage instance to 2D point.
50      */

51     private HashMap m_workflows = new HashMap();
52
53     /**
54      *
55      */

56     private WorkflowLayoutStore() {
57         super();
58         ContextHandler.getInstance().addListener(ContextType.CONTEXT_SHUTDOWN, this);
59         this.load();
60     }
61     
62     /**
63      * Returns the workflow layout store instance following the singleton
64      * pattern.
65      *
66      * @return Current instance
67      */

68     public static WorkflowLayoutStore getInstance() {
69         if(m_instance==null) {
70             m_instance = new WorkflowLayoutStore();
71         }
72         return m_instance;
73     }
74     
75     /**
76      * Sets the layout position of a specific workflow stage instance as
77      * part of a specific workflow.
78      *
79      * @param sWorkflowPath Full path to workflow
80      * @param sStagePath Full path to workflow stage instance
81      * @param point 2D point
82      */

83     public void setStageLayout(String JavaDoc sWorkflowPath, String JavaDoc sStagePath, Point2D.Float point) {
84         HashMap stageMap = (HashMap) this.m_workflows.get(sWorkflowPath);
85         if(stageMap==null) {
86             stageMap = new HashMap();
87             this.m_workflows.put(sWorkflowPath, stageMap);
88         }
89         stageMap.put(sStagePath, point);
90     }
91     
92     private void moveGraphsToTopLeft() {
93         Iterator itor = this.m_workflows.values().iterator();
94         while (itor.hasNext()) {
95             float minX = (float) 10000000000.0;
96             float minY = (float) 10000000000.0;
97             
98             HashMap stageMap = (HashMap) itor.next();
99             Iterator itor2 = stageMap.keySet().iterator();
100             while (itor2.hasNext()) {
101                 String JavaDoc sStagePath = (String JavaDoc) itor2.next();
102                 Point2D.Float point = (Float JavaDoc) stageMap.get(sStagePath);
103                 if(point.x<minX) {
104                     minX = point.x;
105                 }
106                 if(point.y<minY) {
107                     minY = point.y;
108                 }
109             }
110             
111             float diffX = 0;
112             float diffY = 0;
113             
114             if(minX>20) {
115                 diffX = minX-20;
116             }
117             if(minY>20) {
118                 diffY = minY-20;
119             }
120             
121             if(diffX>0 || diffY>0) {
122                 itor2 = stageMap.keySet().iterator();
123                 while (itor2.hasNext()) {
124                     String JavaDoc sStagePath = (String JavaDoc) itor2.next();
125                     Point2D.Float point = (Float JavaDoc) stageMap.get(sStagePath);
126                     point.x = point.x - diffX;
127                     point.y = point.y - diffY;
128                 }
129             }
130         }
131     }
132     
133     /**
134      * Returns the layout position for a given workflow stage instance as
135      * part of a specific workflow.
136      *
137      * @param sWorkflowPath Full path to workflow
138      * @param sStagePath Full path to workflow stage instance
139      * @return 2D point
140      */

141     public Point2D.Float getStageLayout(String JavaDoc sWorkflowPath, String JavaDoc sStagePath) {
142         Point2D.Float point = null;
143         
144         HashMap stageMap = (HashMap) this.m_workflows.get(sWorkflowPath);
145         if(stageMap!=null) {
146             point = (Float JavaDoc) stageMap.get(sStagePath);
147         }
148         
149         return point;
150     }
151     
152     /**
153      * Loads the workflow layout information from a local XML file.
154      *
155      */

156     private void load() {
157         File fXML = new File("C:\\ContentManager\\layouts.xml");
158         if(fXML.exists()) {
159             try {
160                 Document xml =
161                     DocumentBuilderFactory
162                         .newInstance()
163                         .newDocumentBuilder()
164                         .parse(
165                         new org.xml.sax.InputSource JavaDoc(new FileReader( fXML )));
166
167                 DOMXPath xpWorkflows = new DOMXPath("child::workflow");
168                 List workflows = xpWorkflows.selectNodes(xml.getDocumentElement());
169                 Iterator itor = workflows.iterator();
170                 while (itor.hasNext()) {
171                     Element elWorkflow = (Element) itor.next();
172                     DOMXPath xpStages = new DOMXPath("child::workflowstage");
173                     DOMXPath xpPath = new DOMXPath("child::path/.");
174                     String JavaDoc sWorkflowPath = xpPath.stringValueOf(elWorkflow);
175                     List stages = xpStages.selectNodes(elWorkflow);
176                     Iterator itor2 = stages.iterator();
177                     while (itor2.hasNext()) {
178                         Element elStage = (Element) itor2.next();
179                         float x = java.lang.Float.parseFloat( elStage.getAttribute("x") );
180                         float y = java.lang.Float.parseFloat( elStage.getAttribute("y") );
181                         String JavaDoc sStagePath = xpPath.stringValueOf(elStage);
182                         Point2D.Float point = new Point2D.Float(x, y);
183                         this.setStageLayout(sWorkflowPath, sStagePath, point);
184                     }
185                 }
186                 
187                 this.moveGraphsToTopLeft();
188                 
189             } catch(Exception JavaDoc e) {
190                 e.printStackTrace();
191             }
192         }
193     }
194     
195     /**
196      * Saves the workflow layout information to a local XML file.
197      *
198      */

199     private void save() {
200         try {
201             Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
202             
203             Element elRoot = xml.createElement("layouts");
204             
205             Iterator itor = this.m_workflows.keySet().iterator();
206             while (itor.hasNext()) {
207                 String JavaDoc sWorkflowPath = (String JavaDoc) itor.next();
208                 
209                 Element elWorkflow = xml.createElement("workflow");
210                 elRoot.appendChild(elWorkflow);
211                 
212                 Element elPath = xml.createElement("path");
213                 Text txt = xml.createTextNode(sWorkflowPath);
214                 elPath.appendChild(txt);
215                 elWorkflow.appendChild(elPath);
216                 elRoot.appendChild(elWorkflow);
217                 
218                 HashMap stageMap = (HashMap) this.m_workflows.get(sWorkflowPath);
219                 Iterator itor2 = stageMap.keySet().iterator();
220                 while (itor2.hasNext()) {
221                     String JavaDoc sStagePath = (String JavaDoc) itor2.next();
222                     Point2D.Float point = (Float JavaDoc) stageMap.get(sStagePath);
223                     float x = point.x;
224                     float y = point.y;
225                     
226                     Element elStage = xml.createElement("workflowstage");
227                     elWorkflow.appendChild(elStage);
228                     elStage.setAttribute("x", new java.lang.Float JavaDoc(x).toString());
229                     elStage.setAttribute("y", new java.lang.Float JavaDoc(y).toString());
230                     
231                     
232                     elPath = xml.createElement("path");
233                     txt = xml.createTextNode(sStagePath);
234                     elPath.appendChild(txt);
235                     elStage.appendChild(elPath);
236                     
237                 }
238             }
239             
240             File fXML = new File("C:\\ContentManager\\layouts.xml");
241             XMLPrettyPrint printer = new XMLPrettyPrint();
242             printer.printNodeToFile(elRoot, fXML);
243             
244         } catch (ParserConfigurationException e) {
245             e.printStackTrace();
246         } catch (FactoryConfigurationError e) {
247             e.printStackTrace();
248         }
249     }
250
251     /* (non-Javadoc)
252      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
253      */

254     public void contextMessage(ContextEvent ce) {
255         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_SHUTDOWN) {
256             this.save();
257         }
258     }
259
260 }
261
Popular Tags