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

📄 graphmanager.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		return vertex;
	}
	//insert while element(椭圆)
	public Object insertContinue(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 50, 35, Color.ORANGE, true, new GraphTemplateCell(""), "flow.graph.gui.graph.item.GraphItemContinue");
		
		//设置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.CONTINUE_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		return vertex;
	}

	//insert invoke element(矩形)
	public Object insertInvoke(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 60, 40, Color.ORANGE, true, new GraphTemplateCell("函数调用"), "flow.graph.gui.graph.item.GraphItemRectangle");

		//设置BPEL类型
		//FlowGraphConstants.setBpelType(vertex.getAttributes(), BPELFlag.BPEL_INVOKE);
		String cellid = getGraphID();
		if(cellid != null){
			int id = getID(cellid)+1;
			FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_"+id);
			setGraphID(id);
		}
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.INVOKE_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		return vertex;
	}

	public Object insertBlank(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 60, 40, Color.ORANGE, true, new GraphTemplateCell("代码补偿"), "flow.graph.gui.graph.item.GraphItemBlank");

		//设置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.BLANK_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		return vertex;
	}

	public Object insertGroup(Point2D point){
		DefaultGraphCell group = createGroupCell();
		GPCellViewFactory.setViewClass(group.getAttributes(), "flow.graph.gui.graph.item.GraphItemGroup");
		GraphConstants.setOpaque(group.getAttributes(), false);
		GraphConstants.setGroupOpaque(group.getAttributes(), false);
		group.getAttributes().applyMap(createGroupAttributes());
		FlowGraphConstants.setTemplateType(group.getAttributes(), TemplateConstants.GROUP_TEMPLATE);
		group.addPort();
		Object cell = insertInvoke(point);
		graph.getGraphLayoutCache().insertGroup(group, new Object[]{cell});
		return group;
	}

	public Object insertGroup(Object[] cells){
		DefaultGraphCell group = createGroupCell();
		GPCellViewFactory.setViewClass(group.getAttributes(), "flow.graph.gui.graph.item.GraphItemGroup");
		GraphConstants.setOpaque(group.getAttributes(), false);
		GraphConstants.setGroupOpaque(group.getAttributes(), false);
		group.getAttributes().applyMap(createGroupAttributes());
		FlowGraphConstants.setTemplateType(group.getAttributes(), TemplateConstants.GROUP_TEMPLATE);
		group.addPort();
		graph.getGraphLayoutCache().insertGroup(group, cells);
		return group;
	}

	//insert assign element(圆角矩形)
	public void insertAssign(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);
	}

	//insert switch element(钻实形)
	public Object insertSwitch(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 60, 40, Color.ORANGE, true, new GraphTemplateCell("条件判断"), "flow.graph.gui.graph.item.GraphItemDiamond");

		//设置BPEL类型
		String cellid = getGraphID();
		if(cellid != null){
			int id = getID(cellid)+1;
			System.out.println("cellid="+cellid);
			FlowGraphConstants.setCellID(vertex.getAttributes(), "LuaItem_"+id);
			setGraphID(id);
		}
		FlowGraphConstants.setTemplateType(vertex.getAttributes(), TemplateConstants.SWITCH_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		return vertex;
	}

	//insert model element(圆角矩形)
	public Object insertModel(Point2D point){
		DefaultGraphCell vertex = createModelVertex(point.getX(), point.getY(), 100, 60, Color.ORANGE, true, new GraphTemplateCell("函数模板"), "flow.graph.gui.graph.item.GraphItemModel");

		//设置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.MODEL_TEMPLATE);
		graph.getGraphLayoutCache().insert(vertex);
		return vertex;
	}

	//三角形,暂时空闲
	public void insertTriangle(Point2D point){
		DefaultGraphCell vertex = createVertex(point.getX(), point.getY(), 60, 40, Color.ORANGE, true, new DefaultGraphCell(""), "flow.graph.gui.graph.item.GraphItemTriangle");
		graph.getGraphLayoutCache().insert(vertex);
	}
	
	public String getGraphID(){
		Object[] cells = graph.getRoots();
		if(cells != null){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					if(FlowGraphConstants.getTemplateType(cell.getAttributes()) != null){
						if(FlowGraphConstants.getTemplateType(cell.getAttributes()).toString().equals(TemplateConstants.START_TEMPLATE)){
							if(FlowGraphConstants.getCellID(cell.getAttributes()) != null)
								return FlowGraphConstants.getCellID(cell.getAttributes()).toString();
						}
					}
				}
			}
		}
		return null;
	}

	public void setGraphID(int id){
		Object[] cells = graph.getRoots();
		if(cells != null){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					if(FlowGraphConstants.getTemplateType(cell.getAttributes()) != null){
						if(FlowGraphConstants.getTemplateType(cell.getAttributes()).toString().equals(TemplateConstants.START_TEMPLATE)){
							FlowGraphConstants.setCellID(cell.getAttributes(), "LuaItem_"+id);
						}
					}
				}
			}
		}
	}

	// Hook for subclassers
	public Map createCellAttributes(Point2D point) {
		Map map = new Hashtable();
		// Snap the Point to the Grid
		if (graph != null) {
			point = graph.snap((Point2D) point.clone());
		} else {
			point = (Point2D) point.clone();
		}
		// Add a Bounds Attribute to the Map
		GraphConstants.setBounds(map, new Rectangle2D.Double(point.getX(), point.getY(), 0, 0));
		// Make sure the cell is resized on insert
		GraphConstants.setResize(map, false);
		// Add a nice looking gradient background
		GraphConstants.setGradientColor(map, Color.blue);
		// Add a Border Color Attribute to the Map
		GraphConstants.setBorderColor(map, Color.black);
		// Add a White Background
		GraphConstants.setBackground(map, Color.white);
		// Make Vertex Opaque
		GraphConstants.setOpaque(map, true);
		
		return map;
	}

	// Hook for subclassers
	protected DefaultGraphCell createDefaultGraphCell() {
		DefaultGraphCell cell = new DefaultGraphCell("Cell " + new Integer(cellCount++));
		// Add one Floating Port
		cell.addPort();
		return cell;
	}

	protected DefaultGraphCell createOtherGraphCell() {
		DefaultGraphCell cell = new DefaultGraphCell("Cell " + new Integer(cellCount++));
		GPCellViewFactory.setViewClass(cell.getAttributes(), "flow.graph.gui.graph.item.JGraphRoundRectView");
		// Add one Floating Port
		cell.addPort();
		return cell;
	}

	public DefaultGraphCell createVertex(double x, double y, double w, double h, Color bg, 
				boolean raised, DefaultGraphCell cell, String viewClass) {

		// set the view class (indirection for the renderer and the editor)
		if (viewClass != null)
			GPCellViewFactory.setViewClass(cell.getAttributes(), viewClass);
		//GraphConstants.setDashPattern(cell.getAttributes(), new float[]{4, 2});
		// Set bounds
		GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x, y, w, h));
		//GraphConstants.setIcon(cell.getAttributes(), new ImageIcon(GraphManager.class.getResource("images/insert.gif")));
		GraphConstants.setInset(cell.getAttributes(), 20);
		//GraphConstants.setLabelPosition(cell.getAttributes(), new Point2D.Double(x,y+h));
		//GraphConstants.setLabelAlongEdge(cell.getAttributes(), true);
		// Set fill color
		GraphConstants.setVerticalTextPosition(cell.getAttributes(), 0);
		if (bg != null) {
			GraphConstants.setGradientColor(cell.getAttributes(), bg);
			GraphConstants.setOpaque(cell.getAttributes(), true);
		}
		
		//GraphConstants
		// Set raised border
		if (raised)
			GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
		else
			// Set black border
			GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
		cell.addPort();

		return cell;
	}

	public DefaultGraphCell createModelVertex(double x, double y, double w, double h, Color bg, 
			boolean raised, DefaultGraphCell cell, String viewClass) {

		// set the view class (indirection for the renderer and the editor)
		if (viewClass != null)
			GPCellViewFactory.setViewClass(cell.getAttributes(), viewClass);
		// Set bounds
		GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x, y, w, h));
		GraphConstants.setInset(cell.getAttributes(), 20);
		// Set fill color
		if (bg != null) {
			GraphConstants.setGradientColor(cell.getAttributes(), bg);
			GraphConstants.setOpaque(cell.getAttributes(), true);
		}

		// Set raised border
		if (raised)
			GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createRaisedBevelBorder());
		else
			// Set black border
			GraphConstants.setBorderColor(cell.getAttributes(), Color.black);

		return cell;
	}
	
	public DefaultPort getPort(DefaultGraphCell cell){
		if(cell != null){
			for(int i=0;i<cell.getChildCount();i++){
				if(cell.getChildAt(i) instanceof DefaultPort){
					return (DefaultPort)cell.getChildAt(i);
				}
			}
		}
		return null;
	}
	
	public void connect(DefaultGraphCell source, DefaultGraphCell target){
		if(getPort(source) != null && getPort(target) != null){
			connect((Port)getPort(source), (Port)getPort(target));
		}
	}
	
	
	// Insert a new Edge between source and target
	public void connect(Port source, Port target) {
		// Construct Edge with no label
		//可连
		if(((GraphExtends)graph).isFatherAndSon((DefaultPort)source, (DefaultPort)target) == true){
			//父子关系,不可连
			return;
		}
		if(((GraphExtends)graph).isDirectBrother((DefaultPort)source, (DefaultPort)target) == true){
			//亲兄弟,不可连
			return;
		}
		//System.out.println("source:"+source.getClass());
		DefaultEdge edge = createDefaultEdge();
		if (graph.getModel().acceptsSource(edge, source)
				&& graph.getModel().acceptsTarget(edge, target)) {
			// Create a Map thath holds the attributes for the edge
			edge.getAttributes().applyMap(createEdgeAttributes());
			// Insert the Edge and its Attributes
			String cellid = getGraphID();
			if(cellid != null){
				int id = getID(cellid)+1;
				FlowGraphConstants.setCellID(edge.getAttributes(), "LuaItem_"+id);
				setGraphID(id);
			}
			FlowGraphConstants.setTemplateType(edge.getAttributes(), TemplateConstants.WHILE_TEMPLATE);
			graph.getGraphLayoutCache().insertEdge(edge, source, target);
			//graph.getGraphLayoutCache().get
			//createEdgePoint(edge);
		}
	}


	// Hook for subclassers
	protected DefaultEdge createDefaultEdge() {
		return new DefaultEdge();
	}

	// Hook for subclassers
	public Map createEdgeAttributes() {
		Map map = new Hashtable();
		// Add a Line End Attribute
		//GraphConstants.setLineEnd(map, GraphConstants.ARROW_SIMPLE);
		// Add a label along edge attribute
		//GraphConstants.setLabelAlongEdge(map, true);
	    //GraphConstants.setLineEnd(map, GraphConstants.ARROW_SIMPLE);
	    // Add a label along edge attribute
	    //GraphConstants.setLabelAlongEdge(map, true);
		int arrow = GraphConstants.ARROW_CLASSIC;
		GraphConstants.setLineEnd(map, arrow);
		GraphConstants.setEndFill(map, true);
		
		GraphConstants.setLineColor(map, new Color(206, 176, 24));		
		//GraphConstants.setDashPattern(map, new float[]{4, 2});
		//渐变
		//GraphConstants.setGradientColor(map, Color.GRAY);		
		//拐弯
		//GraphConstants.setRouting(map, new GraphRouting());
		//GraphConstants.setRouting(map, new DefaultEdge.LoopRouting());		
		GraphConstants.setLineStyle(map, GraphConstants.STYLE_SPLINE);
		return map;
	}
	
	public void createEdgePoint(DefaultEdge e){
		EdgeView edge = (EdgeView)graph.getGraphLayoutCache().getMapping(e, false);
		List newPoints = new ArrayList();
		int n = edge.getPointCount();
		Point2D from = edge.getPoint(0);
		newPoints.add(from);
		if (edge.getSource() instanceof PortView) {
			newPoints.set(0, edge.getSource());
			from = ((PortView) edge.getSource()).getLocation();
		} else if (edge.getSource() != null) {
			Rectangle2D b = edge.getSource().getBounds();
			from = edge.getAttributes().createPoint(b.getCenterX(),
					b.getCenterY());
		}
		Point2D to = edge.getPoint(n - 1);
		CellView target = edge.getTarget();

⌨️ 快捷键说明

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