View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.panels.request.views.html;
14  
15  import com.eviware.soapui.impl.rest.RestRequest;
16  import com.eviware.soapui.impl.rest.panels.request.AbstractRestRequestDesktopPanel.RestResponseDocument;
17  import com.eviware.soapui.impl.rest.panels.request.AbstractRestRequestDesktopPanel.RestResponseMessageEditor;
18  import com.eviware.soapui.impl.support.AbstractHttpRequest;
19  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
20  import com.eviware.soapui.support.UISupport;
21  import com.eviware.soapui.support.components.BrowserComponent;
22  import com.eviware.soapui.support.components.JXToolBar;
23  import com.eviware.soapui.support.editor.views.AbstractXmlEditorView;
24  
25  import javax.swing.*;
26  import java.awt.*;
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  
30  @SuppressWarnings( "unchecked" )
31  public class RestHtmlResponseView extends AbstractXmlEditorView<RestResponseDocument> implements PropertyChangeListener //, BrComponentListener 
32  {
33     private final RestRequest restRequest;
34     private JPanel contentPanel;
35     private JPanel panel;
36     private JLabel statusLabel;
37     private BrowserComponent browser;
38  
39     public RestHtmlResponseView( RestResponseMessageEditor restRequestMessageEditor, RestRequest restRequest )
40     {
41        super( "HTML", restRequestMessageEditor, RestHtmlResponseViewFactory.VIEW_ID );
42        this.restRequest = restRequest;
43  
44        restRequest.addPropertyChangeListener( this );
45     }
46  
47     public JComponent getComponent()
48     {
49        if( panel == null )
50        {
51           panel = new JPanel( new BorderLayout() );
52  
53           panel.add( buildToolbar(), BorderLayout.NORTH );
54           panel.add( buildContent(), BorderLayout.CENTER );
55           panel.add( buildStatus(), BorderLayout.SOUTH );
56        }
57  
58        return panel;
59     }
60  
61     @Override
62     public void release()
63     {
64        super.release();
65  
66        browser.release();
67        restRequest.removePropertyChangeListener( this );
68     }
69  
70     private Component buildStatus()
71     {
72        statusLabel = new JLabel();
73        statusLabel.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
74        return statusLabel;
75     }
76  
77     private Component buildContent()
78     {
79        contentPanel = new JPanel( new BorderLayout() );
80  
81        browser = new BrowserComponent();
82  
83        HttpResponse response = restRequest.getResponse();
84        if( response != null )
85           setEditorContent( response );
86  
87        Component component = browser.getComponent();
88        component.setMinimumSize( new Dimension( 100, 100 ) );
89        contentPanel.add( component );
90  
91        return contentPanel;
92     }
93  
94     protected void setEditorContent( HttpResponse httpResponse )
95     {
96        if( httpResponse != null && httpResponse.getContentType() != null && httpResponse.getContentType().contains( "html" ) )
97        {
98           try
99           {
100             browser.setContent( httpResponse.getContentAsString(), httpResponse.getContentType(),
101                     httpResponse.getURL().toURI().toString() );
102          }
103          catch( Exception e )
104          {
105             e.printStackTrace();
106          }
107       }
108       else
109       {
110          browser.setContent( "<missing content>", "text/text" );
111       }
112    }
113 
114    private Component buildToolbar()
115    {
116       JXToolBar toolbar = UISupport.createToolbar();
117 
118       return toolbar;
119    }
120 
121 
122    public void propertyChange( PropertyChangeEvent evt )
123    {
124       if( evt.getPropertyName().equals( AbstractHttpRequest.RESPONSE_PROPERTY ) )
125       {
126          setEditorContent( ((HttpResponse) evt.getNewValue()) );
127       }
128    }
129 
130    @Override
131    public void setXml( String xml )
132    {
133    }
134 
135    public boolean saveDocument( boolean validate )
136    {
137       return false;
138    }
139 
140    public void setEditable( boolean enabled )
141    {
142    }
143 
144 
145 }