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

📄 graphmanager.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
				/*
				System.out.println("select:"+graph.getSelectionCell());
				DefaultGraphCell cell1 = (DefaultGraphCell)graph.getSelectionCell();
				if(cell1 != null){
					//System.out.println("parent:"+cell1.getParent());
					//System.out.println("root:"+cell1.getRoot());
					//System.out.println(cell1.getAttributes());
					//System.out.println(cell1.getParent().getClass());
					if(cell1.getParent() != null){
						DefaultGraphCell group = (DefaultGraphCell)cell1.getParent();
						System.out.println(group.getAttributes());
						
					}
				}*/
				super.mousePressed(e);
			}
		}

		// Find Port under Mouse and Repaint Connector
		public void mouseDragged(MouseEvent e) {
			showFlowPort(graph, e, 1);
			// If remembered Start Point is Valid
			if (start != null) {
				// Fetch Graphics from Graph
				Graphics g = graph.getGraphics();
				// Reset Remembered Port
				PortView newPort = getTargetPortAt(e.getPoint());
				// Do not flicker (repaint only on real changes)
				if (newPort == null || newPort != port) {
					// Xor-Paint the old Connector (Hide old Connector)
					paintConnector(Color.black, graph.getBackground(), g);
					// If Port was found then Point to Port Location
					port = newPort;
					if (port != null)
						current = graph.toScreen(port.getLocation());
					// Else If no Port was found then Point to Mouse Location
					else
						current = graph.snap(e.getPoint());
					// Xor-Paint the new Connector
					paintConnector(graph.getBackground(), Color.black, g);
				}
			}
			// Call Superclass
			super.mouseDragged(e);
		}

		public PortView getSourcePortAt(Point2D point) {
			// Disable jumping
			graph.setJumpToDefaultPort(false);
			PortView result;
			try {
				// Find a Port View in Model Coordinates and Remember
				result = graph.getPortViewAt(point.getX(), point.getY());
			} finally {
				graph.setJumpToDefaultPort(true);
			}
			return result;
		}

		// Find a Cell at point and Return its first Port as a PortView
		protected PortView getTargetPortAt(Point2D point) {
			// Find a Port View in Model Coordinates and Remember
			return graph.getPortViewAt(point.getX(), point.getY());
		}

		// Connect the First Port and the Current Port in the Graph or Repaint
		public void mouseReleased(MouseEvent e) {
			// If Valid Event, Current and First Port
			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());
			}
			if (e != null && port != null && firstPort != null
					&& firstPort != port) {
				// Then Establish Connection
				connect((Port) firstPort.getCell(), (Port) port.getCell());
				e.consume();
				// Else Repaint the Graph
			} else
				graph.repaint();
			// Reset Global Vars
			firstPort = port = null;
			start = current = null;
			// Call Superclass
			super.mouseReleased(e);
		}

		// Show Special Cursor if Over Port
		public void mouseMoved(MouseEvent e) {
			// Check Mode and Find Port
			showFlowPort(graph, e, 0);
			if (e != null && getSourcePortAt(e.getPoint()) != null
					&& graph.isPortsVisible()) {
				// Set Cusor on Graph (Automatically Reset)
				graph.setCursor(new Cursor(Cursor.HAND_CURSOR));
				// Consume Event
				// Note: This is to signal the BasicGraphUI's
				// MouseHandle to stop further event processing.
				e.consume();
			} else
				// Call Superclass
				super.mouseMoved(e);
		}

		// Use Xor-Mode on Graphics to Paint Connector
		protected void paintConnector(Color fg, Color bg, Graphics g) {
			// Set Foreground
			g.setColor(fg);
			// Set Xor-Mode Color
			g.setXORMode(bg);
			// Highlight the Current Port
			paintPort(graph.getGraphics());
			// If Valid First Port, Start and Current Point
			if (firstPort != null && start != null && current != null)
				// Then Draw A Line From Start to Current Point
				g.drawLine((int) start.getX(), (int) start.getY(),
						(int) current.getX(), (int) current.getY());
		}

		// Use the Preview Flag to Draw a Highlighted Port
		protected void paintPort(Graphics g) {
			// If Current Port is Valid
			if (port != null) {
				// If Not Floating Port...
				boolean o = (GraphConstants.getOffset(port.getAllAttributes()) != null);
				// ...Then use Parent's Bounds
				Rectangle2D r = (o) ? port.getBounds() : port.getParentView().getBounds();
				// Scale from Model to Screen
				r = graph.toScreen((Rectangle2D) r.clone());
				// Add Space For the Highlight Border
				r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r
						.getHeight() + 6);
				// Paint Port in Preview (=Highlight) Mode
				graph.getUI().paintCell(g, port, r, true);
			}
		}
		
		public void showFlowPort(JGraph graph, MouseEvent e, int action){
			if(graph.isPortsVisible() == true){
				DefaultGraphCell cell = getVertexHandle(graph, e.getPoint());
				//DefaultGraphCell cell = (DefaultGraphCell)graph.getFirstCellForLocation(e.getX(),e.getY());
				//CellView cellview = graph.getTopmostViewAt(e.getX(),e.getY(),true,true);
				//DefaultGraphCell cell = (DefaultGraphCell)cellview.getCell();
				boolean flag = false;
				if(cell != null){
					flag = true;
					for(int i=0;i<cell.getChildCount();i++){
						if(cell.getChildAt(i) instanceof DefaultPort){
							DefaultPort port = (DefaultPort)cell.getChildAt(i);
							PortView view = (PortView)graph.getGraphLayoutCache().getMapping(port, false);
							paintFlowPort(graph.getGraphics(), graph, view);
						}
					}
				}
				if(action == 0){
					if(flag == false || flowGraphCell == null || flowGraphCell.equals(cell) == false)
						graph.repaint();
					flowGraphCell = cell;
				}
				/*
				if(flag == false && action == 0)//moved;1 - drag;
					graph.repaint();
				*/
			}
		}
		
		public DefaultGraphCell getVertexHandle(JGraph graph, Point2D pt) {
			Object[] cells = graph.getRoots();
			//System.out.println("pt="+pt);
			for (int i = 0; i < cells.length; i++) {
				//System.out.println("class="+cells[i].getClass());
				if(cells[i] instanceof DefaultEdge){
					continue;
				}
				else if(cells[i] instanceof DefaultPort){
					continue;
				}
				DefaultGraphCell cell = (DefaultGraphCell)cells[i];
				DefaultGraphCell cell1 = getVertexHandle(graph, cell, pt);
				if(cell1 != null)
					return cell1;
			}
			return null;
		}
		
		public DefaultGraphCell getVertexHandle(JGraph graph, DefaultGraphCell cell, Point2D pt) {
			if(cell != null){
				Point2D containerPoint = graph.fromScreen((Point2D) pt.clone());
				if(cell.getAttributes() == null){
					//System.out.println("cell.getAttributes() == null");
					return null;
				}
				
				if(GraphConstants.getBounds(cell.getAttributes()) == null){
					//System.out.println("GraphConstants.getBounds(cell.getAttributes()) == null");
					return null;
				}
				
				//System.out.println(cell+"'count="+cell.getChildCount());
				if(DefaultGraphModel.isGroup(graph.getModel(), cell)){
					for(int i=0;i<cell.getChildCount();i++){
						if(cell.getChildAt(i) instanceof DefaultEdge){
							continue;
						}
						else if(cell.getChildAt(i) instanceof DefaultPort){
							continue;
						}
						DefaultGraphCell cell1 = (DefaultGraphCell)cell.getChildAt(i);
						if(DefaultGraphModel.isGroup(graph.getModel(), cell)){
							DefaultGraphCell cell2 = getVertexHandle(graph, cell1, pt);
							if(cell2 != null)
								return cell2;
								
						}
					}
					Rectangle2D rec = graph.getCellBounds(cell.getChildren().toArray());
					int inset = GraphConstants.getInset(cell.getAttributes());
					Rectangle2D rect = new Rectangle2D.Float((int)rec.getX()-inset, (int)rec.getY()-inset, (int)rec.getWidth()+inset*2, (int)rec.getHeight()+inset*2);
					if (rect.contains(containerPoint.getX(), containerPoint.getY())) {
						return cell;
					}
				}
				else{
					if (GraphConstants.getBounds(cell.getAttributes()).contains(containerPoint.getX(), containerPoint.getY())) {
						return cell;
					}
				}
			}
			return null;
		}

		protected void paintFlowPort(Graphics g, JGraph graph, PortView p){
			ImageIcon portIcon = new ImageIcon(GraphManager.class.getResource("images/port1.gif"));
			if (p != null) {
				// If Not Floating Port...
				boolean o = (GraphConstants.getOffset(p.getAllAttributes()) != null);
				// ...Then use Parent's Bounds
				Rectangle2D r = (o) ? p.getBounds() : p.getParentView().getBounds();
				// Scale from Model to Screen
				r = graph.toScreen((Rectangle2D) r.clone());
				// Add Space For the Highlight Border
				//r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r.getHeight() + 6);
				// Paint Port in Preview (=Highlight) Mode
				//graph.getUI().paintCell(g, port, r, true);
				portIcon.paintIcon(p.renderer, g, (int)p.getBounds().getX()-3, (int)p.getBounds().getY()-3);
			}
		}

	} // End of Editor.MyMarqueeHandler

	//
	// PopupMenu
	//
	public JPopupMenu createPopupMenu(final Point pt, final Object cell) {
		JPopupMenu menu = new JPopupMenu();
		Object object = graph.getSelectionCell();//.getFirstCellForLocation(pt.getX(), pt.getY());
		if(object != null){
			//不为空
			if(object instanceof DefaultEdge){
				//边
				JMenuItem editItem = new JMenuItem("编辑元素...", new ImageIcon(GraphManager.class.getResource("images/edit.gif")));
				editItem.addActionListener(new ActionListener(){

					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						graph.startEditingAtCell(cell);
					}});
				menu.add(editItem);
				menu.addSeparator();

				DefaultEdge edge = (DefaultEdge)cell;
				final DefaultPort port = (DefaultPort)edge.getSource();
				if(port != null){
					//System.out.println("edge.childcount="+edge.getChildCount());
					if(port.getParent() != null && port.getParent() instanceof DefaultGraphCell){
						if(FlowGraphConstants.getTemplateType(((DefaultGraphCell)port.getParent()).getAttributes()) != null){
							if(FlowGraphConstants.getTemplateType(((DefaultGraphCell)port.getParent()).getAttributes()).toString().equals(TemplateConstants.SWITCH_TEMPLATE)){
								JMenuItem propertyItem = new JMenuItem("元素属性...", new ImageIcon(GraphManager.class.getResource("images/property.gif")));
								propertyItem.addActionListener(new ActionListener(){

									public void actionPerformed(ActionEvent e) {
										// TODO Auto-generated method stub
										//System.out.println("aalaksdjflksadf");
										new CaseForm((DefaultGraphCell)port.getParent(), (DefaultEdge)cell, graph);
									}});
								menu.add(propertyItem);
							}
							else{
								JMenuItem propertyItem = new JMenuItem("元素属性...", new ImageIcon(GraphManager.class.getResource("images/property.gif")));
								propertyItem.addActionListener(new ActionListener(){

									public void actionPerformed(ActionEvent e) {
										// TODO Auto-generated method stub
										//System.out.println("aalaksdjflksadf");
										new ExtendsForm((DefaultGraphCell)cell, graph);
									}});
								menu.add(propertyItem);
							}
						}
					}
				}
				EdgeView view = (EdgeView)graph.getGraphLayoutCache().getMapping(cell, false);
				if(view.getPointCount() > 2){
					menu.addSeparator();
					//变直线
					if(view.getPointCount() >2){
						JMenuItem propertyItem = new JMenuItem("设置直线...", new ImageIcon(GraphManager.class.getResource("images/line.gif")));
						propertyItem.addActionListener(new ActionListener(){

							public void actionPerformed(ActionEvent e) {
								// TODO Auto-generated method stub
								EdgeView view1 = (EdgeView)graph.getGraphLayoutCache().getMapping(cell, false);
								if(view1.getPointCount() > 2)
									view1.removePoint(2);
								if(view1.getPointCount() > 2)
									view1.removePoint(2);
								applyViewChange(graph, view1);
							}});
						menu.add(propertyItem);
					}
					JMenuItem removeItem = new JMenuItem("删除拐点...", new ImageIcon(GraphManager.class.getResource("images/port.gif")));
					removeItem.addActionListener(new ActionListener(){

						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							EdgeView view1 = (EdgeView)graph.getGraphLayoutCache().getMapping(cell, false);
							//System.out.println("point.count="+view1.getPointCount());
							for(int i=2;i<view1.getPointCount();i++){
								//System.out.println("i="+i);
								view1.removePoint(i);
							}
							applyViewChange(graph, view1);
						}});
					menu.add(removeItem);
				}
				else{
					menu.addSeparator();
					//变曲线
					JMenuItem propertyItem = new JMenuItem("设置曲线...", new ImageIcon(GraphManager.class.getResource("images/circle.gif")));
					propertyItem.addActionListener(new ActionListener(){

						public void actionPerformed(ActionEvent e) {
							// TODO Auto-generated method stub
							createEdgePoint((DefaultEdge)cell);
						}});
					menu.add(propertyItem);
				}
			}
			else{
				//节点
				if(DefaultGraphModel.isGroup(graph.getModel(), object)){
					//组节点
					//循环

⌨️ 快捷键说明

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