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

📄 graphmanager.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		if (target instanceof PortView)
			to = ((PortView) target).getLocation();
		else if (target != null) {
			Rectangle2D b = target.getBounds();
			to = edge.getAttributes().createPoint(b.getCenterX(),
					b.getCenterY());
		}
		if (from != null && to != null) {
			Point2D[] routed;
			double dx = Math.abs(from.getX() - to.getX());
			double dy = Math.abs(from.getY() - to.getY());
			double x2 = from.getX() + ((to.getX() - from.getX()) / 2);
			double y2 = from.getY() + ((to.getY() - from.getY()) / 2);
			routed = new Point2D[2];
			if (dx > dy) {
				routed[0] = edge.getAttributes().createPoint(x2,from.getY());
				routed[1] = edge.getAttributes().createPoint(x2, to.getY());
			} else {
				routed[0] = edge.getAttributes().createPoint(from.getX(),y2);
				routed[1] = edge.getAttributes().createPoint(to.getX(), y2);
			}
			edge.addPoint(edge.getPointCount()-1, routed[0]);
			edge.addPoint(edge.getPointCount()-1, routed[1]);
		}
		applyViewChange(graph, edge);
	}
	
	public Map createGroupAttributes() {
		System.out.println("Group Attribute...");
		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.setLineColor(map, Color.RED);
		GraphConstants.setBorderColor(map, Color.GRAY);
		GraphConstants.setDashPattern(map, new float[] { 2, 2 });
		GraphConstants.setGroupOpaque(map, true);
		GraphConstants.setInset(map, 20);
		return map;
	}

	// Create a Group that Contains the Cells
	public void group(Object[] cells) {
		// Order Cells by Model Layering
		cells = getGroupFilterCells(cells);
		cells = graph.order(cells);
		//cells = graph.getDescendants(cells);
		// If Any Cells in View
		if (cells != null && cells.length > 1) {
			//DefaultGraphCell group = createVertex(140, 25, 60, 40, Color.GRAY, false, new DefaultGraphCell("brave"), "flow.graph.gui.graph.item.GraphItemDiamond");
			
			DefaultGraphCell group = createGroupCell();
			GPCellViewFactory.setViewClass(group.getAttributes(), "flow.graph.gui.graph.item.GraphItemGroup");
			//Rectangle2D bound = GraphConstants.getBounds(group.getAttributes());
			//GraphConstants.setBounds(group.getAttributes(), new Rectangle2D.Double(bound.getX()-10, bound.getY()-10, bound.getWidth()+10, bound.getHeight()-10));
			//GraphConstants.setBounds(group.getAttributes(), new Rectangle2D.Double(bound.getX(), bound.getY(), bound.getWidth(), bound.getHeight()));
			// Insert into model
			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);
			graph.getGraphLayoutCache().toBack(new Object[]{group});
			graph.setSelectionCell(group);
			fixedGroupCellEdge(group);
		}
	}
	
	public Object[] getGroupFilterCells(Object[] cells){
		Vector v = new Vector();
		if(cells.length > 0){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultEdge){
					//是连线
					//判断如果节点不在组中,不包括该连线的组合
					DefaultEdge edge = (DefaultEdge)cells[i];
					DefaultGraphCell sourceCell = (DefaultGraphCell)((DefaultPort)edge.getSource()).getParent(); 
					DefaultGraphCell targetCell = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();
					if(sourceCell == null || targetCell == null){
						continue;
					}
					else{
						boolean sourceflag = false;
						boolean targetflag = false;
						DefaultGraphCell source = (DefaultGraphCell)((DefaultPort)edge.getSource()).getParent();
						DefaultGraphCell target = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();
						for(int j=0;j<cells.length;j++){
							if(cells[j].equals(sourceCell))
								sourceflag = true;
							if(cells[j].equals(targetCell))
								targetflag = true;
						}
						if(sourceflag == false || targetflag == false){
							continue;
						}
						else{
							v.add(cells[i]);
						}
					}
				}
				else 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.getTemplateType(cell.getAttributes()).toString().equals(TemplateConstants.STOP_TEMPLATE)){
							continue;
						}
					}
					v.add(cells[i]);
				}
				else{
					v.add(cells[i]);
				}
			}
		}
		return v.toArray();
	}
	
	public Object getGroupPort(DefaultGraphCell group){
		if(group != null){
			if(FlowGraphConstants.getTemplateType(group.getAttributes()) != null ){
				if(FlowGraphConstants.getTemplateType(group.getAttributes()).toString().equals(TemplateConstants.GROUP_TEMPLATE)){
					//判断是组
					for(int i=0;i<group.getChildCount();i++){
						if(group.getChildAt(i) instanceof DefaultPort)
							return group.getChildAt(i);
					}
				}
			}
		}
		return null;
	}
	
	public boolean isCellEmbedGroup(DefaultGraphCell group, Object obj){
		if(group != null){
			if(FlowGraphConstants.getTemplateType(group.getAttributes()) != null ){
				if(FlowGraphConstants.getTemplateType(group.getAttributes()).toString().equals(TemplateConstants.GROUP_TEMPLATE)){
					//判断是组
					for(int i=0;i<group.getChildCount();i++){
						if(group.getChildAt(i).equals(obj))
							return true;
					}
				}
			}
		}
		return false;
	}
	
	public void fixedGroupCellEdge(DefaultGraphCell group){
		DefaultPort groupPort = (DefaultPort)getGroupPort(group);
		if(groupPort == null) return;
		if(group != null){
			if(FlowGraphConstants.getTemplateType(group.getAttributes()) != null ){
				if(FlowGraphConstants.getTemplateType(group.getAttributes()).toString().equals(TemplateConstants.GROUP_TEMPLATE)){
					//判断是组
					for(int i=0;i<group.getChildCount();i++){
						if(group.getChildAt(i) instanceof GraphTemplateCell){
							DefaultGraphCell cell = (DefaultGraphCell)group.getChildAt(i);
							if(cell.getChildCount() > 0){
								if(cell.getChildAt(0) != null && cell.getChildAt(0) instanceof DefaultPort){
									DefaultPort port = (DefaultPort)cell.getChildAt(0);
									Set set = port.getEdges();
									Iterator iter = set.iterator();
									while(iter.hasNext()){
										DefaultEdge edge = (DefaultEdge)iter.next();
										if(edge.getSource() == null){
											if(edge.getTarget() != null){
												DefaultGraphCell targetCell = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();
												DefaultGraphModel.setTargetPort(graph.getModel(), edge, groupPort);
											}
										}
										else{
											DefaultGraphCell sourceCell = (DefaultGraphCell)((DefaultPort)edge.getSource()).getParent();
											if(edge.getTarget() != null){
												DefaultGraphCell targetCell = (DefaultGraphCell)((DefaultPort)edge.getTarget()).getParent();
												//判断
												
												if(isCellEmbedGroup(group, sourceCell)==true && isCellEmbedGroup(group, targetCell) == false){
													DefaultGraphModel.setSourcePort(graph.getModel(), edge, groupPort);												
												}
												else if(isCellEmbedGroup(group, sourceCell)==false && isCellEmbedGroup(group, targetCell) == true){
													DefaultGraphModel.setTargetPort(graph.getModel(), edge, groupPort);
												}
											}
											else{
												DefaultGraphModel.setSourcePort(graph.getModel(), edge, groupPort);										}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	// Hook for subclassers
	protected DefaultGraphCell createGroupCell() {
		//return new DefaultGraphCell();
		return new GraphTemplateCell();
	}

	// Returns the total number of cells in a graph
	protected int getCellCount(JGraph graph) {
		Object[] cells = graph.getDescendants(graph.getRoots());
		return cells.length;
	}

	// Ungroup the Groups in Cells and Select the Children
	public void ungroup(Object[] cells) {
		graph.getGraphLayoutCache().ungroup(cells);
	}

	// Determines if a Cell is a Group
	public boolean isGroup(Object cell) {
		// Map the Cell to its View
		CellView view = graph.getGraphLayoutCache().getMapping(cell, false);
		if (view != null)
			return !view.isLeaf();
		return false;
	}
	
	public void copy(){
		if(graph.getSelectionCount() > 0){
			try {
				writeGraphString(System.getProperty("user.dir")+"/"+TemplateFactory.CACHE_PATH+"/"+TemplateFactory.CACHE_FILE);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void paste(){
		Object obj = readGraphFile(System.getProperty("user.dir")+"/"+TemplateFactory.CACHE_PATH+"/"+TemplateFactory.CACHE_FILE);
		if(obj != null){
			Object[] cells = ((Vector)obj).toArray();
			Object[] cellss = fixModelCellID(cells);
			//DefaultGraphModel.cloneCell(graph.getModel(), cells);
			graph.getGraphLayoutCache().insert(cellss);
			graph.setSelectionCells(cellss);
			copy();
		}
	}

	// Brings the Specified Cells to Front
	public void toFront(Object[] c) {
		graph.getGraphLayoutCache().toFront(c);
	}

	public void delete(Object[] c){
		if (!graph.isSelectionEmpty()) {
			Object[] cells = graph.getDescendants(graph.getSelectionCells());
			Vector v = new Vector();
			
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultPort){
					DefaultPort port = (DefaultPort)cells[i];
					if(port.getParent() != null){
						DefaultGraphCell cell = (DefaultGraphCell)port.getParent();
						Object obj = FlowGraphConstants.getTemplateType(cell.getAttributes());
						if(obj != null){
							if(obj.toString().equals(TemplateConstants.START_TEMPLATE) || obj.toString().equals(TemplateConstants.STOP_TEMPLATE)){
								continue;
							}
						}
					}
				}
				else if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					Object obj = FlowGraphConstants.getTemplateType(cell.getAttributes());
					if(obj != null){
						if(obj.toString().equals(TemplateConstants.START_TEMPLATE) || obj.toString().equals(TemplateConstants.STOP_TEMPLATE)){
							continue;
						}
					}
				}
				v.add(cells[i]);
				//pos++;
			}
			graph.getModel().remove(v.toArray());
		}		
	}
	
	// Sends the Specified Cells to Back
	public void toBack(Object[] c) {
		graph.getGraphLayoutCache().toBack(c);
	}

	// Undo the last Change to the Model or the View
	public void undo() {
		try {
			undoManager.undo(graph.getGraphLayoutCache());
		} catch (Exception ex) {
			//System.err.println(ex);
		} finally {
			updateHistoryButtons();
		}
	}

	// Redo the last Change to the Model or the View
	public void redo() {
		try {
			undoManager.redo(graph.getGraphLayoutCache());
		} catch (Exception ex) {
			//System.err.println(ex);
		} finally {
			updateHistoryButtons();
		}
	}

	// Update Undo/Redo Button State based on Undo Manager
	protected void updateHistoryButtons() {
		// The View Argument Defines the Context
		undoButton.setEnabled(undoManager.canUndo(graph.getGraphLayoutCache()));
		redoButton.setEnabled(undoManager.canRedo(graph.getGraphLayoutCache()));
	}

	//
	// Listeners
	//

	// From GraphSelectionListener Interface
	public void valueChanged(GraphSelectionEvent e) {
		setStatus(false);
		// Group Button only Enabled if more than One Cell Selected
		
		groupButton.setEnabled(graph.getSelectionCount() > 1);
		// Update Button States based on Current Selection
		boolean enabled = !graph.isSelectionEmpty();
		deleteButton.setEnabled(enabled);
		ungroupButton.setEnabled(isGroup(graph.getSelectionCell()));
		frontButton.setEnabled(enabled);
		backButton.setEnabled(enabled);
		copyButton.setEnabled(enabled);
		cutButton.setEnabled(enabled);
		templateButton.setEnabled(enabled);
	}

	//
	// KeyListener for Delete KeyStroke
	//
	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
		//System.out.println("Key:"+e.getKeyCode());
	}

	public void keyPressed(KeyEvent e) {
		// Listen for Delete Key Press
		//System.out.println("Key:"+e.getKeyCode());
		if (e.getKeyCode() == KeyEvent.VK_DELETE){

⌨️ 快捷键说明

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