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