All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yaoqiang.bpmn.editor.simulation.SimulationExecution Maven / Gradle / Ivy

package org.yaoqiang.bpmn.editor.simulation;

import java.util.ArrayList;
import java.util.List;

import org.yaoqiang.bpmn.editor.swing.BPMNGraphComponent;
import org.yaoqiang.bpmn.engine.operation.ExecutionOperation;
import org.yaoqiang.bpmn.engine.operation.FlowNodeEnd;
import org.yaoqiang.bpmn.engine.operation.FlowNodeExecute;
import org.yaoqiang.bpmn.engine.operation.ProcessEnd;
import org.yaoqiang.bpmn.engine.operation.SequenceFlowDestroyScope;
import org.yaoqiang.bpmn.engine.runtime.Execution;
import org.yaoqiang.bpmn.model.elements.core.common.FlowNode;
import org.yaoqiang.bpmn.model.elements.gateways.ParallelGateway;

import com.mxgraph.model.mxGraphModel;

/**
 * SimulationExecution
 * 
 * @author Shi Yaoqiang([email protected])
 */
public class SimulationExecution extends Execution {

	protected BPMNGraphComponent graphComponent;

	protected FlowNode runningNode;

	protected static mxGraphModel model;

	protected static String oldStyle;

	protected static Object lastNode;

	protected static List nodes = new ArrayList();

	public SimulationExecution() {
	}

	public SimulationExecution(BPMNGraphComponent graphComponent) {
		this.graphComponent = graphComponent;
		model = graphComponent.getGraph().getModel();
	}

	public SimulationExecution(FlowNode node) {
		super(node);
	}

	protected Execution newExecution() {
		SimulationExecution execution = new SimulationExecution(graphComponent);
		execution.setRunningNode(getRunningNode());
		execution.setOldStyle(getOldStyle());
		this.setRunningNode(null);
		this.setOldStyle(null);
		return execution;
	}

	public void performOperation(final ExecutionOperation executionOperation) {
		beforePerformOperation(executionOperation);
		super.performOperation(executionOperation);

	}

	private void beforePerformOperation(ExecutionOperation executionOperation) {
		FlowNode node = getFlowNode();
		if ((executionOperation instanceof FlowNodeExecute) && node != null && node != runningNode) {
			if (runningNode != null) {
				runningNode = null;
				// unHighLightNode(runningNode);
			}
			if (node instanceof ParallelGateway) {
				if (node.getIncomingSequenceFlows().size() < node.getOutgoingSequenceFlows().size()) {
					collectHighLightNode(node);
					runningNode = node;
					// JOptionPane.showMessageDialog(null, "Running Node : " + runningNode.getName());
				}
			} else {
				collectHighLightNode(node);
				runningNode = node;
				// JOptionPane.showMessageDialog(null, "Running Node : " + runningNode.getName());
			}
		} else if ((executionOperation instanceof SequenceFlowDestroyScope) && node != null && node != runningNode && (node instanceof ParallelGateway)) {
			if (node.getIncomingSequenceFlows().size() > node.getOutgoingSequenceFlows().size()) {
				collectHighLightNode(node);
				runningNode = node;
				// JOptionPane.showMessageDialog(null, "Running Node : " + runningNode.getName());
			}

		}
		if (runningNode != null && (executionOperation instanceof ProcessEnd || executionOperation instanceof FlowNodeEnd)) {
			// unHighLightNode(runningNode);
			runningNode = null;
		}
	}

	private void collectHighLightNode(FlowNode node) {
		Object cell = model.getCell(node.getId());
		if (cell != null) {
			nodes.add(cell);
		}
	}

	public void highLightNode(Object cell) {
		if (cell != null) {
			lastNode = cell;
			oldStyle = model.getStyle(cell);
			model.setStyle(cell, oldStyle + ";strokeColor=#00FF00;fillColor=#FFFF00;gradientColor=#CFCF00");
		}
	}

	public void unHighLightNode(Object cell) {
		if (cell != null && oldStyle != null) {
			model.setStyle(cell, oldStyle);
			oldStyle = null;
		}
	}

	public static void reset() {
		nodes.clear();
		if (lastNode != null && oldStyle != null) {
			model.setStyle(lastNode, oldStyle);
			oldStyle = null;
		}
	}

	public static List getNodes() {
		return nodes;
	}

	public FlowNode getRunningNode() {
		return runningNode;
	}

	public void setRunningNode(FlowNode runningNode) {
		this.runningNode = runningNode;
	}

	public String getOldStyle() {
		return oldStyle;
	}

	public void setOldStyle(String oldStyle) {
		SimulationExecution.oldStyle = oldStyle;
	}

}