View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2009 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  /*
14   *  soapUI, copyright (C) 2004-2009 eviware.com 
15   *
16   *  soapUI is free software; you can redistribute it and/or modify it under the 
17   *  terms of version 2.1 of the GNU Lesser General Public License as published by 
18   *  the Free Software Foundation.
19   *
20   *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
21   *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
22   *  See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
26  
27  import java.beans.PropertyChangeEvent;
28  import java.beans.PropertyChangeListener;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import javax.swing.JComponent;
33  
34  import com.eviware.soapui.impl.rest.RestMethod;
35  import com.eviware.soapui.impl.rest.RestRepresentation;
36  import com.eviware.soapui.impl.rest.panels.method.RestRepresentationsTable;
37  import com.eviware.soapui.model.iface.Submit;
38  import com.eviware.soapui.model.iface.SubmitContext;
39  import com.eviware.soapui.support.components.JXToolBar;
40  import com.eviware.soapui.support.editor.EditorView;
41  import com.eviware.soapui.support.editor.inspectors.AbstractXmlInspector;
42  import com.eviware.soapui.support.editor.views.xml.raw.RawXmlEditorFactory;
43  import com.eviware.soapui.support.editor.xml.XmlDocument;
44  
45  public abstract class AbstractRestRepresentationsInspector extends AbstractXmlInspector implements
46  		PropertyChangeListener
47  {
48  	private RestRepresentationsTable representationTable;
49  	private final RestMethod restMethod;
50  	private List<RestRepresentation.Type> types;
51  
52  	protected AbstractRestRepresentationsInspector( RestMethod restMethod, String name, String description,
53  			RestRepresentation.Type[] types )
54  	{
55  		super( name, description, true, RestRepresentationsInspectorFactory.INSPECTOR_ID );
56  		this.restMethod = restMethod;
57  		this.types = Arrays.asList( types );
58  
59  		restMethod.addPropertyChangeListener( "representations", this );
60  		updateLabel();
61  	}
62  
63  	public JComponent getComponent()
64  	{
65  		if( representationTable == null )
66  		{
67  			buildUI();
68  		}
69  
70  		return representationTable;
71  	}
72  
73  	protected void addToToolbar( JXToolBar toolbar )
74  	{
75  	}
76  
77  	protected void buildUI()
78  	{
79  		representationTable = new RestRepresentationsTable( restMethod,
80  				types.toArray( new RestRepresentation.Type[] {} ), true )
81  		{
82  			protected JXToolBar buildToolbar()
83  			{
84  				JXToolBar toolbar = super.buildToolbar();
85  				addToToolbar( toolbar );
86  				return toolbar;
87  			}
88  		};
89  	}
90  
91  	public RestMethod getMethod()
92  	{
93  		return restMethod;
94  	}
95  
96  	@Override
97  	public boolean isEnabledFor( EditorView<XmlDocument> view )
98  	{
99  		return !view.getViewId().equals( RawXmlEditorFactory.VIEW_ID );
100 	}
101 
102 	public boolean beforeSubmit( Submit submit, SubmitContext context )
103 	{
104 		return true;
105 	}
106 
107 	@Override
108 	public void release()
109 	{
110 		representationTable.release();
111 		restMethod.removePropertyChangeListener( "representations", this );
112 	}
113 
114 	public void propertyChange( PropertyChangeEvent evt )
115 	{
116 		updateLabel();
117 	}
118 
119 	private void updateLabel()
120 	{
121 		int cnt = 0;
122 		for( RestRepresentation representation : restMethod.getRepresentations() )
123 		{
124 			if( types.contains( representation.getType() ) )
125 				cnt++ ;
126 		}
127 
128 		setTitle( "Representations (" + cnt + ")" );
129 	}
130 }