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  package com.eviware.soapui.impl.wsdl.actions.iface.tools.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.List;
20  
21  import javax.swing.JPanel;
22  import javax.swing.JScrollPane;
23  import javax.swing.JTable;
24  import javax.swing.table.AbstractTableModel;
25  
26  import com.eviware.soapui.SoapUI;
27  import com.eviware.soapui.impl.wsdl.WsdlInterface;
28  import com.eviware.soapui.support.UISupport;
29  import com.eviware.soapui.support.types.StringToStringMap;
30  import com.eviware.x.form.XForm.ToolkitType;
31  import com.eviware.x.impl.swing.AbstractSwingXFormField;
32  
33  /***
34   * Swing JTable for holding Namespace/Package mappings
35   * 
36   * @author ole.matzura
37   */
38  
39  public class NamespaceTable extends AbstractSwingXFormField<JPanel>
40  {
41  	private JTable table;
42  	private JScrollPane scrollPane;
43  	private final WsdlInterface iface;
44  	private NamespaceTableModel namespaceTableModel;
45  	private boolean returnEmpty;
46  
47  	public NamespaceTable( WsdlInterface iface )
48  	{
49  		super( new JPanel( new BorderLayout() ) );
50  
51  		this.iface = iface;
52  
53  		namespaceTableModel = new NamespaceTableModel();
54  		table = new JTable( namespaceTableModel );
55  		scrollPane = new JScrollPane( table );
56  		scrollPane.setPreferredSize( new Dimension( 400, 150 ) );
57  		getComponent().add( scrollPane, BorderLayout.CENTER );
58  	}
59  
60  	public void setReturnEmpty( boolean returnEmpty )
61  	{
62  		this.returnEmpty = returnEmpty;
63  	}
64  
65  	public JPanel getComponent( ToolkitType toolkitType )
66  	{
67  		if( toolkitType == ToolkitType.SWT )
68  		{
69  			UISupport.showErrorMessage( "SWT not supported by namespace table" );
70  			return null;
71  		}
72  
73  		return getComponent();
74  	}
75  
76  	public void setValue( String value )
77  	{
78  		namespaceTableModel.setMappings( StringToStringMap.fromXml( value ) );
79  	}
80  
81  	public String getValue()
82  	{
83  		return namespaceTableModel.getMappings().toXml();
84  	}
85  
86  	private class NamespaceTableModel extends AbstractTableModel
87  	{
88  		private List<String> namespaces = new ArrayList<String>();
89  		private List<String> packages;
90  
91  		public NamespaceTableModel()
92  		{
93  			try
94  			{
95  				if( iface != null )
96  					namespaces.addAll( iface.getWsdlContext().getInterfaceDefinition().getDefinedNamespaces() );
97  			}
98  			catch( Exception e )
99  			{
100 				SoapUI.logError( e );
101 			}
102 
103 			packages = new ArrayList<String>( Arrays.asList( new String[namespaces.size()] ) );
104 		}
105 
106 		public void setMappings( StringToStringMap mapping )
107 		{
108 			for( int c = 0; c < namespaces.size(); c++ )
109 			{
110 				if( mapping.containsKey( namespaces.get( c ) ) )
111 				{
112 					packages.set( c, mapping.get( namespaces.get( c ) ) );
113 				}
114 				else
115 				{
116 					packages.set( c, "" );
117 				}
118 			}
119 
120 			fireTableDataChanged();
121 		}
122 
123 		public int getRowCount()
124 		{
125 			return namespaces.size();
126 		}
127 
128 		public int getColumnCount()
129 		{
130 			return 2;
131 		}
132 
133 		public Class<?> getColumnClass( int columnIndex )
134 		{
135 			return String.class;
136 		}
137 
138 		public String getColumnName( int column )
139 		{
140 			return column == 0 ? "Namespace" : "Package";
141 		}
142 
143 		public boolean isCellEditable( int rowIndex, int columnIndex )
144 		{
145 			return columnIndex == 1;
146 		}
147 
148 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
149 		{
150 			if( columnIndex == 1 )
151 				packages.set( rowIndex, aValue.toString() );
152 		}
153 
154 		public Object getValueAt( int rowIndex, int columnIndex )
155 		{
156 			if( columnIndex == 0 )
157 				return namespaces.get( rowIndex );
158 			else
159 				returnong> packages.get( rowIndex );
160 		}
161 
162 		public StringToStringMap getMappings()
163 		{
164 			StringToStringMap result = new StringToStringMap();
165 			for( int c = 0; c < namespaces.size(); c++ )
166 			{
167 				String pkg = packages.get( c );
168 				if( returnEmpty || ( pkg != null && pkg.trim().length() > 0 ) )
169 				{
170 					result.put( namespaces.get( c ), pkg == null ? "" : pkg.trim() );
171 				}
172 			}
173 
174 			return result;
175 		}
176 	}
177 
178 	@Override
179 	public boolean isMultiRow()
180 	{
181 		return true;
182 	}
183 }