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 kc...@360commerce.com on 2002/11/18 20:17:08 UTC

[PATCH] adds primitive HTML rendering to ViewResultsFullVisualizer


The patch below gives the user the option of viewing a response as the HTML
source text or as rendered HTML.  Although the JEditorPane has limited
capabilities compared to most web browsers, it at least makes it possible to get
a quick and dirty idea of what the response looks like without digging through
all of the HTML source.

--- \temp\ViewResultsFullVisualizer.java      Mon Nov 18 12:44:49 2002
+++ ViewResultsFullVisualizer.java Mon Nov 18 13:08:42 2002
@@ -59,9 +59,13 @@
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.io.UnsupportedEncodingException;

 import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JEditorPane;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
@@ -70,6 +74,7 @@
 import javax.swing.JTree;
 import javax.swing.event.TreeSelectionEvent;
 import javax.swing.event.TreeSelectionListener;
+import javax.swing.text.html.HTMLEditorKit;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeCellRenderer;
 import javax.swing.tree.DefaultTreeModel;
@@ -93,19 +98,34 @@
  ***************************************/
 public class ViewResultsFullVisualizer
     extends AbstractVisualizer
-    implements TreeSelectionListener, Clearable
+    implements ActionListener, TreeSelectionListener, Clearable
 {
     public final static Color SERVER_ERROR_COLOR = Color.red;
     public final static Color CLIENT_ERROR_COLOR = Color.blue;
     public final static Color REDIRECT_COLOR = Color.green;
+    protected static final String HTML_BUTTON_LABEL = "Render HTML";
+    protected static final String TEXT_BUTTON_LABEL = "Show Text";
     protected DefaultMutableTreeNode root;
     protected DefaultTreeModel treeModel;
     protected GridBagLayout gridBag;
     protected GridBagConstraints gbc;
+
+    /** The button that will pop up the response as rendered HTML or
+        text.  **/
+    protected JButton htmlOrTextButton;
+
+    /** The response to be displayed.  **/
+    protected String response;
+
+    /** The pane where the rendered HTML response is displayed.  **/
+    protected JEditorPane htmlEditPane;
+
     protected JPanel resultPanel;
     protected JScrollPane treePane;
     protected JScrollPane resultPane;
     protected JSplitPane treeSplitPane;
+    /** The text area where the response is displayed.  **/
+    protected JTextArea textArea;
     protected JTree jTree;
     protected int childIndex;
     transient private static Logger log =
@@ -271,7 +291,6 @@
                    // get the text response and image icon
                    // to determine which is NOT null
                    byte[] responseBytes = (byte[]) res.getResponseData();
-                   String response = null;
                    ImageIcon icon = null;
                    if (res.getDataType() != null
                         && res.getDataType().equals(SampleResult.TEXT))
@@ -291,15 +310,22 @@
                    }
                    if (response != null)
                    {
-                        JTextArea textArea = new JTextArea();
-                        textArea.setText(response);
-                        textArea.setColumns(70);
-                        textArea.setLineWrap(true);
-                        textArea.setWrapStyleWord(true);
-                        textArea.setTabSize(4);
                         gbc.gridx = 0;
-                        gridBag.setConstraints(textArea, gbc);
-                        resultPanel.add(textArea);
+                        gbc.gridy++;
+                        gridBag.setConstraints(htmlOrTextButton,
+                                         gbc);
+                        resultPanel.add(htmlOrTextButton);
+
+                        // Text display <=> HTML labeled button
+                        if
(HTML_BUTTON_LABEL.equals(htmlOrTextButton.getText()))
+                        {
+                             showTextResponse(response);
+                        }
+                        // HTML display <=> Text labeled button
+                        else
+                        {
+                             showRenderedResponse(response);
+                        }
                    }
                    else if (icon != null)
                    {
@@ -315,6 +341,89 @@
          }
          log.debug("End : valueChanged1");
     }
+
+    protected void initTextArea()
+    {
+         textArea = new JTextArea();
+         textArea.setColumns(70);
+         textArea.setLineWrap(true);
+         textArea.setWrapStyleWord(true);
+         textArea.setTabSize(4);
+         gridBag.setConstraints(textArea, gbc);
+         resultPanel.add(textArea);
+         gbc.gridy++;
+    }
+
+    protected void showTextResponse(String response)
+    {
+         resultPanel.remove(htmlEditPane);
+
+         gbc.gridx = 0;
+         gbc.gridy++;
+         gridBag.setConstraints(textArea, gbc);
+         textArea.setText(response);
+         textArea.setCaretPosition(0);
+         resultPanel.add(textArea);
+    }
+
+
+    /**********************************************************************
+     * Display the response as text or as rendered HTML.  Change the
+     * text on the button appropriate to the current display.
+     * @param e the ActionEvent being processed
+     *********************************************************************/
+
+    public void actionPerformed(ActionEvent e)
+    {
+         // If the htmlOrTextButton is clicked, show the response in the
+         // appropriate way, and change the button label
+         if (htmlOrTextButton.equals(e.getSource()))
+         {
+              // Show rendered HTML
+              if (HTML_BUTTON_LABEL.equals(htmlOrTextButton.getText()))
+              {
+                   showRenderedResponse(response);
+                   htmlOrTextButton.setText(TEXT_BUTTON_LABEL);
+              }
+              // Show the textual response
+              else
+              {
+                   showTextResponse(response);
+                   htmlOrTextButton.setText(HTML_BUTTON_LABEL);
+              }
+         }
+    }
+
+
+    protected void initHtmlEditPane()
+    {
+         htmlEditPane = new JEditorPane();
+         HTMLEditorKit htmlKit = new HTMLEditorKit();
+         htmlEditPane.setEditorKit(htmlKit);
+    }
+
+    protected void showRenderedResponse(String response)
+    {
+         resultPanel.remove(textArea);
+
+         int htmlIndex = response.indexOf("<html>");
+         String html = response.substring(htmlIndex, response.length());
+         htmlEditPane.setText(html);
+         htmlEditPane.setCaretPosition(0);
+
+         gbc.gridx = 0;
+         gbc.gridy++;
+         gridBag.setConstraints(htmlEditPane, gbc);
+         resultPanel.add(htmlEditPane);
+    }
+
+    protected void initHtmlOrTextButton()
+    {
+         htmlOrTextButton = new JButton(HTML_BUTTON_LABEL);
+         htmlOrTextButton.addActionListener(this);
+    }
+
+
     /****************************************
      * Initialize this visualizer
      ***************************************/
@@ -336,6 +445,9 @@
          gbc = new GridBagConstraints();
          resultPanel = new JPanel(gridBag);
          resultPane = new JScrollPane(resultPanel);
+         initHtmlOrTextButton();
+         initTextArea();
+         initHtmlEditPane();
          treeSplitPane =
               new JSplitPane(JSplitPane.VERTICAL_SPLIT, treePane, resultPane);
          getFilePanel().add(getErrorLoggingCheckbox());.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>