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

📄 svgmodeldigester.java

📁 一个java写的business process management系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 2003-2004, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.model.codegen.svg;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import net.orthanc.flow4j.base.FigureConsts;
import net.orthanc.flow4j.base.Util;
import net.orthanc.flow4j.designer.figures.AnchorRegistry;
import net.orthanc.flow4j.model.AbstractModelDigester;
import net.orthanc.flow4j.model.bind.AbstractTransitionFlowletBind;
import net.orthanc.flow4j.model.bind.BendpointBind;
import net.orthanc.flow4j.model.bind.BooleanTransitionBind;
import net.orthanc.flow4j.model.bind.CallFlowletBind;
import net.orthanc.flow4j.model.bind.DecisionFlowletBind;
import net.orthanc.flow4j.model.bind.EndFlowletBind;
import net.orthanc.flow4j.model.bind.FlowModelBind;
import net.orthanc.flow4j.model.bind.FlowletLabelBind;
import net.orthanc.flow4j.model.bind.IFlowletBind;
import net.orthanc.flow4j.model.bind.JavaTaskFlowletBind;
import net.orthanc.flow4j.model.bind.JoinFlowletBind;
import net.orthanc.flow4j.model.bind.JumpFlowletBind;
import net.orthanc.flow4j.model.bind.ScriptTaskFlowletBind;
import net.orthanc.flow4j.model.bind.StartFlowletBind;
import net.orthanc.flow4j.model.bind.TemplateFlowletBind;
import net.orthanc.flow4j.model.bind.TransitionBind;
import net.orthanc.flow4j.runtime.ITaskFlowlet;
import net.orthanc.flow4j.runtime.descriptors.TaskPropertyDescriptor;
import net.orthanc.flow4j.runtime.descriptors.TaskPropertyDescriptors;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.ext.awt.g2d.AbstractGraphics2D;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.fop.svg.PDFTranscoder;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * @author greifa
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SVGModelDigester extends AbstractModelDigester {

	static private final Color FLOWLET_FILL_COLOR = new Color(193, 213, 255);
	static private final Color FLOWLET_BORDER_COLOR = new Color(105, 140, 255);

	static private final BasicStroke SINGLE_STROKE = new BasicStroke(1);
	static private final BasicStroke DOUBLE_STROKE = new BasicStroke(2);

	static private SAXParser saxParser;

	private ClassLoader taskClassLoader;

	private SVGGraphics2D svgGenerator;
	private Dimension canvasSize = new Dimension(100, 100);

	private Map flowletIdToFlowletBindMapping = new HashMap();

	/**
	 * Constructor
	 *
	 */
	public SVGModelDigester(ClassLoader taskClassLoader)throws ParserConfigurationException, SAXException {
		this.taskClassLoader = taskClassLoader;
		// Get a DOMImplementation
		DOMImplementation domImpl =
			GenericDOMImplementation.getDOMImplementation();

		// Create an instance of org.w3c.dom.Document
		Document document = domImpl.createDocument(null, "svg", null);

		if (saxParser == null)
			try {
				SAXParserFactory spFactory = SAXParserFactory.newInstance();
				saxParser = spFactory.newSAXParser();
			} catch (FactoryConfigurationError e) {
				throw e;
			} catch (ParserConfigurationException e) {
				throw e;
			} catch (SAXException e) {
				throw e;
			}

		// Create an instance of the SVG Generator
		svgGenerator = new SVGGraphics2D(document);
	}


	/**
	 * TODO
	 * @return
	 */
	public SVGGraphics2D getSVGGenerator() {
		return svgGenerator;
	}

	/**
	 * Paints the flowlets shape. fills the polygon and draws the outline.
	 * @param g
	 * @param pointList
	 */
	private void paintFlowletFigure(SVGGraphics2D g, List pointList) {
		g.setPaint(FLOWLET_FILL_COLOR);
		Polygon poly = new Polygon();
		for (Iterator iter = pointList.iterator(); iter.hasNext();) {
			Point point = (Point) iter.next();
			poly.addPoint(point.x, point.y);
		}

		g.fillPolygon(poly);

		g.setStroke(DOUBLE_STROKE);
		g.setPaint(FLOWLET_BORDER_COLOR);
		g.drawPolygon(poly);
	}

	private Point getTopLeft(Rectangle rect) {
		return new Point(rect.getLocation());
	}
	private Point getTop(Rectangle rect) {
		return new Point(rect.x + rect.width / 2, rect.y);
	}
	private Point getBottom(Rectangle rect) {
		return new Point(rect.x + rect.width / 2, rect.y + rect.height);
	}
	private Point getLeft(Rectangle rect) {
		return new Point(rect.x, rect.y + rect.height / 2);
	}
	private Point getRight(Rectangle rect) {
		return new Point(rect.x + rect.width, rect.y + rect.height / 2);
	}
	private Point getTopRight(Rectangle rect) {
		return new Point(rect.x + rect.width, rect.y);
	}
	private Point getBottomRight(Rectangle rect) {
		return new Point(rect.x + rect.width, rect.y + rect.height);
	}
	private Point getBottomLeft(Rectangle rect) {
		return new Point(rect.x, rect.y + rect.height);
	}

	private void adjustCanvasSize(Point point) {
		if (point.x > canvasSize.width)
			canvasSize.setSize(point.x, canvasSize.height);
		if (point.y > canvasSize.height)
			canvasSize.setSize(canvasSize.width, point.y);
	}


	/* (non-Javadoc)
	 * @see net.orthanc.flow4j.model.AbstractModelDigester#digestStartFlowlet(net.orthanc.flow4j.model.bind.StartFlowletBind, net.orthanc.flow4j.model.bind.FlowModelBind)
	 */
	protected void digestStartFlowlet(
		StartFlowletBind flowletBind,
		FlowModelBind flowModelBind) {
			int w = FigureConsts.DIMENSION_FIGURE_START.width;
			int h = FigureConsts.DIMENSION_FIGURE_START.height;
			Rectangle rect = new Rectangle(w, h);
			rect.translate(flowletBind.getX(), flowletBind.getY());
			adjustCanvasSize(getBottomRight(rect));

			List points = new ArrayList();
			points.add(getTopLeft(rect));
			points.add(getBottom(rect));
			points.add(getTopRight(rect));

			paintFlowletFigure(svgGenerator, points);
			drawLabel(svgGenerator, flowletBind.getLabel());
			flowletIdToFlowletBindMapping.put(flowletBind.getId(flowModelBind), flowletBind);
	}



	/* (non-Javadoc)
	 * @see net.orthanc.flow4j.model.AbstractModelDigester#digestJavaTaskFlowlet(net.orthanc.flow4j.model.bind.JavaTaskFlowletBind, net.orthanc.flow4j.model.bind.FlowModelBind)
	 */
	protected void digestJavaTaskFlowlet(
		JavaTaskFlowletBind flowletBind,
		FlowModelBind flowModelBind) {
			int w = FigureConsts.DIMENSION_FIGURE_JAVATASK.width;
			int h = FigureConsts.DIMENSION_FIGURE_JAVATASK.height;
			Rectangle rect = new Rectangle(w, h);
			rect.translate(flowletBind.getX(), flowletBind.getY());
			adjustCanvasSize(getBottomRight(rect));

			List points = new ArrayList();
			points.add(getTopLeft(rect));
			points.add(getBottomLeft(rect));
			points.add(getBottomRight(rect));
			points.add(getTopRight(rect));

			paintFlowletFigure(svgGenerator, points);
			String className = flowletBind.getLabel().getText();
			String labelText;
			Color color = Color.BLACK;
			try {
				Class taskClass =
					taskClassLoader == null
						? Class.forName(className)
						: Class.forName(className, true, taskClassLoader);
				ITaskFlowlet taskFlowlet = (ITaskFlowlet) taskClass.newInstance();

				//	determine the parameterized task name
				//	properties
				TaskPropertyDescriptors descs =
					taskFlowlet.getPropertyDescriptors();
				Map taskProperties = new HashMap();
				if (descs != null && !descs.isEmpty()) {
					for (Iterator iter = descs.iterator(); iter.hasNext();) {
						TaskPropertyDescriptor desc =
							(TaskPropertyDescriptor) iter.next();
						String value = flowletBind.getProperty(desc.getName());
						taskProperties.put(desc.getName(), value);
					}
				}
				//	set the parameterized task name
				labelText =
					Util.replaceProperties(taskFlowlet.getName(), taskProperties);
				if (labelText == null)
					labelText = className;
			} catch (Exception e) {
				if (! (e instanceof ClassNotFoundException))
					e.printStackTrace();
				labelText = className;
				color = Color.RED;
			}

			int x = flowletBind.getLabel().getX();
			int y = flowletBind.getLabel().getY();
			drawLabel(svgGenerator, labelText, x, y, color);
			flowletIdToFlowletBindMapping.put(flowletBind.getId(flowModelBind), flowletBind);
	}

	/* (non-Javadoc)
	 * @see net.orthanc.flow4j.model.AbstractModelDigester#digestScriptTaskFlowlet(net.orthanc.flow4j.model.bind.ScriptTaskFlowletBind, net.orthanc.flow4j.model.bind.FlowModelBind)
	 */
	protected void digestScriptTaskFlowlet(
		ScriptTaskFlowletBind flowletBind,
		FlowModelBind flowModelBind) {
			int w = FigureConsts.DIMENSION_FIGURE_SCRIPTTASK.width;
			int h = FigureConsts.DIMENSION_FIGURE_SCRIPTTASK.height;
			Rectangle rect = new Rectangle(w, h);
			rect.translate(flowletBind.getX(), flowletBind.getY());
			adjustCanvasSize(getBottomRight(rect));

			List points = new ArrayList();
			points.add(getTopLeft(rect));
			points.add(getBottomLeft(rect));
			points.add(getBottomRight(rect));
			points.add(getTopRight(rect));

			paintFlowletFigure(svgGenerator, points);
			Font origFont = svgGenerator.getFont();
			Font font = new Font(origFont.getName(), origFont.getStyle(), 36);
			svgGenerator.setFont(font);
			drawLabel(svgGenerator, "S", getTopLeft(rect).x+13, getTopLeft(rect).y-3, FLOWLET_BORDER_COLOR);
			svgGenerator.setFont(origFont);

			String filePath = flowletBind.getLabel().getText();
			int x = flowletBind.getLabel().getX();
			int y = flowletBind.getLabel().getY();
			drawLabel(svgGenerator, filePath.trim(), x, y, Color.BLACK);
			flowletIdToFlowletBindMapping.put(flowletBind.getId(flowModelBind), flowletBind);
	}

	/* (non-Javadoc)
	 * @see net.orthanc.flow4j.model.AbstractModelDigester#digestDecisionFlowlet(net.orthanc.flow4j.model.bind.DecisionFlowletBind, net.orthanc.flow4j.model.bind.FlowModelBind)
	 */
	protected void digestDecisionFlowlet(
		DecisionFlowletBind flowletBind,
		FlowModelBind flowModelBind) {
			int w = FigureConsts.DIMENSION_FIGURE_DECISION.width;
			int h = FigureConsts.DIMENSION_FIGURE_DECISION.height;
			Rectangle rect = new Rectangle(w, h);
			rect.translate(flowletBind.getX(), flowletBind.getY());
			adjustCanvasSize(getBottomRight(rect));

			List points = new ArrayList();
			points.add(getTop(rect));
			points.add(getLeft(rect));
			points.add(getBottom(rect));
			points.add(getRight(rect));

			paintFlowletFigure(svgGenerator, points);
			String desc = flowletBind.getDescription();
			if (desc != null && desc.trim().length() != 0) {

⌨️ 快捷键说明

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