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

📄 servertreeview.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				// TODO Auto-generated method stub
				
			}

			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}});
	}
	
	/**
	 * Called when a node in the <code>tree</code> is selected.
	 * 
	 * @param event Used to determine which node has been selected.
	 * 
	 * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
	 */
	public void valueChanged(TreeSelectionEvent event) {
		TreePath tp = event.getPath();
		if (tp != null) {
			Object opath[] = tp.getPath();
			//System.out.println("valueChanged...1");
			DefaultMutableTreeNode	n = (DefaultMutableTreeNode)opath[opath.length-1];
			if (n.getUserObject() instanceof ServerTreeViewSelectionListener) {
				//System.out.println("valueChanged...2");
				((ServerTreeViewSelectionListener)n.getUserObject()).doSelection(tree, n);
				tree.expandPath(tp);
				tree.setSelectionPath(tp);
			}
			//System.out.println("valueChanged...3");

			if(opath.length == 1){
				//选中业务列表
				newServiceAction.setEnabled(true);			
				modifyServiceAction.setEnabled(false);
				newNodeAction.setEnabled(true);
				modifyNodeAction.setEnabled(false);

				toolNewServiceAction.setEnabled(true);			
				toolModifyServiceAction.setEnabled(false);
				toolNewNodeAction.setEnabled(true);
				toolModifyNodeAction.setEnabled(false);
			}
			else if (opath.length == 2) {
				if((((DefaultMutableTreeNode)opath[1]).getUserObject() instanceof ServiceNode)){
					//选中业务节点
					newServiceAction.setEnabled(false);			
					modifyServiceAction.setEnabled(true);
					newNodeAction.setEnabled(true);
					modifyNodeAction.setEnabled(false);

					toolNewServiceAction.setEnabled(false);			
					toolModifyServiceAction.setEnabled(true);
					toolNewNodeAction.setEnabled(true);
					toolModifyNodeAction.setEnabled(false);
				}
				else{
					//选中逻辑节点
					newServiceAction.setEnabled(false);			
					modifyServiceAction.setEnabled(false);
					newNodeAction.setEnabled(false);
					
					toolNewServiceAction.setEnabled(false);			
					toolModifyServiceAction.setEnabled(false);
					toolNewNodeAction.setEnabled(false);
					if (n.getUserObject() instanceof BusinessNode){
						modifyNodeAction.setEnabled(true);
						toolModifyNodeAction.setEnabled(true);
					}
				}
			}
			else{
				//选中逻辑节点
				newServiceAction.setEnabled(false);			
				modifyServiceAction.setEnabled(false);
				newNodeAction.setEnabled(false);

				toolNewServiceAction.setEnabled(false);			
				toolModifyServiceAction.setEnabled(false);
				toolNewNodeAction.setEnabled(false);

				if (n.getUserObject() instanceof BusinessNode){
					modifyNodeAction.setEnabled(true);
					toolModifyNodeAction.setEnabled(true);
				}
			}
			
			Object userObject = n.getUserObject();
			if (userObject != null && userObject instanceof Deleteable) {
				deleteAction.setEnabled(true);
				deleteAction.putValue(Action.NAME,"删除 "+((Deleteable)userObject).getDescription());
				tree.getActionMap().put(deleteAction,deleteAction);

				toolDeleteAction.setEnabled(true);
				toolDeleteAction.putValue(Action.NAME,"删除 "+((Deleteable)userObject).getDescription());
				tree.getActionMap().put(toolDeleteAction,toolDeleteAction);
			} else {
				deleteAction.setEnabled(false);
				deleteAction.putValue(Action.NAME,"删除");	
				tree.getActionMap().remove(deleteAction);

				toolDeleteAction.setEnabled(false);
				toolDeleteAction.putValue(Action.NAME,"删除");	
				tree.getActionMap().remove(toolDeleteAction);
			}
			
			deleteAction.setEnabled(n.getUserObject() instanceof Deleteable);
			toolDeleteAction.setEnabled(n.getUserObject() instanceof Deleteable);
		} else {
			newServiceAction.setEnabled(false);			
			modifyServiceAction.setEnabled(false);			
			newNodeAction.setEnabled(false);
			modifyNodeAction.setEnabled(false);

			toolNewServiceAction.setEnabled(false);			
			toolModifyServiceAction.setEnabled(false);			
			toolNewNodeAction.setEnabled(false);
			toolModifyNodeAction.setEnabled(false);

			deleteAction.setEnabled(false);	
			deleteAction.putValue(Action.NAME,"删除");	
			tree.getActionMap().remove(deleteAction);

			toolDeleteAction.setEnabled(false);	
			toolDeleteAction.putValue(Action.NAME,"删除");	
			tree.getActionMap().remove(toolDeleteAction);
		}
	}

	/**
	 * Called when a branch in the <code>tree</code> is being expanded.
	 * 
	 * @param event Used to determine which node is being expanded
	 * 
	 * @see javax.swing.event.TreeWillExpandListener#treeWillExpand(javax.swing.event.TreeExpansionEvent)
	 */
	public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
		Object					opath[] = event.getPath().getPath();
		DefaultMutableTreeNode	n = (DefaultMutableTreeNode)opath[opath.length-1];
		Object					userObject = n.getUserObject();
		
		if (userObject instanceof ServerTreeViewExpansionListener) {
			((ServerTreeViewExpansionListener)userObject).willExpand(tree, n);
		}
	}
	
	/**
	 * Called when a branch in the <code>tree</code> is being collapsed.
	 * 
	 * @see javax.swing.event.TreeWillExpandListener#treeWillCollapse(javax.swing.event.TreeExpansionEvent)
	 */
	public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {	}
	/**
	 * @return
	 */
	
	/*
	public Action getNewServiceAction() {
		return newServiceAction;
	}

	public Action getModifyServiceAction() {
		return modifyServiceAction;
	}*/

	/**
	 * @return
	 */
	public Action getNewNodeAction() {
		return newNodeAction;
	}
	
	public Action getModifyNodeAction(){
		return modifyNodeAction;
	}

	public void selectNodeInstance(DefaultMutableTreeNode parent, Class c) {
		DefaultMutableTreeNode node;	
		for(int x=0;x<parent.getChildCount();x++) {
			node = (DefaultMutableTreeNode)parent.getChildAt(x);
			if (node.getUserObject().getClass() == c) {
				tree.setSelectionPath(null);
				tree.setSelectionPath(new TreePath(node.getPath()));				
			}
		}
	}
	
	/*
	public void addService(){
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			
			String name = JOptionPane.showInputDialog(FlowManager.getInstance(),
					"请输入新业务名称",
					"新建业务",
					JOptionPane.QUESTION_MESSAGE);
			if (name != null && name.length() > 0) {
				//System.out.println("name="+name);
				DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[0];
				StartNode start = (StartNode)node.getUserObject();
				if (start.addService(name)) {
					selectNodeInstance(node,ServiceNode.class);
					//activatePath(path);
				}
			}
		}
	}
	*/

	public void addNode(){
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			
			String name = JOptionPane.showInputDialog(FlowManager.getInstance(),
					"请输入新节点名称",
					"新建节点",
					JOptionPane.QUESTION_MESSAGE);
			if (name != null && name.length() > 0) {
				//System.out.println("name="+name);
				if(objPath.length == 1){
					//业务列表增加节点
					DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[0];
					StartNode start = (StartNode)node.getUserObject();
					if (start.addNode(start.getServiceBean().getT_id(), name)) {
						selectNodeInstance(node,StartNode.class);
						activatePath(path);
					}
				}
				else if(objPath.length == 2){
					//业务节点增加节点
					DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[1];
					ServiceNode service = (ServiceNode)node.getUserObject();
					if (service.addNode(service.getServiceBean().getT_id(), name)) {
						selectNodeInstance(node,ServiceNode.class);
						activatePath(path);
					}
				}
			}
			//refreshRoot();
		}
	}
	
	public void modifyNode(){
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
			
			String name = JOptionPane.showInputDialog(FlowManager.getInstance(),
					"原节点名称:"+((DefaultMutableTreeNode)objPath[objPath.length-1]).getUserObject().toString()+"\n请输入新节点名称",
					"修改节点",
					JOptionPane.QUESTION_MESSAGE);
			if (name != null && name.length() > 0) {
				/*
				if (service.modifyNode(business, name)) {
					selectNodeInstance(node,BusinessNode.class);
					//activatePath(path);
				}
				*/
				if(objPath.length == 2){
					//业务列表增加节点
					DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[1];
					BusinessNode business = (BusinessNode)node.getUserObject();
					StartNode start = (StartNode)parent.getUserObject();
					if (start.modifyNode(business.getNodeBean(), name)) {
						selectNodeInstance(node,StartNode.class);
						activatePath(path);
					}
				}
				else if(objPath.length == 3){
					//业务节点增加节点
					DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[2];
					BusinessNode business = (BusinessNode)node.getUserObject();
					ServiceNode service = (ServiceNode)parent.getUserObject();
					if (service.modifyNode(business, name)) {
						selectNodeInstance(node,ServiceNode.class);
						activatePath(path);
					}
				}
				tree.updateUI();
				//refreshRoot();
			}
		}
	}
	
	public void modifyService(){
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
			DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[1];
			ServiceNode service = (ServiceNode)node.getUserObject();
			
			String name = JOptionPane.showInputDialog(FlowManager.getInstance(),
					"原业务名称:"+service.toString()+"\n请输入新业务名称",
					"修改业务",
					JOptionPane.QUESTION_MESSAGE);
			if (name != null && name.length() > 0) {
				//业务列表增加节点
				StartNode start = (StartNode)parent.getUserObject();
				if (start.modifyService(service, name)) {
					selectNodeInstance(node,StartNode.class);
					activatePath(path);
				}
				tree.updateUI();
				//refreshRoot();
			}
		}
	}
	
	public void deleteSelected() {
		
		TreePath path = tree.getSelectionPath();
		
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)objPath[objPath.length-1];
			Deleteable deleteable = (Deleteable)node.getUserObject();
			
			if (deleteable.delete()) {
				// change the selection path to the above item
				Vector	newPath = new Vector();
				for(int x=0;x<objPath.length-1;x++)
					newPath.add(objPath[x]);				
				tree.setSelectionPath(new TreePath(newPath.toArray()));
				
				//DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
				//activatePath(path.getParentPath());
				 
				 
				//refreshRoot();
			}
		}
		/*
		TreePath path = tree.getSelectionPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode	node = (DefaultMutableTreeNode)objPath[objPath.length-1];
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
			BusinessNode business = (BusinessNode)node.getUserObject();
			int res = JOptionPane.showConfirmDialog(FlowManager.getInstance(),
					"确定要删除节点 \""+node.getUserObject().toString()+"\"?\n",
					"系统删除操作提示",
					JOptionPane.YES_NO_OPTION);
			if (res == JOptionPane.YES_OPTION) {
				if(objPath.length == 2){
					//业务列表增加节点
					StartNode start = (StartNode)parent.getUserObject();
					start.deleteBusinessNode(business);
					selectNodeInstance(parent,StartNode.class);
					activatePath(path.getParentPath());
				}
				else if(objPath.length == 3){
					//业务节点增加节点
					ServiceNode service = (ServiceNode)parent.getUserObject();
					service.deleteBusinessNode(business);
					selectNodeInstance(parent,ServiceNode.class);
					activatePath(path.getParentPath());
				}
			}
			refreshRoot();
		}
		*/
	}

	public void deleteServiceNode(ServiceNode s) {
		TreePath path = tree.getSelectionPath();
		
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)objPath[objPath.length-1];

			//BusinessNode business = (BusinessNode)node.getUserObject();
			System.out.println("`````deleteServiceNode....start");
			//删除业务列表节点
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
			StartNode start = (StartNode)parent.getUserObject();
			start.deleteServiceNode(s);
			selectNodeInstance(parent, StartNode.class);
			//activatePath(path.getParentPath());
			//resetChildren(start, "");
			resetRoot();
			tree.updateUI();
		}
	}

	public void deleteBusinessNode(BusinessNode s) {
		TreePath path = tree.getSelectionPath();
		TreePath parentPath = path.getParentPath();
		if (path != null) {
			Object objPath[] = path.getPath();
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)objPath[objPath.length-1];

			//BusinessNode business = (BusinessNode)node.getUserObject();
			if(objPath.length == 2){
				System.out.println("deleteBusinessNode....start");
				//删除业务列表节点
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
				StartNode start = (StartNode)parent.getUserObject();
				start.deleteBusinessNode(s);
				selectNodeInstance(parent, StartNode.class);
				//activatePath(path.getParentPath());
				//resetChildren(start, "");
				//start.reload();
				//refreshRoot();
				resetRoot();
				tree.updateUI();
			}
			else{
				//删除子业务节点
				System.out.println("deleteBusinessNode....service");
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getParentPath().getLastPathComponent();
				ServiceNode service = (ServiceNode)parent.getUserObject();
				service.deleteBusinessNode(s);
				selectNodeInstance(parent, ServiceNode.class);
				resetChildren(service, "");
				//activatePath(path.getParentPath());
				//System.out.println("service="+service);
				//explandPath(service);
				tree.updateUI();
				activatePath(parentPath);
			}
			
			//refreshRoot();
			//treeView.activatePath(path);
		}
	}

	/**
	 * Provides access to the delete action.
	 * @return The delete action.
	 */
	public Action getDeleteAction() {
		return deleteAction;
	}
}

⌨️ 快捷键说明

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