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

📄 treeapplet.java

📁 是一个Tcp和Udp网络通信的小程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				JMenuItem deleteItem = null;
				JMenuItem updateItem = null;
				JMenuItem detailItem = null;

				final Element element = dataBox.getSelectionModel().lastElement();
				if (element == null) {
					System.out.println("[未选择任何组件]");
					return null;
				}

				/** 组件相关操作 **/
				if (!(element instanceof Group) && !(element instanceof Dummy)) {
					if ((element instanceof Node) && !Boolean.valueOf(element.getClientProperty(TopoClientConstants._IS_UNKNOWN_DEVICE).toString())) {
						// 组件的详细信息
						detailItem = new JMenuItem("详细信息");
						detailItem.setFont(font);
						detailItem.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent event) {
								// 打开节点的详细页面
								try {
									openNodeDetailURL(element);
								} catch (MalformedURLException ex) {
									ex.printStackTrace();
									return;
								}
							}
						});
						popupMenu.add(detailItem);
					}
				}
				
				// 添加
				
				JMenu createMenu=new JMenu("添加");
				createMenu.setFont(font);
				
				createItem = new JMenuItem("添加组");
				createItem.setFont(font);
				createItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						DialogPanel dialogPanel = new DialogPanel();
						dialogPanel.setAutoscrolls(false);
						if (dialogPanel.showDialog(TreeApplet.this, "添加")) {
							String[] result = dialogPanel.getResult();
							if (result[0] == null || result[0].trim().equals("")) {
								JOptionPane.showMessageDialog(null, "您输入的名称为空!", TopoClientConstants._MESSAGE_WARNING, JOptionPane.WARNING_MESSAGE);
								return;
							}
							String createType=null;
							if (result[1].equals("true")) {
								createType=TopoClientConstants._GROUP;
							} else if (result[1].equals("false")) {
								createType=TopoClientConstants._DUMMY;
							}
							
							String strResponseXml = topoActionMngr.addElementExcludeLink(strViewID, createType, element.getID().toString(), result[0]);
							if (strResponseXml == null) {
								JOptionPane.showMessageDialog(null, TopoClientConstants._RESPONSE_DATA_NULL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
							} else {
								try {
									parseXmlDataToDataBox(strResponseXml);
									JOptionPane.showMessageDialog(null, TopoClientConstants._ADD_ELEMENT_SUCESSFUL, TopoClientConstants._MESSAGE_INFO, JOptionPane.INFORMATION_MESSAGE);
								} catch (IOException ex) {
									JOptionPane.showMessageDialog(null, TopoClientConstants._PARSE_RESPONSE_DATA_FAIL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
									ex.printStackTrace();
								}
							}
						}
						dialogPanel=null;
					}
				});
				
				createMenu.add(createItem);
				
				createNodeItem=new JMenuItem("添加节点");
				createNodeItem.setFont(font);
				createNodeItem.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						
					}
				});
				
				createMenu.add(createNodeItem);
				
				popupMenu.add(createMenu);
				
				// 删除
				deleteItem = new JMenuItem("删除");
				deleteItem.setFont(font);
				deleteItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						Iterator list = dataBox.getSelectionModel().selection();
						while (list.hasNext()) {
							Element element = (Element) list.next();
							Iterator children = element.children();
							if (children != null && children.hasNext()) {
								JOptionPane.showMessageDialog(null, "您不能删除该组件,因为它不为空!", TopoClientConstants._MESSAGE_WARNING, JOptionPane.WARNING_MESSAGE);
								return;
							}
						}

						int result = JOptionPane.showConfirmDialog(null, "您确定要删除该组件吗?", TopoClientConstants._MESSAGE_INFO, JOptionPane.YES_NO_OPTION);
						if (result != JOptionPane.YES_OPTION) {
							return;
						}

						// 生成待删除组件的列表
						list = dataBox.getSelectionModel().selection();
						if (list != null) {
							ArrayList<String[]> listElementTypeAndID = new ArrayList<String[]>();
							while (list.hasNext()) {
								Element element = (Element) list.next();
								listElementTypeAndID.add(new String[]{ TopoClientUtil.getElementType(element), element.getID().toString() });
							}
							
							// 删除操作
							String strResponseXml = topoActionMngr.deleteElement(strViewID, listElementTypeAndID);
							if (strResponseXml == null) {
								JOptionPane.showMessageDialog(null, TopoClientConstants._RESPONSE_DATA_NULL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
							} else {
								try {
									parseXmlDataToDataBox(strResponseXml);
									JOptionPane.showMessageDialog(null, TopoClientConstants._DELETE_ELEMENT_SUCESSFUL, TopoClientConstants._MESSAGE_INFO, JOptionPane.INFORMATION_MESSAGE);
								} catch (IOException ex) {
									JOptionPane.showMessageDialog(null, TopoClientConstants._PARSE_RESPONSE_DATA_FAIL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
									ex.printStackTrace();
								}
							}
						}
					}
				});
				popupMenu.add(deleteItem);
				
				/** 当前组件的告警详细信息 **/
				int intCurrElmtAlarmCount = element.getAlarmState().getAlarmCount();
				if (intCurrElmtAlarmCount > 0) {
					JMenuItem alarmDetailItem = new JMenuItem("告警信息");
					alarmDetailItem.setFont(font);
					alarmDetailItem.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent event) {
							if ((element instanceof Node) || (element instanceof Link)) {
								try {
									String strArmDetailReqURL = strAlarmDetailURL.concat("?viewId=" + strViewID 
																				+ "&elementId=" + element.getID().toString()
																				+ "&actionObjType=" + TopoClientUtil.getElementType(element));
									getAppletContext().showDocument(new URL(strArmDetailReqURL), "Network");
								} catch (MalformedURLException ex) {
									ex.printStackTrace();
									return;
								}
							}
						}
					});
					popupMenu.add(alarmDetailItem);
				}
				
				// 重命名
				updateItem = new JMenuItem("重命名");
				updateItem.setFont(font);
				updateItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) {
						String newName = JOptionPane.showInputDialog(null, "请输入新名称!", element.getName());
						if (newName == null) {
							return;
						}
						if (newName.trim().equals("")) {
							JOptionPane.showMessageDialog(null, "您输入的名称为空!", TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
							return;
						}
						
						// 重命名操作
						String strResponseXml = topoActionMngr.renameElement(strViewID, TopoClientUtil.getElementType(element), 
															element.getID().toString(), element.getName(), newName);
						if (strResponseXml == null) {
							JOptionPane.showMessageDialog(null, TopoClientConstants._RESPONSE_DATA_NULL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
						} else {
							try {
								parseXmlDataToDataBox(strResponseXml);
								JOptionPane.showMessageDialog(null, TopoClientConstants._RENAME_ELEMENT_SUCESSFUL, TopoClientConstants._MESSAGE_INFO, JOptionPane.INFORMATION_MESSAGE);
							} catch (IOException ex) {
								JOptionPane.showMessageDialog(null, TopoClientConstants._PARSE_RESPONSE_DATA_FAIL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
								ex.printStackTrace();
							}
						}
					}
				});
				popupMenu.add(updateItem);
				
				
				return popupMenu;
			}
		};
		
		return popupMenuGenerator;
	}
	
	/**
	 * 打开节点详细页面。
	 * @throws MalformedURLException 
	 */
	public void openNodeDetailURL(Element element) throws MalformedURLException {
		try {
			// 判断是否为节点
			String strNodeOrLinkDetailReqURL = strNodeOrLinkDetailURL.concat("?viewId=" + strViewID 
													+ "&elementId=" + element.getID().toString()
													+ "&actionObjType=" + TopoClientUtil.getElementType(element));
			getAppletContext().showDocument(new URL(strNodeOrLinkDetailReqURL), "Network");
		} catch (MalformedURLException ex) {
			throw ex;
		}
	}
	
	
	
	/**
	 * 设置拓扑树的事件监听器。
	 * @param topoTree
	 */
	private void setTopoTreeListener(final TTree topoTree, final TopoActionManager topoActionMngr) {
		if (topoTree != null) {
			
			// 设置拖放操作事件
			topoTree.setDnDHandler(new DefaultDnDHandler() {
				protected void dropProcess(TTree tree, Element targetElement) {
					if ((targetElement instanceof Group) || (targetElement instanceof Dummy)) {
						// 拖放操作
						Iterator iter = dataBox.getSelectionModel().selection();
						if (iter != null) {
							Element element = null;
							ArrayList<String[]> listElementTypeAndID = new ArrayList<String[]>();
							while (iter.hasNext()) {
								element = (Element) iter.next();
								listElementTypeAndID.add(new String[]{ TopoClientUtil.getElementType(element), element.getParent().getID().toString(), 
																			targetElement.getID().toString(), element.getID().toString()});
							}
							String strResponseXml = topoActionMngr.dragElement(strViewID, listElementTypeAndID);
							if (strResponseXml == null) {
								JOptionPane.showMessageDialog(null, TopoClientConstants._RESPONSE_DATA_NULL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
							} else {
								try {
									parseXmlDataToDataBox(strResponseXml);
//									JOptionPane.showMessageDialog(null, TopoClientConstants._DRAG_ELMENT_SUCESSFUL, TopoClientConstants._MESSAGE_INFO, JOptionPane.INFORMATION_MESSAGE);
								} catch (IOException ex) {
									JOptionPane.showMessageDialog(null, TopoClientConstants._PARSE_RESPONSE_DATA_FAIL, TopoClientConstants._MESSAGE_ERROR, JOptionPane.ERROR_MESSAGE);
									ex.printStackTrace();
								}
							}
						}
					}
				}
			});

			topoTree.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent event) {
					int row = topoTree.getRowForLocation(event.getX(), event.getY());
					if (row >= 0)
						return;
					else {
						topoTree.getDataBox().getSelectionModel().clearSelection();
					}
				}
			});

			topoTree.addElementNodeDoubleClickedActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					Element element = (Element) dataBox.getSelectionModel().lastElement();
					if (!(element instanceof Group) && !(element instanceof Link) && !(element instanceof Dummy)) {
						if ((element instanceof Node) && !Boolean.valueOf(element.getClientProperty(TopoClientConstants._IS_UNKNOWN_DEVICE).toString())) {
							// 打开详细页面
							try {
								openNodeDetailURL(element);
							} catch (MalformedURLException ex) {
								ex.printStackTrace();
								return;
							}
						}
					}
				}
			});
		}
	}
	
	public TTree getTopoTree() {
		return topoTree;
	}

	public void setTopoTree(TTree topoTree) {
		this.topoTree = topoTree;
	}

	public String getStrCurrViewBackImage() {
		return strCurrViewBackImage;
	}

	public void setStrCurrViewBackImage(String strCurrViewBackImage) {
		this.strCurrViewBackImage = strCurrViewBackImage;
	}

	//get dataBox
	public TDataBox getDataBox() {
		return dataBox;
	}
	//set dataBox
	public void setDataBox(TDataBox dataBox) {
		this.dataBox = dataBox;
	}

	public TopoActionManager getTopoActionMngr() {
		return topoActionMngr;
	}

	public String getStrViewID() {
		return strViewID;
	}

	public String getStrCurrViewIsLayouted() {
		return strCurrViewIsLayouted;
	}

	public HashMap<String, String> getHmAllViewBackImageList() {
		return hmAllViewBackImageList;
	}
}

⌨️ 快捷键说明

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