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