📄 registeredclasspane.java
字号:
import com.sun.xml.tree.XmlDocument;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.*;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.io.InputStream;import java.net.URL;import java.util.Vector;/** * Title: work-flow * Description: * Copyright: Copyright (c) 2002 * Company: CSU * @author: smallbeetle * @version 1.0 */public class RegisteredClassPane extends JPanel implements TreeSelectionListener, MouseListener{ public RegisteredClassPane() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } JLabel RegisteredClassHeader = new JLabel(); //防止在面板的最上方显示标题 JSplitPane jSplitPane1 = new JSplitPane(); //分割面板放置在主面板的中部 JScrollPane jScrollPane1 = new JScrollPane(); JTree classTree = new JTree(); //放置在分割面板的上部 JTextArea nodeDescription = new JTextArea(); //对树形结点描述信息显示的模板 ClassNode selectedNode; DefaultMutableTreeNode root; private void jbInit() throws Exception { nodeDescription.setEditable(false) ; nodeDescription.setBackground(Color.lightGray); nodeDescription.setWrapStyleWord(true); nodeDescription.setLineWrap(true); this.setLayout(new BorderLayout()); RegisteredClassHeader.setText("Registered Classes"); jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT); nodeDescription.setText(""); this.add(RegisteredClassHeader, BorderLayout.NORTH); this.add(jSplitPane1, BorderLayout.CENTER); jSplitPane1.add(jScrollPane1, JSplitPane.TOP); jSplitPane1.add(new JScrollPane(nodeDescription), JSplitPane.BOTTOM); jScrollPane1.getViewport().add(classTree, null); //将树加入面板的可视区 classTree.addTreeSelectionListener(this); //将面板设为classTree selection的被通知目标 notified classTree.addMouseListener(this) ; //将面板设为classTree mouse事件的被通知目标 notified classTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION ); classTree.setCellRenderer(new ClassTreeNodeRender()); //设置树型为自己所定义的图型 //建立根节点 root = new DefaultMutableTreeNode(new ClassNode("work flow", "工作流","工作流设计平台", ClassNode.LAB )); classTree.setModel(new DefaultTreeModel(root)) ; classTree.setRootVisible(true); try { readXML(); } catch (Exception ex) { } }//user add their own class node into the vlab public void addNode(ClassNode[] classes){ DefaultMutableTreeNode clsNode = null; for (int i = 0; i < classes.length; i++) { ClassNode node = classes[i]; if (node.type == ClassNode.CLASS) { clsNode = new DefaultMutableTreeNode(node); root.add(clsNode); }else if (clsNode != null){ clsNode.add(new DefaultMutableTreeNode(node)); } } DefaultTreeModel model = (DefaultTreeModel)classTree.getModel(); model.reload(); classTree.revalidate(); } private void readXML(){ URL url = null; InputStream in = null; try{ url = new URL((String)csuSystem.getObject("SYSTEMCLASS")); in = url.openStream(); Document listXML = XmlDocument.createXmlDocument(in,false); NodeList classList = listXML.getElementsByTagName("class"); int classListLength = classList.getLength(); ClassNode[] classNodes = new ClassNode[classListLength]; for(int loop_class = 0; loop_class < classListLength; loop_class++){ Element cur_class =(Element)classList.item(loop_class); DefaultMutableTreeNode cls = null;//读出在SystemClass.xml中的参数 String cls_name = cur_class.getChildNodes().item(1).getChildNodes().item(0).getNodeValue(); String cls_label = cur_class.getChildNodes().item(3).getChildNodes().item(0).getNodeValue(); String cls_desc = cur_class.getChildNodes().item(5).getChildNodes().item(0).getNodeValue(); cls = new DefaultMutableTreeNode(new ClassNode(cls_name,cls_label, cls_desc, ClassNode.CLASS)); NodeList fieldList = cur_class.getElementsByTagName("field"); int fieldLen = fieldList.getLength(); for(int loop_field=0;loop_field < fieldLen;loop_field++){ Element cur_field =(Element)fieldList.item(loop_field); String cls_fname = cur_field.getChildNodes().item(1).getChildNodes().item(0).getNodeValue(); String cls_flabel = cur_field.getChildNodes().item(3).getChildNodes().item(0).getNodeValue(); String cls_fdesc = cur_field.getChildNodes().item(5).getChildNodes().item(0).getNodeValue(); cls.add(new DefaultMutableTreeNode(new ClassNode(cls_fname, cls_flabel, cls_fdesc, ClassNode.PROPERTY))) ; } NodeList methodList = cur_class.getElementsByTagName("method"); int methodLen = methodList.getLength(); for(int loop_method=0;loop_method<methodLen;loop_method++){ Element cur_method =(Element)methodList.item(loop_method); String cls_mname = cur_method.getChildNodes().item(1).getChildNodes().item(0).getNodeValue(); String cls_mlabel = cur_method.getChildNodes().item(3).getChildNodes().item(0).getNodeValue(); String cls_mdesc = cur_method.getChildNodes().item(5).getChildNodes().item(0).getNodeValue(); cls.add(new DefaultMutableTreeNode(new ClassNode(cls_mname, cls_mlabel, cls_mdesc, ClassNode.METHOD))); } in.close(); root.add(cls); } }catch(Exception ex){ ex.printStackTrace(); } } public ClassNode getSelectedNode(){ return selectedNode; } public void mouseClicked(MouseEvent e) { if (e.getModifiers() == MouseEvent.BUTTON3_MASK){ classTree.setSelectionPaths(null); } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} //get all registered methods or properties(decided by the "type") of the class specified by classURL //@type Node.PROPERTY, Node.METHOD private ClassNode[] getNode(String name, int type){ if ( type != ClassNode.METHOD && type != ClassNode.PROPERTY ) return null; for (int i = 0; i < root.getChildCount(); i++ ){ DefaultMutableTreeNode tn1 = (DefaultMutableTreeNode)(root.getChildAt(i)); ClassNode n1 = (ClassNode)tn1.getUserObject(); if (n1.name.equals(name)&&n1.type == ClassNode.CLASS ){ //find the class Vector v = new Vector(); //用来存储className下面地所有method方法 TreeNode tn2; DefaultMutableTreeNode dn2; ClassNode n2; for(int j = 0; j < tn1.getChildCount(); j++ ){ tn2 = tn1.getChildAt(j); dn2 = (DefaultMutableTreeNode)tn2; n2 = (ClassNode)(dn2.getUserObject()); if (n2.type == type)v.add(n2); } ClassNode[] nodes = new ClassNode[v.size()]; ClassNode node; for (int k = 0; k < nodes.length; k++){ node = (ClassNode)v.get(k); nodes[k] = (ClassNode)node.clone(); } return nodes; } } return null; } public String[][] getMethodList(String classURL){ String[][] methodList; try{ ClassNode[] nodes = this.getNode(classURL, ClassNode.METHOD); methodList = new String[nodes.length][2]; for (int i = 0; i < nodes.length; i++){ methodList[i][0] = nodes[i].name; methodList[i][1] = nodes[i].label; } }catch(Exception e){ return null; } return methodList; } public String getMethodName(String classURL){ String name; try{ ClassNode[] nodes = this.getNode(classURL, ClassNode.METHOD); name = nodes[0].name; }catch(Exception e){ return null; } return name; }//when user choice another node, we show the message of the device in nodeDescriotion panel public void valueChanged(TreeSelectionEvent e) { TreePath path = classTree.getSelectionPath() ; if (path != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent() ; selectedNode = (ClassNode)node.getUserObject() ; nodeDescription.setText(selectedNode.description); } else { nodeDescription.setText(""); selectedNode = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -