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