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