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

📄 graphmanager.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			// Execute Remove Action on Delete Key Press
			//remove.actionPerformed(null);
			delete(graph.getSelectionCells());
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT ||
				e.getKeyCode() == KeyEvent.VK_UP||
				e.getKeyCode() == KeyEvent.VK_RIGHT||
				e.getKeyCode() == KeyEvent.VK_DOWN){
			//方向键
			//graphScrollPane.setAutoscrolls(false);
			moveSelectCells(graph.getSelectionCells(), e.getKeyCode()-37);
			//graphScrollPane.setAutoscrolls(true);
		}
		else if(e.getKeyCode() == KeyEvent.VK_A && e.isControlDown()){
			//全选
			System.out.println("AAAAA");
			graph.setSelectionCells(graph.getDescendants(graph.getRoots()));
		}
		else if(e.getKeyCode() == KeyEvent.VK_C && e.isControlDown()){
			//拷贝
			/*
			Action action = TransferHandler.getCopyAction();
			ActionEvent e1 = new ActionEvent(graph, 0, "");
			action.actionPerformed(e1);
			*/
			copy();
		}
		else if(e.getKeyCode() == KeyEvent.VK_V && e.isControlDown()){
			//粘贴
			/*
			Action action = TransferHandler.getPasteAction();
			ActionEvent e1 = new ActionEvent(graph, 0, "");
			action.actionPerformed(e1);
			*/
			//System.out.println("paste...");
			paste();
		}
		else if(e.getKeyCode() == KeyEvent.VK_Z&& e.isControlDown()){
			//撤销
			System.out.println("undo...");
			undo();
		}
		else if(e.getKeyCode() == KeyEvent.VK_Y && e.isControlDown()){
			//重做
			System.out.println("redo...");
			redo();
		}
	}
	
	private void moveSelectCells(Object[] cells, int flag){
		/*
		 * flag == 0:left
		 * flag == 1:up
		 * flag == 2:right
		 * flag == 3:down
		 */
		int step = 1;
		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.GROUP_TEMPLATE)){
						double tmpx = GraphConstants.getBounds(cell.getAttributes()).getX();
						double tmpy = GraphConstants.getBounds(cell.getAttributes()).getY();
						double tmpw = GraphConstants.getBounds(cell.getAttributes()).getWidth();
						double tmph = GraphConstants.getBounds(cell.getAttributes()).getHeight();
						if(flag == 0){
							tmpx -= step;
						}
						if(flag == 1){
							tmpy -= step;
						}
						if(flag == 2){
							tmpx += step;
						}
						if(flag == 3){
							tmpy += step;
						}
						GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(tmpx, tmpy, tmpw, tmph));
						graph.getGraphLayoutCache().editCell(cell, cell.getAttributes());
					}
					else{
						moveSelectCells(cell.getChildren().toArray(), flag);
					}
				}
			}
		}
	}
	
	private Object[] fixModelCellID(Object[] cells){
		String cellid = getGraphID();
		int id = 0;
		if(cellid != null)
			id = getID(cellid)+1;
		if(cells != null){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					if(FlowGraphConstants.getCellID(cell.getAttributes()) != null){

						FlowGraphConstants.setCellID(cell.getAttributes(), "LuaItem_"+id);
						id += 1;
						
						double x = GraphConstants.getBounds(cell.getAttributes()).getX();
						double y = GraphConstants.getBounds(cell.getAttributes()).getY();
						double w = GraphConstants.getBounds(cell.getAttributes()).getWidth();
						double h = GraphConstants.getBounds(cell.getAttributes()).getHeight();
						GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(x+10, y+10, w, h));
					}
				}
			}
		}

		setGraphID(id);
		return cells;
	}
	private Object[] fixModelCellID(Object[] cells, Point point){
		double x = 0,y = 0;
		Object[] obj = new Object[cells.length];
		String cellid = getGraphID();
		int id = 0;
		if(cellid != null)
			id = getID(cellid)+1;
		if(cells != null){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					//PrintGraphCell(cell);
					if(FlowGraphConstants.getCellID(cell.getAttributes()) != null){
						//System.out.println("fix:"+cell+","+GraphConstants.getBounds(cell.getAttributes()));
						if(x == 0){
							x = GraphConstants.getBounds(cell.getAttributes()).getX();
							y = GraphConstants.getBounds(cell.getAttributes()).getY();
						}
						else{
							if(x > GraphConstants.getBounds(cell.getAttributes()).getX())
								x = GraphConstants.getBounds(cell.getAttributes()).getX();
							if(y > GraphConstants.getBounds(cell.getAttributes()).getY())
								y = GraphConstants.getBounds(cell.getAttributes()).getY();
						}
						FlowGraphConstants.setCellID(cell.getAttributes(), "LuaItem_"+id);
						id += 1;
					}
				}
			}
		}
		
		if(cells != null){
			for(int i=0;i<cells.length;i++){
				if(cells[i] instanceof DefaultGraphCell){
					DefaultGraphCell cell = (DefaultGraphCell)cells[i];
					if(FlowGraphConstants.getCellID(cell.getAttributes()) != null){
						double tmpx = GraphConstants.getBounds(cell.getAttributes()).getX();
						double tmpy = GraphConstants.getBounds(cell.getAttributes()).getY();
						double tmpw = GraphConstants.getBounds(cell.getAttributes()).getWidth();
						double tmph = GraphConstants.getBounds(cell.getAttributes()).getHeight();
						GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(tmpx+(point.getX()-x), tmpy+(point.getY()-y), tmpw, tmph));
					}
				}
			}
		}
		setGraphID(id);
		return cells;
	}

	//
	// Custom Graph
	//

	// Defines a Graph that uses the Shift-Button (Instead of the Right
	// Mouse Button, which is Default) to add/remove point to/from an edge.
	public static class MyGraph extends JGraph {

		// Construct the Graph using the Model as its Data Source
		public MyGraph(GraphModel model) {
			this(model, null);
		}

		// Construct the Graph using the Model as its Data Source
		public MyGraph(GraphModel model, GraphLayoutCache cache) {
			super(model, cache);
			// Make Ports Visible by Default
			setPortsVisible(true);
			// Use the Grid (but don't make it Visible)
			setGridEnabled(true);
			// Set the Grid Size to 10 Pixel
			setGridSize(6);
			// Set the Tolerance to 2 Pixel
			setTolerance(2);
			// Accept edits if click on background
			setInvokesStopCellEditing(true);
			// Allows control-drag
			setCloneable(true);
			// Jump to default port on connect
			setJumpToDefaultPort(true);
		}

	}

	//
	// Custom Edge Handle
	//

	// Defines a EdgeHandle that uses the Shift-Button (Instead of the Right
	// Mouse Button, which is Default) to add/remove point to/from an edge.
	public static class MyEdgeHandle extends EdgeView.EdgeHandle {

		/**
		 * @param edge
		 * @param ctx
		 */
		public MyEdgeHandle(EdgeView edge, GraphContext ctx) {
			super(edge, ctx);
		}

		// Override Superclass Method
		public boolean isAddPointEvent(MouseEvent event) {
			// Points are Added using Shift-Click
			return event.isShiftDown();
		}

		// Override Superclass Method
		public boolean isRemovePointEvent(MouseEvent event) {
			// Points are Removed using Shift-Click
			return event.isShiftDown();
		}

	}

	//
	// Custom Model
	//

	// A Custom Model that does not allow Self-References
	public static class MyModel extends DefaultGraphModel {
		// Override Superclass Method
		public MyModel() {
			super();
		}

		public MyModel(List roots, AttributeMap attributes) {
			super(roots, attributes);
		}

		public boolean acceptsSource(Object edge, Object port) {
			return (port != null);
		}

		public boolean acceptsTarget(Object edge, Object port) {
			return (port != null);
		}
	}

	//
	// Custom MarqueeHandler

	// MarqueeHandler that Connects Vertices and Displays PopupMenus
	public class MyMarqueeHandler extends BasicMarqueeHandler {
		protected DefaultGraphCell flowGraphCell;

		// Holds the Start and the Current Point
		protected Point2D start, current;

		// Holds the First and the Current Port
		protected PortView port, firstPort;

		// Override to Gain Control (for PopupMenu and ConnectMode)
		public boolean isForceMarqueeEvent(MouseEvent e) {
			if (e.isShiftDown())
				return false;
			// If Right Mouse Button we want to Display the PopupMenu
			//if(SwingUtilities.isMiddleMouseButton(e)) return true;
			if (SwingUtilities.isRightMouseButton(e))
				// Return Immediately
				return true;
			// Find and Remember Port
			port = getSourcePortAt(e.getPoint());
			// If Port Found and in ConnectMode (=Ports Visible)
			if (port != null && graph.isPortsVisible())
				return true;
			// Else Call Superclass
			return super.isForceMarqueeEvent(e);
		}

		// Display PopupMenu or Remember Start Location and First Port
		public void mousePressed(final MouseEvent e) {
			//System.out.println("point="+e.getPoint());
			//if(graph.getSelectionCell() != null){
			//	PrintGraphCell(graph.getSelectionCell());
			//}
			//System.out.println("check:"+graph.getSelectionCell());
			int offset = 20;
			//System.out.println("e.point="+e.getPoint());
			if(ToolBarButtonGroup.getSelected() != null){
				((SelectButton)ToolBarButtonGroup.getSelected()).doClick(new Point((int)(e.getPoint().getX()-offset), (int)(e.getPoint().getY()-offset)));
				shapeArc.doClick(null);
			}
			JGroupPanel panel = FlowManager.getInstance().getGroupPanel().getGroupPanel();
			if(panel.getActiveGroup().toString().equals("业务模板")){
				JGroupPanel gp = (JGroupPanel)panel.getMember(1, 0);
				JGroupContainer container = gp.getActiveGroup();
				if(container != null){
					if(container.getUserObject() != null){
						JButtonGroup bg = (JButtonGroup)container.getUserObject();
						if(bg.getSelected() != null){
							//System.out.println("4-----------------"+gp.getActiveGroupIndex());
							Object obj = ((SelectButton)bg.getSelected()).performedAction(new Point((int)(e.getPoint().getX()-offset), (int)(e.getPoint().getY()-offset)));
							//PrintGraphCell(obj);
							//Vector clo = (Vector)((Vector)obj).clone();
							//Object[] cells = clo.toArray();
							Object[] cells = ((Vector)obj).toArray();
							//cells = graph.getDescendants(cells);
							Object[] insertcells = fixModelCellID(cells, e.getPoint());
							graph.getGraphLayoutCache().insert(insertcells);
							//graph.setSelectionCells(insertcells);
							//System.out.println("5---------------------");
							bg.unSelectedAll();
						}
					}
				}
			}			

			// If Right Mouse Button
			//e.getClickCount() == 2;
			//System.out.println("mouse Pressed...1");
			if (SwingUtilities.isRightMouseButton(e)) {
				// Find Cell in Model Coordinates
				//Object cell = graph.getFirstCellForLocation(e.getX(), e.getY());
				//createPopupMenu(e.getPoint(), cell).show(graph, e.getX(),e.getY());
				// Create PopupMenu for the Cell
				/*
				if(cell instanceof DefaultEdge){
					//System.out.println("click...edge:"+cell);
					//System.out.println(((DefaultEdge)cell).get.getSource());
					 //FlowGraphConstants.getTemplateType(((DefaultGraphCell)((DefaultEdge)cell).getSource()).getAttributes()).toString().equals(TemplateConstants.SWITCH_TEMPLATE))
					JPopupMenu menu = createPopupMenu(e.getPoint(), cell);
					// Display PopupMenu
					menu.show(graph, e.getX(), e.getY());
					return;
				}
				//if(!((GraphExtends)graph).isGroup(cell) || (cell instanceof DefaultEdge && FlowGraphConstants.getTemplateType(((DefaultGraphCell)((DefaultEdge)cell).getSource()).getAttributes()).toString().equals(TemplateConstants.SWITCH_TEMPLATE))){
				if(!((GraphExtends)graph).isGroup(cell)){// || (cell instanceof DefaultEdge && FlowGraphConstants.getTemplateType(((DefaultGraphCell)((DefaultEdge)cell).getSource()).getAttributes()).toString().equals(TemplateConstants.SWITCH_TEMPLATE))){
					//System.out.println("menu2...");
					JPopupMenu menu = createPopupMenu(e.getPoint(), cell);
					// Display PopupMenu
					menu.show(graph, e.getX(), e.getY());
					// Else if in ConnectMode and Remembered Port is Valid
				}*/

			} else if (port != null && graph.isPortsVisible()) {
				//System.out.println("double click...1");
				// Remember Start Location
				start = graph.toScreen(port.getLocation());
				// Remember First Port
				firstPort = port;
			} else {
				// Call Superclass
				//System.out.println("double click...2");

⌨️ 快捷键说明

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