KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > dnd > WorkflowDropTarget


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.him.dnd;
20
21 import java.awt.*;
22 import java.awt.dnd.*;
23 import java.util.*;
24
25 import org.openharmonise.commons.xml.namespace.*;
26 import org.openharmonise.him.*;
27 import org.openharmonise.him.context.StateHandler;
28 import org.openharmonise.him.swing.resourcetree.*;
29 import org.openharmonise.him.window.messages.*;
30 import org.openharmonise.vfs.*;
31 import org.openharmonise.vfs.context.*;
32 import org.openharmonise.vfs.metadata.*;
33 import org.openharmonise.vfs.metadata.value.*;
34 import org.openharmonise.vfs.servers.ServerList;
35 import org.openharmonise.vfs.status.*;
36 import org.openharmonise.workfloweditor.flowchart.*;
37 import org.openharmonise.workfloweditor.flowchart.shapes.*;
38 import org.openharmonise.workfloweditor.vfs.*;
39
40
41 /**
42  * Drop target for workflow editor diagram. Used when dragging stages
43  * from the resource tree into the diagram.
44  *
45  * @author Matthew Large
46  * @version $Revision: 1.2 $
47  *
48  */

49 public class WorkflowDropTarget implements DropTargetListener {
50
51     DropTarget m_target;
52     FlowChart m_chart;
53     ResourceTree m_tree;
54     
55     private boolean bDisplayErrorMessage = true;
56     
57     private StageShape m_stageShape = null;
58
59     /**
60      * Constructs a new workflow drop target.
61      *
62      * @param chart Workflow diagram
63      * @param tree Resource tree
64      */

65     public WorkflowDropTarget(FlowChart chart, ResourceTree tree) {
66         m_chart = chart;
67         m_tree = tree;
68         m_target = new DropTarget(m_chart, this);
69       }
70
71     /* (non-Javadoc)
72      * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
73      */

74     public void dragEnter(DropTargetDragEvent arg0) {
75     }
76
77     /* (non-Javadoc)
78      * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
79      */

80     public void dragOver(DropTargetDragEvent dtde) {
81         Point pt = dtde.getLocation();
82         if(this.m_stageShape==null) {
83             VirtualFile vfFile = m_tree.getSelectedResource();
84             
85             VFSWorkflowModel model = (VFSWorkflowModel) m_chart.getModel();
86             
87             boolean bDuplicate = false;
88             
89             Iterator itor = model.getWorkflowStages().iterator();
90             while (itor.hasNext()) {
91                 VFSWorkflowStage stage = (VFSWorkflowStage) itor.next();
92                 VirtualFile stageFile = ServerList.getInstance().getHarmoniseServer().getVFS().getVirtualFile(stage.getPath()).getResource();
93                 if(stageFile!=null && stageFile.getFileName().equals(vfFile.getFileName())) {
94                     bDuplicate = true;
95                 }
96             }
97             
98             if(!bDuplicate) {
99                 AbstractVirtualFileSystem vfs = vfFile.getVFS();
100                 
101                 VFSWorkflowStageDefinition stageDefn = new VFSWorkflowStageDefinition(vfFile, (VFSWorkflowModel)this.m_chart.getModel());
102                 VirtualFile vfStagesCollection = model.getStagesCollection();
103                 String JavaDoc sNewPath = vfStagesCollection.getFullPath() + "/" + vfFile.getFileName();
104                 
105                 VirtualFile vfNewFile = new VirtualFile(sNewPath);
106                 vfNewFile.setContent(null);
107                 if(vfs.addVirtualFile(sNewPath, vfNewFile)!=null) {
108                     VersionedVirtualFile vfStageFile = (VersionedVirtualFile) vfs.getVirtualFile(sNewPath).getResource();
109                     PropertyInstance propInst = vfStageFile.getProperty(NamespaceType.OHRM.getURI(), "WORKFLOW_RELN");
110                     ResourceValue value = (ResourceValue) propInst.getNewValueInstance();
111                     value.setValue(stageDefn.getPath());
112                     propInst.addValue(value);
113
114                     PropertyInstance propInst2 = vfStageFile.getProperty(NamespaceType.OHRM.getURI(), "WORKFLOW_MAND");
115                     BooleanValue value2 = (BooleanValue) propInst2.getNewValueInstance();
116                     value2.setValue(true);
117                     propInst2.addValue(value2);
118                     
119                     StatusData status = vfStageFile.sync();
120
121                     
122                     StateHandler.getInstance().addWait("WORKFLOWSAVE");
123                     try {
124                         itor = model.getWorkflowStages().iterator();
125                         
126                         while (itor.hasNext()) {
127                             VFSWorkflowStage stage = (VFSWorkflowStage) itor.next();
128                             VersionedVirtualFile vfFile2 = (VersionedVirtualFile) vfs.getVirtualFile(stage.getPath()).getResource();
129                             if(vfFile2.isChanged()) {
130                                 StatusData status2 = vfFile2.sync();
131                                 status.addStatusData(status2);
132                                 if( status2.isOK() ) {
133                                     //vfFile = (VersionedVirtualFile) vfs.getVirtualFile(stage.getPath());
134
if(vfFile2.getState()==VirtualFile.STATE_LIVE && vfFile2.getPendingVersionPath()!=null) {
135                                         VersionedVirtualFile vfPending = (VersionedVirtualFile) vfs.getVirtualFile(vfFile2.getPendingVersionPath()).getResource();
136                                         if(vfPending!=null) {
137                                             vfPending.checkin();
138                                         }
139                                     } else if(vfFile2.getState()==VirtualFile.STATE_PENDING) {
140                                         vfFile2.checkin();
141                                     }
142                                 } else {
143                                     //System.err.println("STAGE SYNC FAILED!!!!!!");
144
}
145                             }
146                         }
147                     } catch(Exception JavaDoc e) {
148                         e.printStackTrace();
149                     } finally {
150                         StateHandler.getInstance().removeWait("WORKFLOWSAVE");
151                     }
152                     
153                     if(status.isOK()) {
154                         vfStageFile = (VersionedVirtualFile) vfs.getVirtualFile(sNewPath).getResource();
155                         StatusData status2 = vfStageFile.checkin();
156                         if( status2.isOK() ) {
157                             vfStageFile = (VersionedVirtualFile) vfs.getVirtualFile(sNewPath).getResource();
158                         
159                             VFSWorkflowStage stage = new VFSWorkflowStage(model, vfStageFile);
160                             stage.setDefinition(stageDefn);
161                 
162                             this.m_chart.getModel().addWorkflowStage(stage);
163             
164                             m_stageShape = new StageShape(100,100, stage);
165                             this.m_chart.addShape(m_stageShape);
166                         }
167                     }
168                     
169                 }
170                 this.m_stageShape.setX(pt.x);
171                 this.m_stageShape.setY(pt.y);
172             } else {
173                 if(bDisplayErrorMessage) {
174                     MessageHandler.getInstance().fireMessageEvent("You cannot have two Workflow Stages with the same name in a single Workflow.", MessageHandler.TYPE_ERROR);
175                     this.bDisplayErrorMessage=false;
176                 }
177             }
178     
179         }
180         this.m_chart.repaint();
181     }
182
183     /* (non-Javadoc)
184      * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
185      */

186     public void dropActionChanged(DropTargetDragEvent arg0) {
187     }
188
189     /* (non-Javadoc)
190      * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
191      */

192     public void drop(DropTargetDropEvent dtde) {
193         this.m_stageShape=null;
194         this.bDisplayErrorMessage=true;
195     }
196
197     /* (non-Javadoc)
198      * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
199      */

200     public void dragExit(DropTargetEvent arg0) {
201     }
202
203 }
204
Popular Tags