View Javadoc

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