1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support.components;
14
15 import java.awt.BorderLayout;
16 import java.awt.Component;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19
20 import javax.swing.AbstractAction;
21 import javax.swing.Action;
22 import javax.swing.JEditorPane;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25 import javax.swing.SwingUtilities;
26
27 import com.eviware.soapui.SoapUI;
28 import com.eviware.soapui.support.Tools;
29 import com.eviware.soapui.support.UISupport;
30 import com.eviware.soapui.support.xml.XmlUtils;
31 import com.teamdev.jxbrowser.WebBrowser;
32 import com.teamdev.jxbrowser.WebBrowserFactory;
33 import com.teamdev.jxbrowser.WebBrowserWindow;
34 import com.teamdev.jxbrowser.WindowCreator;
35 import com.teamdev.jxbrowser.event.LocationEvent;
36 import com.teamdev.jxbrowser.event.RequestAdapter;
37 import com.teamdev.jxbrowser.event.StateEvent;
38 import com.teamdev.jxbrowser.event.StatusChangeEvent;
39 import com.teamdev.jxbrowser.event.StatusChangeListener;
40 import com.teamdev.xpcom.Xpcom;
41
42 public class BrowserComponent
43 {
44 private WebBrowser browser;
45 private static WebBrowserFactory webBrowserFactory;
46 private JPanel panel = new JPanel( new BorderLayout() );
47 private JPanel statusBar;
48 private JLabel statusLabel;
49 private WebBrowserWindow browserWindowAdapter = new BrowserWindowAdapter();
50 private final boolean addToolbar;
51
52 private static Boolean initialized = false;
53
54 public BrowserComponent( boolean addToolbar )
55 {
56 if( !initialized )
57 {
58 synchronized( initialized )
59 {
60 if( !initialized )
61 {
62 try
63 {
64 if( !isJXBrowserDisabled() )
65 Xpcom.initialize( Xpcom.AWT );
66 initialized = true;
67 }
68 catch( Throwable t )
69 {
70 t.printStackTrace();
71 }
72 }
73 }
74 }
75
76 this.addToolbar = addToolbar;
77 }
78
79 public static boolean isJXBrowserDisabled()
80 {
81 return "true".equals( System.getProperty( "soapui.jxbrowser.disable", "false" ) );
82 }
83
84 public Component getComponent()
85 {
86 if( isJXBrowserDisabled() )
87 {
88 JEditorPane jxbrowserDisabledPanel = new JEditorPane();
89 jxbrowserDisabledPanel.setText( "browser component disabled" );
90 panel.add( jxbrowserDisabledPanel );
91 }
92 else
93 {
94 if( browser == null )
95 {
96 initBrowser();
97 navigate( "about:blank", null );
98
99 statusBar = new JPanel();
100 statusLabel = new JLabel();
101 statusBar.add( statusLabel, BorderLayout.CENTER );
102
103 if( addToolbar )
104 panel.add( buildToolbar(), BorderLayout.NORTH );
105
106 panel.add( browser.getComponent(), BorderLayout.CENTER );
107 panel.add( statusBar, BorderLayout.SOUTH );
108 }
109 }
110 return panel;
111 }
112
113 private Component buildToolbar()
114 {
115 JXToolBar toolbar = UISupport.createToolbar();
116
117 toolbar.addFixed( UISupport.createToolbarButton( new BackAction() ) );
118 toolbar.addRelatedGap();
119 toolbar.addFixed( UISupport.createToolbarButton( new ForwardAction() ) );
120
121 toolbar.addGlue();
122
123 return toolbar;
124 }
125
126 private class BackAction extends AbstractAction
127 {
128 public BackAction()
129 {
130 putValue( SMALL_ICON, UISupport.createImageIcon( "/arrow_left.png" ) );
131 putValue( Action.SHORT_DESCRIPTION, "Navigate to previous selection" );
132 }
133
134 public void actionPerformed( ActionEvent e )
135 {
136 if( browser.getHistory().getCurrentPosition() == 0 )
137 Toolkit.getDefaultToolkit().beep();
138 else
139 browser.goBack();
140 }
141 }
142
143 private class ForwardAction extends AbstractAction
144 {
145 public ForwardAction()
146 {
147 putValue( SMALL_ICON, UISupport.createImageIcon( "/arrow_right.png" ) );
148 putValue( Action.SHORT_DESCRIPTION, "Navigate to next selection" );
149 }
150
151 public void actionPerformed( ActionEvent e )
152 {
153
154
155
156
157
158 browser.goForward();
159 }
160 }
161
162 private void initBrowser()
163 {
164 if( webBrowserFactory == null )
165 webBrowserFactory = WebBrowserFactory.getInstance();
166
167 browser = webBrowserFactory.createBrowser();
168 browser.addStatusChangeListener( new StatusChangeListener()
169 {
170 public void statusChanged( final StatusChangeEvent event )
171 {
172 if( statusLabel != null )
173 {
174 SwingUtilities.invokeLater( new Runnable()
175 {
176 public void run()
177 {
178 statusLabel.setText( event.getStatus() );
179 }
180 } );
181 }
182 }
183 } );
184
185 browser.addRequestListener( new RequestAdapter()
186 {
187 public void stateChanged( final StateEvent event )
188 {
189
190
191 }
192 } );
193
194 browser.setWindowCreator( new WindowCreator()
195 {
196 public WebBrowserWindow createChildWindow( Component parentComponent, long flags )
197 {
198 return browserWindowAdapter;
199 }
200 } );
201
202 browser.deactivate();
203 }
204
205 public void release()
206 {
207 if( browser != null )
208 {
209 browser.stop();
210 browser.deactivate();
211 browser.dispose();
212 }
213 browser = null;
214 }
215
216 public void setContent( String contentAsString, String contentType, String contextUri )
217 {
218 if( browser == null )
219 {
220 initBrowser();
221 }
222 browser.setContentWithContext( contentAsString, contentType, contextUri );
223 }
224
225 public void setContent( String content, String contentType )
226 {
227
228
229 if( browser == null )
230 {
231 initBrowser();
232 }
233
234 try
235 {
236 browser.setContent( content, contentType );
237 }
238 catch( Throwable t )
239 {
240 SoapUI.logError( t );
241 }
242 }
243
244 public boolean navigate( String url, String errorPage )
245 {
246 if( browser == null )
247 {
248 initBrowser();
249 }
250
251 SwingUtilities.invokeLater( new Navigator( url, errorPage ) );
252
253 return true;
254 }
255
256 public String getContent()
257 {
258 return browser == null ? null : XmlUtils.serialize( browser.getDocument() );
259 }
260
261 private final class Navigator implements Runnable
262 {
263 private final String url;
264 private final String errorPage;
265
266 private Navigator( String url, String errorPage )
267 {
268 this.errorPage = errorPage;
269 this.url = url;
270 }
271
272 public void run()
273 {
274 try
275 {
276 browser.navigate( url );
277 }
278 catch( Throwable e )
279 {
280 SoapUI.log( e.toString() );
281 }
282
283
284
285 }
286 }
287
288 private class BrowserWindowAdapter implements WebBrowserWindow
289 {
290 public void close()
291 {
292 }
293
294 public boolean isClosed()
295 {
296 return true;
297 }
298
299 public void setModal( boolean arg0 )
300 {
301 }
302
303 public void setSize( int arg0, int arg1 )
304 {
305 }
306
307 public void setVisible( boolean arg0 )
308 {
309 }
310
311 public void setWebBrowser( WebBrowser arg0 )
312 {
313 if( arg0 != null )
314 {
315 arg0.addRequestListener( new RequestAdapter()
316 {
317
318 @Override
319 public void locationChanged( final LocationEvent arg0 )
320 {
321 if( !arg0.getLocation().equals( "about:blank" ) )
322 {
323 if( UISupport.confirm( "Open url [" + arg0.getLocation() + "] in external browser?", "Open URL" ) )
324 Tools.openURL( arg0.getLocation() );
325
326 arg0.getWebBrowser().stop();
327 arg0.getWebBrowser().deactivate();
328 arg0.getWebBrowser().dispose();
329 arg0.getWebBrowser().removeRequestListener( this );
330 }
331
332 }
333 } );
334
335
336
337
338
339
340 }
341
342 }
343 }
344
345 }