View Javadoc

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