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

📄 graphmanager.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * @(#)GraphEd.java 3.3 23-APR-04
 * 
 * Copyright (c) 2001-2004, Gaudenz Alder All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *  
 */
package flow.graph.gui.graph;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.TransferHandler;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.UndoableEditEvent;

import org.jgraph.JGraph;
import org.jgraph.event.GraphModelEvent;
import org.jgraph.event.GraphModelListener;
import org.jgraph.event.GraphSelectionEvent;
import org.jgraph.event.GraphSelectionListener;
import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.BasicMarqueeHandler;
import org.jgraph.graph.CellHandle;
import org.jgraph.graph.CellView;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Edge;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphContext;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.GraphUndoManager;
import org.jgraph.graph.Port;
import org.jgraph.graph.PortView;
import org.jgraph.graph.VertexView;

import flow.graph.app.FlowManager;
import flow.graph.db.bean.DataBaseNode;
import flow.graph.gui.graph.cell.BPELFlag;
import flow.graph.gui.graph.cell.EditBPEL;
import flow.graph.gui.graph.cell.FlowGraphCell;
import flow.graph.gui.graph.encoder.XMLEncoder;
import flow.graph.gui.graph.file.GraphEncoder;
import flow.graph.gui.graph.item.GraphCodeFormat;
import flow.graph.gui.graph.lua.GraphTemplateCell;
import flow.graph.gui.graph.lua.gui.BlankForm;
import flow.graph.gui.graph.lua.gui.CaseForm;
import flow.graph.gui.graph.lua.gui.ExtendsForm;
import flow.graph.gui.graph.lua.gui.WhileForm;
import flow.graph.gui.graph.lua.xml.TemplateConstants;
import flow.graph.gui.graph.template.TemplateFactory;
import flow.graph.gui.graph.template.TemplateForm;
import flow.graph.gui.tools.JButtonGroup;
import flow.graph.gui.tools.JGroupContainer;
import flow.graph.gui.tools.JGroupPanel;
import flow.graph.gui.tools.SelectButton;
import flow.graph.gui.tools.SimpleButton;
import flow.graph.gui.tree.BusinessNode;
import flow.graph.gui.tree.ServerTreeView;
import flow.graph.test.EditorGraphUI;
import flow.graph.test.MyFrame;
import flow.graph.test.TestBean;
import flow.graph.util.Encoder;
import flow.graph.gui.graph.cell.bpel.Process;

public class GraphManager extends JPanel implements GraphSelectionListener, KeyListener, ChangeListener {
	// JGraph instance
	protected JGraph graph;
	
	protected BusinessNode businessNode;
	
	// Undo Manager
	protected GraphUndoManager undoManager;
	
	protected JScrollPane graphScrollPane;

	// Actions which Change State
	//protected Action undo, redo, remove, group, ungroup, tofront, toback, cut, copy, paste;

	//protected SelectButton shapeArc, shapeStart, shapeStop, shapeEclipse, shapeRectangle, shapeRoundRectangle, shapeDiamond, shapeTriangle;
	protected SelectButton shapeArc, shapeStart, shapeStop, shapeWhile, shapeInvoke, shapeAssign, shapeSwitch, shapeModel, shapeTriangle, shapeBlank, shapeGroup, shapeBreak, shapeContinue;
	protected SimpleButton undoButton, redoButton, deleteButton, groupButton, ungroupButton, frontButton, backButton, cutButton, copyButton, pasteButton, templateButton;
	// cell count that gets put in cell label
	protected int cellCount = 0;

	protected JButtonGroup ToolBarButtonGroup = new JButtonGroup();

	// Status Bar
	protected StatusBarGraphListener statusBar;

	//背景图片
	private ImageIcon backgroundImage;
	
	protected Vector cacheVector = new Vector();
	
	//是否已经保存
	//true:已经保存;false:没有保存
	private boolean isSaved = false;
	
	// Construct an Editor Panel
	public GraphManager() {
		backgroundImage = new ImageIcon(GraphManager.class.getResource("images/background2.gif"));

		// Construct the Graph
		graph = createGraph();
		// Use a Custom Marquee Handler
		graph.setMarqueeHandler(createMarqueeHandler());
		// Construct Command History
		//
		// Create a GraphUndoManager which also Updates the ToolBar
		undoManager = new GraphUndoManager() {
			// Override Superclass
			public void undoableEditHappened(UndoableEditEvent e) {
				// First Invoke Superclass
				super.undoableEditHappened(e);
				// Then Update Undo/Redo Buttons
				updateHistoryButtons();
			}
		};
		populateContentPane();		
		installListeners(graph);
	}

	
	protected void paintComponent(Graphics g){ 
        setOpaque(false); 
        super.paintComponent(g); 

        Dimension d = getSize(); 
        for (int x = 0; x < d.width; x += backgroundImage.getIconWidth()) { 
            for (int y = 0; y < d.height; y += backgroundImage.getIconHeight()) { 
                g.drawImage(backgroundImage.getImage(), x, y, null, null); 
            } 
        }
	}
	
						  
	// Hook for subclassers
	//JGraph图形布局
	protected void populateContentPane() {
		// Use Border Layout
		this.setOpaque(false);
		setLayout(new BorderLayout());
		// Add a ToolBar
		add(createToolBar(), BorderLayout.NORTH);
		// Add the Graph as Center Component
		graphScrollPane = new JScrollPane(graph);
		//graphScrollPane.getViewport().setAutoscrolls(false);
		//graphScrollPane.set
		add(graphScrollPane, BorderLayout.CENTER);
	}

	// Hook for subclassers
	//创建JGraph类实例
	protected JGraph createGraph() {
		//JGraph graph = new MyGraph(new MyModel());//创建JGraph实例,同时创建Model实例
		GraphModel model = new MyModel();
		DefaultCellViewFactory factory = new GPCellViewFactory();
		GraphLayoutCache layoutCache = new GraphLayoutCache(model, factory, true);

		JGraph graph = new GraphExtends(model, layoutCache);
		graph.setBackgroundImage(backgroundImage);
		ToolTipManager.sharedInstance().registerComponent(graph);

		//顶点编辑模式
		//graph.setUI(new EditorGraphUI());
		return graph;
	}

	// Hook for subclassers
	//安装监听
	protected void installListeners(JGraph graph) {
		// Add Listeners to Graph
		//
		// Register UndoManager with the Model
		graph.getModel().addUndoableEditListener(undoManager);
		// Update ToolBar based on Selection Changes
		graph.getSelectionModel().addGraphSelectionListener(this);
		// Listen for Delete Keystroke when the Graph has Focus
		graph.addKeyListener(this);
		//graph.getModel().addGraphModelListener(statusBar);
	}

	// Hook for subclassers
	//卸载监听
	protected void uninstallListeners(JGraph graph) {
		graph.getModel().removeUndoableEditListener(undoManager);
		graph.getSelectionModel().removeGraphSelectionListener(this);
		graph.removeKeyListener(this);
		graph.getModel().removeGraphModelListener(statusBar);
	}

	// Hook for subclassers
	//创建鼠标动作实例
	protected BasicMarqueeHandler createMarqueeHandler() {
		return new MyMarqueeHandler();
	}

	// Insert a new Vertex at point
	public void insert(Point2D point) {
		// Construct Vertex with no Label
		DefaultGraphCell vertex = createDefaultGraphCell();
		// Create a Map that holds the attributes for the Vertex
		vertex.getAttributes().applyMap(createCellAttributes(point));
		// Insert the Vertex (including child port and attributes)
		graph.getGraphLayoutCache().insert(vertex);
	}
	
	public void graphInit(){
		insertStart(new Point2D.Float(30, 269));
		insertStop(new Point2D.Float(740, 269));
	}

	//insert start element(圆形)
	public void insertStart(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 35, 35, Color.ORANGE, true, new DefaultGraphCell("开始"), "flow.graph.gui.graph.item.GraphItemCirculStart");
		
		//设置前景图案
		//GraphConstants.setIcon(vertex.getAttributes(), new ImageIcon(GraphManager.class.getResource("images/start.gif")));
		//设置BPEL类型
		FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_0");
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.START_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
	}

	private int getID(String cellid){
		int pos = cellid.lastIndexOf("_");
		if(pos >= 0){
			return Integer.parseInt(cellid.substring(pos+1));
		}
		return 10000;
	}
	
	//insert stop element(圆形)
	public void insertStop(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 35, 35, Color.ORANGE, true, new DefaultGraphCell("结束"), "flow.graph.gui.graph.item.GraphItemCirculStop");
		
		//设置前景图案
		//GraphConstants.setIcon(vertex.getAttributes(), new ImageIcon(GraphManager.class.getResource("images/stop.gif")));
		
		//设置BPEL类型
		String cellid = getGraphID();
		if(cellid != null){
			int id = getID(cellid)+1;
			FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_"+id);
			setGraphID(id);
		}
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.STOP_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
	}

	//insert while element(椭圆)
	public Object insertWhile(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 60, 40, Color.ORANGE, true, new GraphTemplateCell("循环节点"), "flow.graph.gui.graph.item.GraphItemRoundRectangle");
		
		//设置BPEL类型
		String cellid = getGraphID();
		if(cellid != null){
			int id = getID(cellid)+1;
			FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_"+id);
			setGraphID(id);
		}
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.WHILE_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		
		DefaultGraphCell breakcell = (DefaultGraphCell)insertBreak(new Point2D.Float((int)point.getX()+120, (int)point.getY()));
		DefaultGraphCell group = (DefaultGraphCell)insertGroup(new Object[]{breakcell});
		if(group != null){
			connect(vertex, group);
		}
		return vertex;
	}

	//insert while element(椭圆)
	public Object insertBreak(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 50, 35, Color.ORANGE, true, new GraphTemplateCell(""), "flow.graph.gui.graph.item.GraphItemBreak");
		
		//设置BPEL类型
		String cellid = getGraphID();
		if(cellid != null){
			int id = getID(cellid)+1;
			FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_"+id);
			setGraphID(id);
		}
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.BREAK_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);

⌨️ 快捷键说明

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