View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.wsdl.panels.iface;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.io.File;
18  import java.io.StringWriter;
19  import java.net.URL;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javax.swing.BorderFactory;
24  import javax.swing.JComponent;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JProgressBar;
28  import javax.swing.JTabbedPane;
29  
30  import org.apache.log4j.Logger;
31  import org.apache.xmlbeans.XmlObject;
32  import org.apache.xmlbeans.XmlOptions;
33  import org.syntax.jedit.JEditTextArea;
34  
35  import com.eviware.soapui.SoapUI;
36  import com.eviware.soapui.impl.wsdl.WsdlInterface;
37  import com.eviware.soapui.impl.wsdl.support.SchemaUtils;
38  import com.eviware.soapui.model.DesktopPanel;
39  import com.eviware.soapui.model.ModelItem;
40  import com.eviware.soapui.support.JXmlTextArea;
41  import com.eviware.soapui.support.ProgressDialog;
42  import com.eviware.soapui.support.SwingWorker;
43  import com.eviware.soapui.support.XmlUtils;
44  import com.jgoodies.forms.builder.ButtonBarBuilder;
45  
46  /***
47   * DesktopPanel for WsdlInterface. Loads all referenced wsdls/xsds for the specified WsdlInterface
48   * and displays these in seperate tabs
49   * 
50   * @author Ole.Matzura
51   */
52  
53  public class WsdlInterfaceDesktopPanel extends JPanel implements DesktopPanel
54  {
55     private final static Logger logger = Logger.getLogger( WsdlInterfaceDesktopPanel.class );
56     private final WsdlInterface iface;
57     private JTabbedPane tabbedPane;
58  
59     public WsdlInterfaceDesktopPanel(WsdlInterface iface)
60     {
61        super(new BorderLayout());
62  
63        this.iface = iface;
64  
65        tabbedPane = new JTabbedPane();
66        tabbedPane.setTabPlacement( JTabbedPane.TOP );
67        
68        add( tabbedPane, BorderLayout.CENTER );
69        String wsdlUrl = iface.getDefinition();
70        
71        Loader loader = new Loader( wsdlUrl );
72        ProgressDialog progressDialog = SoapUI.getInstance() == null ? null : 
73        	new ProgressDialog( "Progress", "Loading Definition..", 1, "Loading..", loader );
74        
75        progressDialog.setCancelLabel( "Run in background" );
76  
77        loader.start(progressDialog);
78        if( progressDialog != null )
79        	progressDialog.setVisible( true );
80        
81        setPreferredSize( new Dimension( 600, 500 ));
82     }
83  
84  	public ModelItem getModelItem()
85  	{
86  		return iface;
87  	}
88  
89  	public boolean onClose()
90  	{
91  		return true;
92  	}
93  
94  	public JComponent getComponent()
95  	{
96  		return this;
97  	}
98  	
99  	 private class Loader extends SwingWorker implements ProgressDialog.CancelHandler
100    {
101       private ProgressDialog progressDialog;
102 		private final String wsdlUrl;
103 		private JProgressBar progressBar;
104 
105       public Loader( String wsdlUrl )
106       {
107 			this.wsdlUrl = wsdlUrl;
108       }
109       
110       public void start(ProgressDialog progressDialog)
111 		{
112 			this.progressDialog = progressDialog;
113 			start();
114 		}
115 
116 		public Object construct()
117       {
118       	try
119          {
120          	List<String> urls = SchemaUtils.getDefinitionUrls( wsdlUrl );
121          	int tabCount = tabbedPane.getTabCount();
122          	
123             for (Iterator<String> iter = urls.iterator(); iter.hasNext();)
124             {
125    				addTab( iter.next() );
126             }
127             
128             while( tabCount-- > 0 )
129             	tabbedPane.remove( 0 );
130             
131             return null;
132          }
133          catch (Exception e)
134          {
135          	logger.error( "Failed to load WSDL; " + e.getClass().getSimpleName() + "; " + e.getMessage() ); 
136             e.printStackTrace();
137             return e;
138          }
139       }
140       
141       private void addTab(String url) throws Exception
142    	{
143       	int ix = url.startsWith( "file:" ) ? url.lastIndexOf( File.separatorChar ) : url.lastIndexOf( '/' );
144       	String title = url.substring( ix+1);
145       	
146       	if( progressBar != null )
147       		progressBar.setString( title );
148       	else if( progressDialog != null )
149       		progressDialog.setProgress( 1, title );
150       	
151       	XmlOptions options = new XmlOptions();
152       	options.setLoadLineNumbers();
153    		XmlObject xmlObject = XmlObject.Factory.parse( new URL( url ), options );
154    		JPanel panel = new JPanel( new BorderLayout() );
155    		JLabel label = new JLabel( url );
156    		label.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ));
157    		panel.add( label, BorderLayout.NORTH );
158    		
159    		JEditTextArea inputArea = new JXmlTextArea();
160    		StringWriter writer = new StringWriter();
161    		XmlUtils.serializePretty( xmlObject, writer );
162          String xmlString = writer.toString();
163    		inputArea.setText( xmlString );
164          inputArea.setCaretPosition( 0 );
165          inputArea.setEditable( false );
166          
167          panel.add( inputArea, BorderLayout.CENTER );
168 			tabbedPane.addTab( title, panel );
169    	}
170 
171       public void finished()
172       {
173       	if( progressDialog != null )
174       		progressDialog.setVisible( false );
175       }
176 
177 		public boolean onCancel()
178 		{
179 	
180 			progressBar = new JProgressBar(0, 1);
181 			progressBar.setSize( new Dimension( 120, 20 ));
182 		   progressBar.setStringPainted(true);
183 		   progressBar.setString("Loading Definition.." );
184 		   progressBar.setIndeterminate(true);
185 			
186 		   ButtonBarBuilder builder = ButtonBarBuilder.createLeftToRightBuilder();	
187          builder.addGlue();
188 			builder.addFixed( progressBar );
189          builder.addGlue();
190          builder.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ));
191          
192 			tabbedPane.addTab( "Loading.. ", builder.getPanel()  );
193 			return true;
194 		}
195    }
196 }