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