⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flowdiagram.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package cn.myapps.core.workflow.element;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.ImageIcon;

import netscape.javascript.JSObject;
import cn.myapps.core.macro.runner.JavaScriptRunner;
import cn.myapps.core.workflow.applet.BFApplet;
import cn.myapps.core.workflow.utility.CommonUtil;
import cn.myapps.core.workflow.utility.FlowType;
import cn.myapps.core.workflow.utility.Sequence;
import cn.myapps.util.StringUtil;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class FlowDiagram extends Canvas {

	private static HashMap IMG_RESOURCE = new HashMap(10);

	private int _statues; // 鼠标状态

	public static final int ACTION_NORMAL = 0x00000000;

	public static final int ACTION_REMOVE = 0x00000001;

	public static final int ACTION_ADD_ABORTNODE = 0x00000010;

	public static final int ACTION_ADD_AUTONODE = 0x00000011;

	public static final int ACTION_ADD_COMPLETENODE = 0x00000012;

	public static final int ACTION_ADD_MANUALNODE = 0x00000013;

	public static final int ACTION_ADD_STARTNODE = 0x00000014;

	public static final int ACTION_ADD_SUSPENDNODE = 0x00000015;

	public static final int ACTION_ADD_TERMINATENODE = 0x00000016;

	public static final int ACTION_ADD_RELATION = 0x00001000;

	public static final int ACTION_EDIT_NODE = 0x10000010;

	public static final int ACTION_EDIT_RELATION = 0x10001000;

	public static final int ACTION_BREAK_LINE = 0x00100000;

	private Vector _elems = new Vector();

	private PaintElement _selected;// **

	private Element _currToEdit;// **

	Image _tmpimg;

	public int flowstatus = FlowType.FLOWSTATUS_OPEN_NOSTART;

	public String flowpath = "";

	public String deleteMSG;

	public int width = 2 * 1024;

	public int height = 2 * 768;

	private boolean _changed = false;

	private double _zoomrate = 1;

	JSObject win = null;

	/**
	 * @roseuid 3E0428D90248
	 */
	public FlowDiagram() {
		// _tmpimg = this.createImage(this.size().width, this.size().height);
		// if (this.getSize().width <= 0 || this.getSize().height <= 0) {
		this.setSize(width, height);
		// }
		_tmpimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		_statues = ACTION_NORMAL;

		try {
			jbInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Image getImageResource(String filename) {
		Object obj = IMG_RESOURCE.get(filename);
		Image img = null;
		if (obj == null) {
			try {
				ImageIcon icon = new ImageIcon(BFApplet.class
						.getResource(filename));
				obj = icon.getImage();
			} catch (Exception e) {
				obj = Toolkit.getDefaultToolkit().createImage(new byte[0]);
			}
			IMG_RESOURCE.put(filename, obj);
		}
		img = (Image) obj;
		return img;
	}

	public void setJSObject(JSObject win) {
		this.win = win;
	}

	public int get_statues() {
		return this._statues;
	}

	// 编辑时用到的接口
	public Element getCurrToEdit() {
		return _currToEdit;
	}

	public boolean getChanged() {
		return this._changed;
	}

	public void editManualNode(ManualNode grp, String id, String name,
			String namelist, String note, String remaindertype,
			String beforetime, String passcondition, String exceedaction,
			String limittimecount, String backnodeid, String formname,
			String fieldpermlist, boolean issplit, boolean isgather, int x,
			int y) {
		grp.id = id;
		grp.name = name;
		grp.note = note;
		grp.namelist = namelist;

		grp.note = note;
		grp.remaindertype = remaindertype;
		grp.beforetime = beforetime;
		grp.passcondition = passcondition;
		grp.exceedaction = exceedaction;
		grp.limittimecount = limittimecount;
		grp.backnodeid = backnodeid;

		// grp.properties = props;
		grp.formname = formname;
		grp.fieldpermlist = fieldpermlist;
		grp.issplit = issplit;
		grp.isgather = isgather;
		grp.x = x;
		grp.y = y;

		this._selected = grp;
	}

	// the StartNode edit Process method

	public void editStartNode(StartNode sn, String id, String name, int x, int y) {

		sn.id = id;
		sn.name = name;
		sn.x = x;
		sn.y = y;

		this._selected = sn;
	}

	public void editAbortNode(AbortNode sn, String id, String name, int x, int y) {

		sn.id = id;
		sn.name = name;
		sn.x = x;
		sn.y = y;

		this._selected = sn;
	}

	public void editCompleteNode(CompleteNode sn, String id, String name,
			int x, int y) {

		sn.id = id;
		sn.name = name;
		sn.x = x;
		sn.y = y;

		this._selected = sn;
	}

	public void editTerminateNode(TerminateNode sn, String id, String name,
			int x, int y) {

		sn.id = id;
		sn.name = name;
		sn.x = x;
		sn.y = y;

		this._selected = sn;
	}

	public void editSuspendNode(SuspendNode sn, String id, String name, int x,
			int y) {

		sn.id = id;
		sn.name = name;
		sn.x = x;
		sn.y = y;

		this._selected = sn;
	}

	// the AutoNode class process method

	public void editAutoNode(AutoNode an, String id, String name, int x, int y) {

		an.id = id;
		an.name = name;
		an.x = x;
		an.y = y;

		this._selected = an;
	}

	/**
	 * @param id
	 * @param name
	 * @param note
	 * @roseuid 3E0406A90239
	 */
	public void editRelation(Relation rlt, String id, String name,
			String condition, String note, String action, String validateScript) {
		rlt.id = id;
		rlt.name = name;
		rlt.condition = condition;
		rlt.note = note;
		rlt.action = action;
		rlt.validateScript = validateScript;
		this._selected = rlt;
	}

	public boolean isCurrentSelected(Element em) {
		if (em != null && _selected != null && em.equals(_selected)) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isCurrentToEdit(Element em) {
		if (em != null && _currToEdit != null && em.equals(_currToEdit)) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 获取指定结点前的所有结点(踢除指定结点)
	 */
	public Vector getAllBeforeNode(Node node, boolean ispassed) {
		Vector all = getAllNodeBeforeNode(null, node, ispassed);
		if (isContain(all, node)) {
			all.removeElement(node);
		}
		return all;
	}

	/*
	 * 获取指定结点前的所有结点
	 */
	public Vector getAllNodeBeforeNode(Vector allnode, Node node,
			boolean ispassed) {
		if (allnode == null) {
			allnode = new Vector();
		}
		if (node == null || node instanceof StartNode) {
			return allnode;
		}

		Vector allrelation = getNodeBeforeRelation(node, ispassed);
		Enumeration enum1 = allrelation.elements();
		while (enum1.hasMoreElements()) {
			Object item = (Object) enum1.nextElement();
			if (item instanceof Relation) {
				Relation r = (Relation) item;
				Node beforeNode = getStartNode(r);
				// if(!allnode.contains(node)){
				if (!isContain(allnode, beforeNode)) {
					if (beforeNode instanceof ManualNode) {
						ManualNode tmp = (ManualNode) beforeNode;
						if ((tmp.namelist).indexOf("*") == -1) {
							allnode.addElement(beforeNode);
						}
					}
					allnode = getAllNodeBeforeNode(allnode, beforeNode,
							ispassed);
				}
			}
		}

		return allnode;
	}

	public boolean isContain(Vector all, Node beforeNode) {
		if (all != null) {
			Enumeration enum1 = all.elements();
			while (enum1.hasMoreElements()) {
				Object item = (Object) enum1.nextElement();
				if (item instanceof Node) {
					Node n = (Node) item;
					if (n.id == beforeNode.id) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public ManualNode addManualNode(String name, int x, int y) {
		ManualNode grp = new ManualNode(this);
		editManualNode(grp, Sequence.getSequence() + "", name, "", "", "", "",
				"", "", "", "", "", "", false, false, x, y);
		this._elems.addElement(grp);
		return grp;
	}

	public ManualNode createManualNode() {
		ManualNode mn = new ManualNode(this);
		return mn;
	}

	// the StartNode add process method

	public StartNode addStartNode(String name, int x, int y) {
		StartNode cn = new StartNode(this);
		editStartNode(cn, Sequence.getSequence(), name, x, y);
		this._elems.addElement(cn);
		return cn;
	}

	public AbortNode addAbortNode(String name, int x, int y) {
		AbortNode cn = new AbortNode(this);
		editAbortNode(cn, Sequence.getSequence(), name, x, y);
		this._elems.addElement(cn);
		return cn;
	}

	public CompleteNode addCompleteNode(String name, int x, int y) {
		CompleteNode cn = new CompleteNode(this);
		editCompleteNode(cn, Sequence.getSequence(), name, x, y);
		this._elems.addElement(cn);
		return cn;
	}

	public SuspendNode addSuspendNode(String name, int x, int y) {
		SuspendNode cn = new SuspendNode(this);
		editSuspendNode(cn, Sequence.getSequence(), name, x, y);
		this._elems.addElement(cn);
		return cn;
	}

	public TerminateNode addTerminateNode(String name, int x, int y) {
		TerminateNode cn = new TerminateNode(this);
		editTerminateNode(cn, Sequence.getSequence(), name, x, y);
		this._elems.addElement(cn);
		return cn;
	}

	// the AutoNode process method
	public AutoNode addAutoNode(String name, int x, int y) {
		AutoNode an = new AutoNode(this);
		editAutoNode(an, Sequence.getSequence(), name, x, y);
		this._elems.addElement(an);
		return an;
	}

	public void addRelation(String name, String condition, String note,
			String action, String validateScript) {
		Relation rlt = new Relation(this);
		editRelation(rlt, Sequence.getSequence(), name, condition, note,
				action, validateScript);
		this._elems.addElement(rlt);
	}

	/**
	 * @param id
	 * @roseuid 3E0406950172
	 */
	public void delActor(String id) {
		delElement(id);
	}

	/**
	 * @param id
	 * @roseuid 3E0406B003D4
	 */
	public void delRelation(String id) {
		delElement(id);
	}

	/**
	 * 改变鼠标状态
	 * 
	 * @param statues
	 * @roseuid 3E0A6E1A0258
	 */
	public void changeStatues(int statues) {

		this._statues = statues;
		switch (_statues) {
		case ACTION_NORMAL:
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			break;
		case ACTION_REMOVE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_ABORTNODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_AUTONODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_COMPLETENODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_MANUALNODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_STARTNODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_SUSPENDNODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_ADD_TERMINATENODE:
			this.setCursor(new Cursor(Cursor.HAND_CURSOR));
			break;
		case ACTION_BREAK_LINE: // add by gusd
			this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
			break; // end

		default:
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
	}

	public void zoomIn() {
		if (_zoomrate * 0.9 < 0.5) {
			return;
		}

		_zoomrate *= 0.9;

		this.getGraphics().clearRect(0, 0, width, height);

		this.setSize((int) (width / _zoomrate), (int) (height / _zoomrate));

	}

	public void zoomOut() {
		if (_zoomrate / 0.9 > 2) {
			return;
		}
		_zoomrate /= 0.9;

		this.getGraphics().clearRect(0, 0, width, height);

		this.setSize((int) (width / _zoomrate), (int) (height / _zoomrate));

	}

	/**
	 * @param id
	 * @roseuid 3E0A6E1A03DF
	 */
	private void delElement(String id) {

		for (Enumeration e = _elems.elements(); e.hasMoreElements();) {
			Element em = (Element) e.nextElement();
			if (em.id != null && em.id.equals(id)) {
				_elems.removeElement(em);
			}

		}
	}

	/**
	 * @param g
	 * @roseuid 3E0A6E1B0047
	 */
	public void update(Graphics g) {
		update(g, true);
	}

	public void update(Graphics g, boolean fillWiteColor) {

		Graphics tg = _tmpimg.getGraphics();
		if (tg instanceof Graphics2D) {
			Graphics2D g2 = ((Graphics2D) tg);
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
		}

		if (fillWiteColor) {
			tg.setColor(Color.WHITE);
			tg.fillRect(0, 0, width, height);
		} else {
			tg.clearRect(0, 0, width, height);
		}

		for (Enumeration e = _elems.elements(); e.hasMoreElements();) {
			Object te = e.nextElement();
			if (te instanceof PaintElement) {
				PaintElement em = (PaintElement) te;

				em.paint(tg);
			}
		}

		paintScreen(g);

		// g.drawImage(_tmpimg, 0, 0, this);

	}

	private void paintScreen(Graphics g) {
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
				RenderingHints.VALUE_INTERPOLATION_BILINEAR);

		// int width = _tmpimg.getWidth(this);
		// int height = _tmpimg.getHeight(this);

		g.drawImage(_tmpimg, 0, 0, (int) (width / _zoomrate),
				(int) (height / _zoomrate), 0, 0, width, height, null);

		// g.

	}

	/**
	 * @param g
	 * @roseuid 3E0A6E1B0065

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -