View Javadoc

1   package com.eviware.soapui.support.components;
2   
3   import com.eviware.soapui.SoapUI;
4   import com.eviware.soapui.support.DefaultHyperlinkListener;
5   import com.eviware.soapui.support.UISupport;
6   
7   import javax.swing.*;
8   import javax.swing.text.html.HTMLEditorKit;
9   import java.awt.*;
10  import java.io.ByteArrayInputStream;
11  import java.io.IOException;
12  import java.net.NoRouteToHostException;
13  import java.net.URL;
14  
15  public class BrowserComponent
16  {
17     private JEditorPane editorPane;
18     private JScrollPane scrollPane;
19  //   private WebBrowser browser;
20  //   private static WebBrowserFactory webBrowserFactory;
21  //
22  //   static
23  //   {
24  //      Xpcom.initialize( Xpcom.AWT );    
25  //   }
26  
27     public BrowserComponent()
28     {
29        editorPane = new JEditorPane();
30        editorPane.setEditorKit( new HTMLEditorKit() );
31        editorPane.setEditable( false );
32        editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ) );
33     }
34  
35     public Component getComponent()
36     {
37  //      if( browser == null )
38  //      {
39  //         initBrowser();
40  //      }
41  //
42  //      return browser.getComponent();
43        if( scrollPane == null )
44        {
45           scrollPane = new JScrollPane( editorPane );
46           UISupport.addPreviewCorner( scrollPane, false );
47        }
48        return scrollPane;
49     }
50  
51     private void initBrowser()
52     {
53  //      if( webBrowserFactory == null )
54  //         webBrowserFactory = WebBrowserFactory.getInstance();
55  //
56  //      browser = webBrowserFactory.createBrowser();
57  //      browser.addStatusChangeListener( new StatusChangeListener() {
58  //         public void statusChanged( StatusChangeEvent event )
59  //         {
60  ////            System.out.println( "Status change: " + event );
61  //         }
62  //      } );
63  //
64  //      browser.deactivate();
65     }
66  
67     public void release()
68     {
69  //      if( browser != null )
70  //         browser.dispose();
71  //      
72  //      browser = null;
73     }
74  
75     public void setContent( String contentAsString, String contentType, String contextUri )
76     {
77        editorPane.setContentType( contentType );
78        try
79        {
80           editorPane.read( new ByteArrayInputStream( contentAsString.getBytes() ), editorPane.getEditorKit().createDefaultDocument() );
81        }
82        catch( IOException e )
83        {
84           e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
85        }
86  
87  //      if( browser == null )
88  //      {
89  //         initBrowser();
90  //      }
91  //      browser.setContentWithContext( contentAsString, contentType, contextUri );
92     }
93  
94     public void setContent( String content, String contentType )
95     {
96        setContent( content, contentType, null );
97  
98  //      if( browser == null )
99  //      {
100 //         initBrowser();
101 //      }
102 //      browser.setContent( content, contentType );
103    }
104 
105    public boolean navigate( String url )
106    {
107       try
108       {
109          editorPane.setPage( new URL( url ) );
110          return true;
111       }
112       catch( NoRouteToHostException e )
113       {
114          SoapUI.log.warn( "Failed to access [" + url + "]" );
115       }
116       catch( Exception e )
117       {
118          SoapUI.logError( e );
119       }
120 
121       return false;
122 
123 //      if( browser == null )
124 //      {
125 //         initBrowser();
126 //      }
127 //
128 //      browser.navigate( url );
129    }
130 
131    public String getContent()
132    {
133 //      return browser == null ? null : XmlUtils.serialize( browser.getDocument() );
134       return editorPane.getText();
135    }
136 }