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.attachments;
    14
    15  import java.beans.PropertyChangeEvent;
    16  import java.beans.PropertyChangeListener;
    17  import java.io.File;
    18  import java.io.IOException;
    19  import java.util.Arrays;
    20
    21  import javax.swing.table.AbstractTableModel;
    22
    23  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    24  import com.eviware.soapui.impl.wsdl.support.WsdlAttachment;
    25  import com.eviware.soapui.model.iface.Attachment;
    26
    27  /***
    28   * TableModel for Request Attachments
    29   *
    30   * @author emibre
    31   */
    32
    33  public class RequestAttachmentTableModel extends AbstractTableModel implements PropertyChangeListener,
    34  			AttachmentTableModel
    35  {
    36
    37  	WsdlRequest request;
    38  	boolean isRequest = false;
    39
    40  	/*** Creates a new instance of AttachmentTableModel */
    41  	public RequestAttachmentTableModel( WsdlRequest request, boolean isRequest )
    42  	{
    43  		this.request = request;
    44  		this.isRequest = isRequest;
    45
    46  		this.request.addPropertyChangeListener( this );
    47  	}
    48
    49  	public void release()
    50  	{
    51  		request.removePropertyChangeListener( this );
    52  	}
    53
    54  	/*
    55  	 * (non-Javadoc)
    56  	 *
    57  	 * @see com.eviware.soapui.impl.wsdl.panels.attachments.AttachmentTableModel#addFile(java.io.File,
    58  	 *      boolean)
    59  	 */
    60  	public void addFile( File file, boolean cacheInRequest ) throws IOException
    61  	{
    62  		if( isRequest )
    63  		{
    64  			Attachment attachment = request.attachFile( file, cacheInRequest );
    65  			attachment.setContentType( ContentTypeHandler.getContentTypeFromFilename( file.getName() ) );
    66  		}
    67
    68  		this.fireTableRowsInserted( request.getAttachmentCount(), request.getAttachmentCount() );
    69  	}
    70
    71  	public void removeAttachment( int[] rowIndexes )
    72  	{
    73  		Arrays.sort( rowIndexes );
    74  		for( int i = rowIndexes.length - 1; i >= 0; i-- )
    75  			removeAttachment( rowIndexes[i] );
    76  	}
    77
    78  	public void removeAttachment( int rowIndex )
    79  	{
    80  		if( isRequest )
    81  		{
    82  			request.removeAttachment( request.getAttachmentAt( rowIndex ) );
    83  			this.fireTableRowsDeleted( rowIndex, rowIndex );
    84  		}
    85  	}
    86
    87  	public int getRowCount()
    88  	{
    89  		if( isRequest )
    90  			return request.getAttachmentCount();
    91  		else
    92  		{
    93  			try
    94  			{
    95  				// Response may not exist yet and attachments may be null
    96  				return request.getResponse().getAttachments().length;
    97  			}
    98  			catch( Exception e )
    99  			{
    100 				return 0;
    101 			}
    102 		}
    103 	}
    104
    105 	public int getColumnCount()
    106 	{
    107 		return isRequest ? 6 : 5;
    108 	}
    109
    110 	public Attachment getAttachmentAt( int rowIndex )
    111 	{
    112 		if( isRequest )
    113 			return request.getAttachmentAt( rowIndex );
    114 		else
    115 			return request.getResponse().getAttachments()[rowIndex];
    116 	}
    117
    118 	public Object getValueAt( int rowIndex, int columnIndex )
    119 	{
    120 		if( rowIndex > getRowCount() )
    121 			return null;
    122
    123 		Attachment att = null;
    124 		if( isRequest )
    125 			att = request.getAttachmentAt( rowIndex );
    126 		else
    127 			att = request.getResponse().getAttachments()[rowIndex];
    128
    129 		switch( columnIndex )
    130 		{
    131 		case 0:
    132 			return att.getName();
    133 		case 1:
    134 			return att.getContentType();
    135 		case 2:
    136 			return att.getSize();
    137 		case 3:
    138 			return att.getPart();
    139 		case 4:
    140 			return att.getAttachmentType();
    141 		case 5:
    142 			return att.getContentID();
    143 		default:
    144 			return null;
    145 		}
    146 	}
    147
    148 	public int findColumn( String columnName )
    149 	{
    150 		if( columnName.equals( "Name" ) )
    151 			return 0;
    152 		else if( columnName.equals( "Content type" ) )
    153 			return 1;
    154 		else if( columnName.equals( "Size" ) )
    155 			return 2;
    156 		else if( columnName.equals( "Part" ) )
    157 			return 3;
    158 		else if( columnName.equals( "Type" ) )
    159 			return 4;
    160
    161 		return -1;
    162 	}
    163
    164 	public String getColumnName( int column )
    165 	{
    166 		if( column == 0 )
    167 			return "Name";
    168 		else if( column == 1 )
    169 			return "Content type";
    170 		else if( column == 2 )
    171 			return "Size";
    172 		else if( column == 3 )
    173 			return "Part";
    174 		else if( column == 4 )
    175 			return "Type";
    176 		else if( column == 5 )
    177 			return "ContentID";
    178 		else
    179 			return null;
    180 	}
    181
    182 	public boolean isCellEditable( int rowIndex, int columnIndex )
    183 	{
    184 		return isRequest && ( columnIndex == 1 || columnIndex == 3 || columnIndex == 5 );
    185 	}
    186
    187 	public void setValueAt( Object aValue, int rowIndex, int columnIndex )
    188 	{
    189 		if( !isRequest )
    190 			return;
    191
    192 		WsdlAttachment att = ( WsdlAttachment ) request.getAttachmentAt( rowIndex );
    193 		if( columnIndex == 1 )
    194 			att.setContentType( ( String ) aValue );
    195 		else if( columnIndex == 3 )
    196 			att.setPart( ( String ) aValue );
    197 		else if( columnIndex == 5 )
    198 			att.setContentID( ( String ) aValue );
    199
    200 		fireTableRowsUpdated( rowIndex, rowIndex );
    201 	}
    202
    203 	/***
    204 	 * Update table when attachments or response changes
    205 	 */
    206
    207 	public void propertyChange( PropertyChangeEvent evt )
    208 	{
    209 		fireTableDataChanged();
    210 	}
    211 }