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.support.wss.support;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Color;
17  import java.awt.Component;
18  import java.awt.Dimension;
19  import java.awt.event.ActionEvent;
20  import java.util.List;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.DefaultCellEditor;
24  import javax.swing.JButton;
25  import javax.swing.JComboBox;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTable;
29  import javax.swing.event.ListSelectionEvent;
30  import javax.swing.event.ListSelectionListener;
31  import javax.swing.table.AbstractTableModel;
32  
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.components.JXToolBar;
35  import com.eviware.soapui.support.types.StringToStringMap;
36  
37  public class WSPartsTable extends JPanel
38  {
39  	private final List<StringToStringMap> parts;
40  	private PartsTableModel partsTableModel;
41  	private JTable partsTable;
42  	private JButton removePartButton;
43  
44  	public WSPartsTable(List<StringToStringMap> parts)
45  	{
46  		super( new BorderLayout() );
47  		this.parts = parts;
48  		
49  		partsTableModel = new PartsTableModel();
50  		partsTable = new JTable( partsTableModel );
51  		partsTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
52  
53  			public void valueChanged( ListSelectionEvent e )
54  			{
55  				removePartButton.setEnabled( partsTable.getSelectedRow() != -1 );
56  			}} );
57  		
58  		partsTable.getColumnModel().getColumn( 3 ).setCellEditor( new DefaultCellEditor( 
59  					new JComboBox( new String[] {"Content", "Element"} )) );
60  		
61  		JScrollPane scrollPane = new JScrollPane( partsTable);
62  		scrollPane.setBackground( Color.WHITE );
63  		scrollPane.setOpaque( true );
64  		add( scrollPane, BorderLayout.CENTER );
65  		add( buildToolbar(), BorderLayout.NORTH );
66  		
67  		setPreferredSize( new Dimension( 350, 150 ) );
68  	}
69  	
70  	private Component buildToolbar()
71  	{
72  		JXToolBar toolbar = UISupport.createSmallToolbar();
73  		
74  		toolbar.addFixed( UISupport.createToolbarButton( new AddPartAction() ));
75  		removePartButton = UISupport.createToolbarButton( new RemovePartAction() );
76  		toolbar.addFixed( removePartButton);
77  		
78  		return toolbar;
79  	}
80  
81  	private class PartsTableModel extends AbstractTableModel
82  	{
83  		public int getColumnCount()
84  		{
85  			return 4;
86  		}
87  
88  		public int getRowCount()
89  		{
90  			return parts.size();
91  		}
92  
93  		@Override
94  		public boolean isCellEditable( int rowIndex, int columnIndex )
95  		{
96  			return true;
97  		}
98  		
99  		@Override
100 		public String getColumnName( int column )
101 		{
102 			switch( column )
103 			{
104 			case 0 : return "ID";
105 			case 1 : return "Name";
106 			case 2 : return "Namespace";
107 			case 3 : return "Encode";
108 			}
109 			
110 			return null;
111 		}
112 
113 		@Override
114 		public void setValueAt( Object aValue, int rowIndex, int columnIndex )
115 		{
116 			StringToStringMap part = parts.get( rowIndex );
117 			if( aValue == null )
118 				aValue = "";
119 			
120 			switch( columnIndex )
121 			{
122 				case 0 :
123 					part.put( "name", "" );
124 					fireTableCellUpdated( rowIndex, 1 );
125 					part.put( "id", aValue.toString() ); return;
126 				case 1 : 
127 					part.put( "id", "" );
128 					fireTableCellUpdated( rowIndex, 0 );
129 					part.put( "name", aValue.toString() ); return;
130 				case 2 : 
131 					part.put( "id", "" );
132 					fireTableCellUpdated( rowIndex, 0 );
133 					part.put( "namespace", aValue.toString() ); return;
134 				case 3 : 
135 					part.put( "enc", aValue.toString() ); return;
136 			}
137 		}
138 
139 		public Object getValueAt( int rowIndex, int columnIndex )
140 		{
141 			StringToStringMap part = parts.get( rowIndex );
142 			
143 			switch( columnIndex )
144 			{
145 				case 0 : return part.get( "id" );
146 				case 1 : return part.get( "name" );
147 				case 2 : return part.get( "namespace" );
148 				case 3 : return part.get( "enc" );
149 			}
150 			
151 			return null;
152 		}
153 
154 		public void remove( int row )
155 		{
156 			parts.remove( row );
157 			fireTableRowsDeleted( row, row );
158 		}
159 
160 		public void addParts( StringToStringMap map )
161 		{
162 			parts.add( map );
163 			fireTableRowsInserted( parts.size()-1, parts.size()-1 );
164 		}}
165 	
166 	private class AddPartAction extends AbstractAction
167 	{
168 		public AddPartAction()
169 		{
170 			putValue( SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ));
171 			putValue( SHORT_DESCRIPTION, "Adds a new part" );
172 		}
173 		
174 		public void actionPerformed( ActionEvent e )
175 		{
176 			partsTableModel.addParts( new StringToStringMap() );
177 		}
178 	}
179 	
180 	private class RemovePartAction extends AbstractAction
181 	{
182 		public RemovePartAction()
183 		{
184 			putValue( SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ));
185 			putValue( SHORT_DESCRIPTION, "Removes the selected part" );
186 			setEnabled( false );
187 		}
188 		
189 		public void actionPerformed( ActionEvent e )
190 		{
191 			int row = partsTable.getSelectedRow();
192 			if( row == -1 )
193 				return;
194 			
195 			if( UISupport.confirm( "Remove selected Part?", "Remove Part" ))
196 			{
197 				partsTableModel.remove( row );
198 			}
199 		}
200 	}
201 }