001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.dnd;
020:
021: import java.awt.*;
022: import java.awt.dnd.*;
023: import java.util.*;
024:
025: import org.openharmonise.commons.xml.namespace.*;
026: import org.openharmonise.him.*;
027: import org.openharmonise.him.context.StateHandler;
028: import org.openharmonise.him.swing.resourcetree.*;
029: import org.openharmonise.him.window.messages.*;
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.context.*;
032: import org.openharmonise.vfs.metadata.*;
033: import org.openharmonise.vfs.metadata.value.*;
034: import org.openharmonise.vfs.servers.ServerList;
035: import org.openharmonise.vfs.status.*;
036: import org.openharmonise.workfloweditor.flowchart.*;
037: import org.openharmonise.workfloweditor.flowchart.shapes.*;
038: import org.openharmonise.workfloweditor.vfs.*;
039:
040: /**
041: * Drop target for workflow editor diagram. Used when dragging stages
042: * from the resource tree into the diagram.
043: *
044: * @author Matthew Large
045: * @version $Revision: 1.2 $
046: *
047: */
048: public class WorkflowDropTarget implements DropTargetListener {
049:
050: DropTarget m_target;
051: FlowChart m_chart;
052: ResourceTree m_tree;
053:
054: private boolean bDisplayErrorMessage = true;
055:
056: private StageShape m_stageShape = null;
057:
058: /**
059: * Constructs a new workflow drop target.
060: *
061: * @param chart Workflow diagram
062: * @param tree Resource tree
063: */
064: public WorkflowDropTarget(FlowChart chart, ResourceTree tree) {
065: m_chart = chart;
066: m_tree = tree;
067: m_target = new DropTarget(m_chart, this );
068: }
069:
070: /* (non-Javadoc)
071: * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
072: */
073: public void dragEnter(DropTargetDragEvent arg0) {
074: }
075:
076: /* (non-Javadoc)
077: * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
078: */
079: public void dragOver(DropTargetDragEvent dtde) {
080: Point pt = dtde.getLocation();
081: if (this .m_stageShape == null) {
082: VirtualFile vfFile = m_tree.getSelectedResource();
083:
084: VFSWorkflowModel model = (VFSWorkflowModel) m_chart
085: .getModel();
086:
087: boolean bDuplicate = false;
088:
089: Iterator itor = model.getWorkflowStages().iterator();
090: while (itor.hasNext()) {
091: VFSWorkflowStage stage = (VFSWorkflowStage) itor.next();
092: VirtualFile stageFile = ServerList.getInstance()
093: .getHarmoniseServer().getVFS().getVirtualFile(
094: stage.getPath()).getResource();
095: if (stageFile != null
096: && stageFile.getFileName().equals(
097: vfFile.getFileName())) {
098: bDuplicate = true;
099: }
100: }
101:
102: if (!bDuplicate) {
103: AbstractVirtualFileSystem vfs = vfFile.getVFS();
104:
105: VFSWorkflowStageDefinition stageDefn = new VFSWorkflowStageDefinition(
106: vfFile, (VFSWorkflowModel) this .m_chart
107: .getModel());
108: VirtualFile vfStagesCollection = model
109: .getStagesCollection();
110: String sNewPath = vfStagesCollection.getFullPath()
111: + "/" + vfFile.getFileName();
112:
113: VirtualFile vfNewFile = new VirtualFile(sNewPath);
114: vfNewFile.setContent(null);
115: if (vfs.addVirtualFile(sNewPath, vfNewFile) != null) {
116: VersionedVirtualFile vfStageFile = (VersionedVirtualFile) vfs
117: .getVirtualFile(sNewPath).getResource();
118: PropertyInstance propInst = vfStageFile
119: .getProperty(NamespaceType.OHRM.getURI(),
120: "WORKFLOW_RELN");
121: ResourceValue value = (ResourceValue) propInst
122: .getNewValueInstance();
123: value.setValue(stageDefn.getPath());
124: propInst.addValue(value);
125:
126: PropertyInstance propInst2 = vfStageFile
127: .getProperty(NamespaceType.OHRM.getURI(),
128: "WORKFLOW_MAND");
129: BooleanValue value2 = (BooleanValue) propInst2
130: .getNewValueInstance();
131: value2.setValue(true);
132: propInst2.addValue(value2);
133:
134: StatusData status = vfStageFile.sync();
135:
136: StateHandler.getInstance().addWait("WORKFLOWSAVE");
137: try {
138: itor = model.getWorkflowStages().iterator();
139:
140: while (itor.hasNext()) {
141: VFSWorkflowStage stage = (VFSWorkflowStage) itor
142: .next();
143: VersionedVirtualFile vfFile2 = (VersionedVirtualFile) vfs
144: .getVirtualFile(stage.getPath())
145: .getResource();
146: if (vfFile2.isChanged()) {
147: StatusData status2 = vfFile2.sync();
148: status.addStatusData(status2);
149: if (status2.isOK()) {
150: //vfFile = (VersionedVirtualFile) vfs.getVirtualFile(stage.getPath());
151: if (vfFile2.getState() == VirtualFile.STATE_LIVE
152: && vfFile2
153: .getPendingVersionPath() != null) {
154: VersionedVirtualFile vfPending = (VersionedVirtualFile) vfs
155: .getVirtualFile(
156: vfFile2
157: .getPendingVersionPath())
158: .getResource();
159: if (vfPending != null) {
160: vfPending.checkin();
161: }
162: } else if (vfFile2.getState() == VirtualFile.STATE_PENDING) {
163: vfFile2.checkin();
164: }
165: } else {
166: //System.err.println("STAGE SYNC FAILED!!!!!!");
167: }
168: }
169: }
170: } catch (Exception e) {
171: e.printStackTrace();
172: } finally {
173: StateHandler.getInstance().removeWait(
174: "WORKFLOWSAVE");
175: }
176:
177: if (status.isOK()) {
178: vfStageFile = (VersionedVirtualFile) vfs
179: .getVirtualFile(sNewPath).getResource();
180: StatusData status2 = vfStageFile.checkin();
181: if (status2.isOK()) {
182: vfStageFile = (VersionedVirtualFile) vfs
183: .getVirtualFile(sNewPath)
184: .getResource();
185:
186: VFSWorkflowStage stage = new VFSWorkflowStage(
187: model, vfStageFile);
188: stage.setDefinition(stageDefn);
189:
190: this .m_chart.getModel().addWorkflowStage(
191: stage);
192:
193: m_stageShape = new StageShape(100, 100,
194: stage);
195: this .m_chart.addShape(m_stageShape);
196: }
197: }
198:
199: }
200: this .m_stageShape.setX(pt.x);
201: this .m_stageShape.setY(pt.y);
202: } else {
203: if (bDisplayErrorMessage) {
204: MessageHandler
205: .getInstance()
206: .fireMessageEvent(
207: "You cannot have two Workflow Stages with the same name in a single Workflow.",
208: MessageHandler.TYPE_ERROR);
209: this .bDisplayErrorMessage = false;
210: }
211: }
212:
213: }
214: this .m_chart.repaint();
215: }
216:
217: /* (non-Javadoc)
218: * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
219: */
220: public void dropActionChanged(DropTargetDragEvent arg0) {
221: }
222:
223: /* (non-Javadoc)
224: * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
225: */
226: public void drop(DropTargetDropEvent dtde) {
227: this .m_stageShape = null;
228: this .bDisplayErrorMessage = true;
229: }
230:
231: /* (non-Javadoc)
232: * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
233: */
234: public void dragExit(DropTargetEvent arg0) {
235: }
236:
237: }
|