You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/05/07 20:06:32 UTC

cvs commit: jakarta-jmeter/src/components/org/apache/jmeter/visualizers XMLDefaultMutableTreeNode.java ViewResultsFullVisualizer.java

sebb        2005/05/07 11:06:32

  Modified:    src/components/org/apache/jmeter/visualizers
                        ViewResultsFullVisualizer.java
  Added:       src/components/org/apache/jmeter/visualizers
                        XMLDefaultMutableTreeNode.java
  Log:
  Bug 34796 - add Tooltips to XML Tree display
  
  Revision  Changes    Path
  1.50      +76 -192   jakarta-jmeter/src/components/org/apache/jmeter/visualizers/ViewResultsFullVisualizer.java
  
  Index: ViewResultsFullVisualizer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/visualizers/ViewResultsFullVisualizer.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- ViewResultsFullVisualizer.java	2 May 2005 14:55:43 -0000	1.49
  +++ ViewResultsFullVisualizer.java	7 May 2005 18:06:32 -0000	1.50
  @@ -44,6 +44,7 @@
   import javax.swing.JTextArea;
   import javax.swing.JTextPane;
   import javax.swing.JTree;
  +import javax.swing.ToolTipManager;
   import javax.swing.event.TreeSelectionEvent;
   import javax.swing.event.TreeSelectionListener;
   import javax.swing.text.BadLocationException;
  @@ -73,13 +74,8 @@
   import org.apache.jmeter.visualizers.gui.AbstractVisualizer;
   import org.apache.jorphan.logging.LoggingManager;
   import org.apache.log.Logger;
  -import org.w3c.dom.Attr;
  -import org.w3c.dom.CDATASection;
  -import org.w3c.dom.Comment;
  -import org.w3c.dom.NamedNodeMap;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
  -import org.w3c.dom.Text;
   import org.xml.sax.ErrorHandler;
   import org.xml.sax.InputSource;
   import org.xml.sax.SAXException;
  @@ -796,186 +792,6 @@
       	}
       }
       
  -    /**
  -    *A extended class of DefaultMutableTreeNode except that it also attached
  -    *XML node and convert XML document into DefaultMutableTreeNode
  -    * author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
  -    * 
  -    */
  -    public class XMLDefaultMutableTreeNode extends DefaultMutableTreeNode 
  -    {
  -
  -    	boolean isRoot;
  -    	private Node xmlNode;
  -    	public XMLDefaultMutableTreeNode(Node root) throws SAXException 
  -    	{
  -    	    super(root.getNodeName());
  -    		initRoot(root);
  -    		
  -    	}
  -    	
  -    	public XMLDefaultMutableTreeNode(String name,Node xmlNode) 
  -    	{
  -    		super(name);
  -    		this.xmlNode = xmlNode;
  -    		
  -    	}
  -    	/**
  -    	 * init root
  -    	 * @param root
  -    	 * @throws SAXException
  -    	 */
  -    	private void initRoot(Node xmlRoot) throws SAXException {
  -    		
  -    	
  -    		NodeList childNodes = xmlRoot.getChildNodes();
  -    		if(childNodes == null) 
  -    			initAttributeNode(xmlRoot, this);
  -    		
  -    		for (int i = 0; i < childNodes.getLength(); i++) {
  -    			Node childNode = childNodes.item(i);
  -    			initNode(childNode, this);
  -    		}
  -
  -    	}
  -    	/**
  -    	 * init node
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initNode(Node node, XMLDefaultMutableTreeNode mTreeNode)
  -    			throws SAXException 
  -    			{
  -
  -    		switch (node.getNodeType())
  -    		{
  -    		case Node.ELEMENT_NODE:
  -    			initElementNode(node, mTreeNode);
  -    			break;
  -    			
  -    		case Node.TEXT_NODE:
  -    			 initTextNode((Text)node, mTreeNode);
  -    			break;
  -    			
  -
  -    		case Node.CDATA_SECTION_NODE:
  -    			initCDATASectionNode((CDATASection)node, mTreeNode);
  -    			break;
  -    		case Node.COMMENT_NODE:
  -    			initCommentNode((Comment)node,mTreeNode);
  -    			break;
  -    		
  -    		default:
  -    		    //if other node type, we will just skip it
  -    			break;
  -
  -    		}
  -
  -    	}
  -    	/**
  -    	 * init element node
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initElementNode(Node node, DefaultMutableTreeNode mTreeNode)
  -    			throws SAXException {
  -    		String nodeName = node.getNodeName();
  -    		
  -    		NodeList childNodes = node.getChildNodes();
  -    		XMLDefaultMutableTreeNode childTreeNode = new XMLDefaultMutableTreeNode(nodeName
  -    				,node);
  -
  -    		mTreeNode.add(childTreeNode);
  -    		initAttributeNode(node, childTreeNode);
  -    		for (int i = 0; i < childNodes.getLength(); i++) 
  -    		{
  -    			Node childNode = childNodes.item(i);
  -    			initNode(childNode, childTreeNode);
  -    		}
  -
  -    	}
  -    	/**
  -    	 * init attribute node
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initAttributeNode(Node node, DefaultMutableTreeNode mTreeNode)
  -    			throws SAXException {
  -    		NamedNodeMap nm = node.getAttributes();
  -    		for (int i = 0; i < nm.getLength(); i++) 
  -    		{
  -    			Attr nmNode = (Attr)nm.item(i);
  -    			String value = nmNode.getName() + " = \"" + nmNode.getValue() + "\"";
  -    			XMLDefaultMutableTreeNode attributeNode = new XMLDefaultMutableTreeNode(
  -    					value,nmNode);
  -    			mTreeNode.add(attributeNode);
  -
  -    		}
  -    	}
  -    	/**
  -    	 * init comment Node
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initCommentNode(Comment node, DefaultMutableTreeNode mTreeNode) throws SAXException{
  -    		String data = node.getData();
  -    		if(data != null || data.length() > 0)
  -    		{
  -    			String value = "<!--" + node.getData() + "-->";
  -    			XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(value,node);
  -    			mTreeNode.add(commentNode);
  -    		}
  -    	}
  -    	/**
  -    	 * init CDATASection Node
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initCDATASectionNode(CDATASection node, DefaultMutableTreeNode mTreeNode) throws SAXException 
  -    	{
  -    		String data = node.getData();
  -    		if(data != null || data.length() > 0) 
  -    		{
  -    			String value = "<!-[CDATA" + node.getData() + "]]>";
  -    			XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(value,node);
  -    			mTreeNode.add(commentNode);
  -    		}
  -    	}
  -    	/**
  -    	 * init the TextNode
  -    	 * @param node
  -    	 * @param mTreeNode
  -    	 * @throws SAXException
  -    	 */
  -    	private void initTextNode(Text node, DefaultMutableTreeNode mTreeNode) throws SAXException 
  -    	{
  -    		String text = node.getNodeValue().trim();
  -    		if(text != null && text.length() > 0) 
  -    		{
  -    			XMLDefaultMutableTreeNode textNode = new XMLDefaultMutableTreeNode(node
  -    				.getNodeValue(),node);
  -    			mTreeNode.add(textNode);
  -    		}
  -    	}
  -    	
  -    	
  -    	
  -    	/**
  -    	 * get the xml node
  -    	 * @return
  -    	 */
  -    	public Node getXMLNode() 
  -    	{
  -    		return xmlNode;
  -    	}
  -    	
  -
  -    }
       
       
        /**
  @@ -983,7 +799,6 @@
        * A Dom tree panel for to display response as tree view
        * author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
        * TODO implement to find any nodes in the tree using TreePath.
  -     * TODO implement tooltip to display long string of node value
        * 
        */
       private class DOMTreePanel extends JPanel {
  @@ -992,7 +807,6 @@
       	public DOMTreePanel(org.w3c.dom.Document document) {
       		super(new GridLayout(1, 0));
       		try {
  -    		    
       		    Node firstElement = getFirstElement(document);
       			DefaultMutableTreeNode top = new XMLDefaultMutableTreeNode(
       					firstElement);
  @@ -1004,7 +818,8 @@
       		    JScrollPane domJScrollPane = new JScrollPane(domJTree);
       			domJTree.setAutoscrolls(true);
       			this.add(domJScrollPane);
  -    			this.setSize(800, 600);
  +    			ToolTipManager.sharedInstance().registerComponent(domJTree);
  +    	        domJTree.setCellRenderer(new DomTreeRenderer());
       			this.setPreferredSize(new Dimension(800, 600));
       		} catch (SAXException e) {
       			log.warn("",e);
  @@ -1029,11 +844,79 @@
       		}
       		return toReturn;
       	}
  -
  -    
  -    	
  -    	
  +    	/**
  +    	 * This class is to view as tooltext. This is very useful, when
  +    	 * the contents has long string and does not fit in the view.
  +    	 * it will also automatically wrap line for each
  +    	 * 100 characters since tool tip support html.
  +    	 * author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
  +    	 */
  +    	private class DomTreeRenderer extends DefaultTreeCellRenderer {
  +    	    public Component getTreeCellRendererComponent(
  +    	                    JTree tree,
  +    	                    Object value,
  +    	                    boolean sel,
  +    	                    boolean expanded,
  +    	                    boolean leaf,
  +    	                    int row,
  +    	                    boolean phasFocus) 
  +    	    {
  +    	           super.getTreeCellRendererComponent(
  +    	                            tree, value, sel,
  +    	                            expanded, leaf, row,
  +    	                            phasFocus);
  +    	           
  +    	            DefaultMutableTreeNode valueTreeNode = (DefaultMutableTreeNode)value;
  +    	            setToolTipText(getHTML(valueTreeNode.toString(),"<br>",100));
  +    	            return this;
  +    	      }
  +    	        
  +    	        /**
  +    	         * get the html
  +    	         * @param str
  +    	         * @param separator
  +    	         * @param maxChar
  +    	         * @return
  +    	         */
  +    	        private String getHTML(String str,String separator, int maxChar) 
  +    	        {
  +    	        	StringBuffer strBuf = new StringBuffer("<html><body bgcolor=\"yellow\"><b>");
  +    	        	char[] chars = str.toCharArray();
  +    	        	for(int i=0; i< chars.length; i++) {
  +    	        		
  +    	        		if(i % maxChar == 0 && i != 0) 
  +    	        			strBuf.append(separator);
  +    	        		strBuf.append(encode(chars[i]));	
  +    	        		
  +    	        	}
  +    	        	strBuf.append("</b></body></html>");
  +    	        	return strBuf.toString();
  +    	        	
  +    	        }
  +    	        private String encode(char c) 
  +    	        {
  +    	            String toReturn = String.valueOf(c);
  +    	            switch(c) {
  +    	            	case '<':
  +    	            	    toReturn = "&lt;";
  +    	            	    break;
  +    	            	case '>':
  +    	            	    toReturn = "&gt;";
  +    	            	    break;
  +    	            	case '\'':
  +    	            	    toReturn = "&apos;";
  +    	            	    break;
  +    	            	case '\"':
  +    	            	    toReturn = "&quot;";
  +    	            	    break;
  +    	            	    
  +    	            }
  +    	            return toReturn;
  +    	        }
  +    	}
       }
  +    
  +    
   
       private static void showErrorMessageDialog(String message,int messageType) {
           JOptionPane.showMessageDialog(null, message, "Error", messageType); 
  @@ -1121,4 +1004,5 @@
           }
       }
       
  +	
   }
  
  
  
  1.1                  jakarta-jmeter/src/components/org/apache/jmeter/visualizers/XMLDefaultMutableTreeNode.java
  
  Index: XMLDefaultMutableTreeNode.java
  ===================================================================
  /*
   * Copyright 2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
  */
  
  package org.apache.jmeter.visualizers;
  
  import javax.swing.tree.DefaultMutableTreeNode;
  import org.w3c.dom.Attr;
  import org.w3c.dom.CDATASection;
  import org.w3c.dom.Comment;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.w3c.dom.Text;
  import org.xml.sax.SAXException;
  
  /**
   *A extended class of DefaultMutableTreeNode except that it also attached
   *XML node and convert XML document into DefaultMutableTreeNode
   * author <a href="mailto:d.maung@mdl.com">Dave Maung</a>
   * 
   */
   public class XMLDefaultMutableTreeNode extends DefaultMutableTreeNode 
   {
      //private static final int LIMIT_STR_SIZE = 100;
   	//private boolean isRoot;
   	private Node xmlNode;
   	public XMLDefaultMutableTreeNode(Node root) throws SAXException 
   	{
   	    super(root.getNodeName());
   	    initAttributeNode(root, this);
   		initRoot(root);
   		
   	}
   	
   	public XMLDefaultMutableTreeNode(String name,Node xmlNode) 
   	{
   		super(name);
   		this.xmlNode = xmlNode;
   		
   	}
   	/**
   	 * init root
   	 * @param root
   	 * @throws SAXException
   	 */
   	private void initRoot(Node xmlRoot) throws SAXException {
   		
   	
   		NodeList childNodes = xmlRoot.getChildNodes();
   		for (int i = 0; i < childNodes.getLength(); i++) {
   			Node childNode = childNodes.item(i);
   			initNode(childNode, this);
   		}
  
   	}
   	/**
   	 * init node
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initNode(Node node, XMLDefaultMutableTreeNode mTreeNode)
   			throws SAXException 
   			{
  
   		switch (node.getNodeType())
   		{
   		case Node.ELEMENT_NODE:
   			initElementNode(node, mTreeNode);
   			break;
   			
   		case Node.TEXT_NODE:
   			 initTextNode((Text)node, mTreeNode);
   			break;
   			
  
   		case Node.CDATA_SECTION_NODE:
   			initCDATASectionNode((CDATASection)node, mTreeNode);
   			break;
   		case Node.COMMENT_NODE:
   			initCommentNode((Comment)node,mTreeNode);
   			break;
   		
   		default:
   		    //if other node type, we will just skip it
   			break;
  
   		}
  
   	}
   	/**
   	 * init element node
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initElementNode(Node node, DefaultMutableTreeNode mTreeNode)
   			throws SAXException {
   		String nodeName = node.getNodeName();
   		
   		NodeList childNodes = node.getChildNodes();
   		XMLDefaultMutableTreeNode childTreeNode = new XMLDefaultMutableTreeNode(nodeName
   				,node);
  
   		mTreeNode.add(childTreeNode);
   		initAttributeNode(node, childTreeNode);
   		for (int i = 0; i < childNodes.getLength(); i++) 
   		{
   			Node childNode = childNodes.item(i);
   			initNode(childNode, childTreeNode);
   		}
  
   	}
   	/**
   	 * init attribute node
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initAttributeNode(Node node, DefaultMutableTreeNode mTreeNode)
   			throws SAXException {
   		NamedNodeMap nm = node.getAttributes();
   		for (int i = 0; i < nm.getLength(); i++) 
   		{
   			Attr nmNode = (Attr)nm.item(i);
   			String value = nmNode.getName() + " = \"" + nmNode.getValue() + "\"";
   			XMLDefaultMutableTreeNode attributeNode = new XMLDefaultMutableTreeNode(
   			       value,nmNode);
   			mTreeNode.add(attributeNode);
  
   		}
   	}
   	/**
   	 * init comment Node
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initCommentNode(Comment node, DefaultMutableTreeNode mTreeNode) throws SAXException{
   		String data = node.getData();
   		if(data != null || data.length() > 0)
   		{
   			String value = "<!--" + node.getData() + "-->";
   			XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(value,node);
   			mTreeNode.add(commentNode);
   		}
   	}
   	/**
   	 * init CDATASection Node
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initCDATASectionNode(CDATASection node, DefaultMutableTreeNode mTreeNode) throws SAXException 
   	{
   		String data = node.getData();
   		if(data != null || data.length() > 0) 
   		{
   			String value = "<!-[CDATA" + node.getData() + "]]>";
   			XMLDefaultMutableTreeNode commentNode = new XMLDefaultMutableTreeNode(value,node);
   			mTreeNode.add(commentNode);
   		}
   	}
   	/**
   	 * init the TextNode
   	 * @param node
   	 * @param mTreeNode
   	 * @throws SAXException
   	 */
   	private void initTextNode(Text node, DefaultMutableTreeNode mTreeNode) throws SAXException 
   	{
   		String text = node.getNodeValue().trim();
   		if(text != null && text.length() > 0) 
   		{
   			XMLDefaultMutableTreeNode textNode = new XMLDefaultMutableTreeNode(text,node);
   			mTreeNode.add(textNode);
   		}
   	}
   	
   	/**
   	 * get the xml node
   	 * @return
   	 */
   	public Node getXMLNode() 
   	{
   		return xmlNode;
   	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org