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.panels.request.components.editor.inspectors.httpheaders;
    14
    15  import java.awt.BorderLayout;
    16  import java.awt.event.ActionEvent;
    17  import java.beans.PropertyChangeEvent;
    18  import java.beans.PropertyChangeListener;
    19
    20  import javax.swing.AbstractAction;
    21  import javax.swing.JButton;
    22  import javax.swing.JComponent;
    23  import javax.swing.JPanel;
    24  import javax.swing.JScrollPane;
    25  import javax.swing.JTable;
    26  import javax.swing.SwingUtilities;
    27  import javax.swing.event.ListSelectionEvent;
    28  import javax.swing.event.ListSelectionListener;
    29  import javax.swing.event.TableModelEvent;
    30  import javax.swing.event.TableModelListener;
    31
    32  import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction;
    33  import com.eviware.soapui.impl.wsdl.panels.request.StringToStringMapTableModel;
    34  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlLocation;
    35  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.AbstractXmlInspector;
    36  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
    37  import com.eviware.soapui.support.UISupport;
    38  import com.eviware.soapui.support.components.JXToolBar;
    39
    40  public class HttpHeadersInspector extends AbstractXmlInspector implements PropertyChangeListener
    41  {
    42  	private StringToStringMapTableModel headersTableModel;
    43  	private final HttpHeadersInspectorModel model;
    44  	private JTable headersTable;
    45  	private JPanel panel;
    46  	private JButton removeButton;
    47  	public boolean changing;
    48
    49  	protected HttpHeadersInspector( HttpHeadersInspectorModel response )
    50  	{
    51  		super( "HTTP Headers (" + response.getHeaders().size() + ")", "Additional HTTP Headers for this message",
    52  					true, HttpHeadersInspectorFactory.INSPECTOR_ID );
    53  		this.model = response;
    54
    55  		response.addPropertyChangeListener( this );
    56  	}
    57
    58  	public JComponent getComponent()
    59  	{
    60  		if( panel != null )
    61  			return panel;
    62
    63  		headersTableModel = new StringToStringMapTableModel( model.getHeaders(), "Header", "Value",
    64  					!model.isReadOnly() );
    65  		headersTableModel.addTableModelListener( new TableModelListener()
    66  		{
    67  			public void tableChanged( TableModelEvent arg0 )
    68  			{
    69  				model.setHeaders( headersTableModel.getData() );
    70  				setTitle( "HTTP Headers (" + model.getHeaders().size() + ")" );
    71  			}} );
    72
    73  		headersTable = new JTable( headersTableModel );
    74
    75  		panel = new JPanel( new BorderLayout() );
    76  		panel.add( new JScrollPane( headersTable ), BorderLayout.CENTER );
    77
    78  		if( !model.isReadOnly() )
    79  		{
    80  			headersTable.setSurrendersFocusOnKeystroke( true );
    81  			headersTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    82
    83  			JXToolBar builder = UISupport.createToolbar();
    84  			builder.addFixed( new JButton( new AddAction() ));
    85  			builder.addRelatedGap();
    86  			removeButton = new JButton( new RemoveAction() );
    87  			builder.addFixed( removeButton);
    88  			builder.addGlue();
    89  			builder.addFixed( UISupport.createToolbarButton( new ShowOnlineHelpAction(HelpUrls.HEADERS_HELP_URL)));
    90
    91  			panel.add( builder, BorderLayout.NORTH );
    92
    93  			headersTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
    94
    95  				public void valueChanged( ListSelectionEvent e )
    96  				{
    97  					removeButton.setEnabled( headersTable.getSelectedRow() != -1 );
    98  				}} );
    99
    100 			if( headersTable.getRowCount() > 0 )
    101 				headersTable.setRowSelectionInterval( 0, 0 );
    102 			else
    103 				removeButton.setEnabled( false );
    104 		}
    105
    106 		return panel;
    107 	}
    108
    109 	public void locationChanged( XmlLocation location )
    110 	{
    111 	}
    112
    113 	@Override
    114 	public void release()
    115 	{
    116 		super.release();
    117 		model.release();
    118 		model.removePropertyChangeListener(this );
    119 	}
    120
    121 	public void propertyChange( PropertyChangeEvent evt )
    122 	{
    123 		if( !changing )
    124 			headersTableModel.setData( model.getHeaders() );
    125 	}
    126
    127 	private final class RemoveAction extends AbstractAction
    128 	{
    129 		private RemoveAction()
    130 		{
    131 			super();
    132 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/remove_property.gif" ));
    133 			putValue( AbstractAction.SHORT_DESCRIPTION, "Removes the selected custom HTTP Header from this message" );
    134 		}
    135
    136 		public void actionPerformed( ActionEvent arg0 )
    137 		{
    138 			int row = headersTable.getSelectedRow();
    139 			if( row != -1 && UISupport.confirm( "Delete selected header?", "Remove Header" ) )
    140 			{
    141 				changing = true;
    142 				headersTableModel.remove( row );
    143 				changing = false;
    144 			}
    145 		}
    146 	}
    147
    148 	private final class AddAction extends AbstractAction
    149 	{
    150 		private AddAction()
    151 		{
    152 			super();
    153 			putValue( AbstractAction.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ));
    154 			putValue( AbstractAction.SHORT_DESCRIPTION, "Adds a custom HTTP Header to this messagae" );
    155 		}
    156
    157 		public void actionPerformed( ActionEvent arg0 )
    158 		{
    159 			Object header = UISupport.prompt( "Specify name of header to add", "Add HTTP Header", "" );
    160 			if( header != null )
    161 			{
    162 				changing = true;
    163 				headersTableModel.add( header.toString(), "" );
    164 				SwingUtilities.invokeLater( new Runnable()
    165 				{
    166 					public void run()
    167 					{
    168 						int row = headersTable.getRowCount()-1;
    169 						headersTable.scrollRectToVisible( headersTable.getCellRect( row,1,true ) );
    170 						headersTable.setRowSelectionInterval( row, row );
    171
    172 						SwingUtilities.invokeLater( new Runnable()
    173 						{
    174 							public void run()
    175 							{
    176 								headersTable.editCellAt(headersTable.getRowCount()-1, 1);
    177 								headersTable.getEditorComponent().requestFocusInWindow();
    178 							}
    179 						} );
    180 					}
    181 				} );
    182
    183 				changing = false;
    184 			}
    185 		}
    186 	}
    187
    188 }