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