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.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.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 AttachmentsTableModel extends AbstractTableModel implements PropertyChangeListener,
34  			AttachmentTableModel
35  {
36  
37  	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.isReadOnly() )
61  		{
62  			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.isReadOnly() )
78  		{
79  			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.isReadOnly() ? 6 : 5;
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.getName();
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 		default:
121 			return null;
122 		}
123 	}
124 
125 	public int findColumn( String columnName )
126 	{
127 		if( columnName.equals( "Name" ) )
128 			return 0;
129 		else if( columnName.equals( "Content type" ) )
130 			return 1;
131 		else if( columnName.equals( "Size" ) )
132 			return 2;
133 		else if( columnName.equals( "Part" ) )
134 			return 3;
135 		else if( columnName.equals( "Type" ) )
136 			return 4;
137 
138 		return -1;
139 	}
140 
141 	public String getColumnName( int column )
142 	{
143 		if( column == 0 )
144 			return "Name";
145 		else if( column == 1 )
146 			return "Content type";
147 		else if( column == 2 )
148 			return "Size";
149 		else if( column == 3 )
150 			return "Part";
151 		else if( column == 4 )
152 			return "Type";
153 		else if( column == 5 )
154 			return "ContentID";
155 		else
156 			return null;
157 	}
158 
159 	public boolean isCellEditable( int rowIndex, int columnIndex )
160 	{
161 		return !container.isReadOnly() && ( columnIndex == 1 || columnIndex == 3 || columnIndex == 5 );
162 	}
163 
164 	public void setValueAt( Object aValue, int rowIndex, int columnIndex )
165 	{
166 		if( !!container.isReadOnly() )
167 			return;
168 
169 		WsdlAttachment att = ( WsdlAttachment ) container.getAttachmentAt( rowIndex );
170 		if( columnIndex == 1 )
171 			att.setContentType( ( String ) aValue );
172 		else if( columnIndex == 3 )
173 			att.setPart( ( String ) aValue );
174 		else if( columnIndex == 5 )
175 			att.setContentID( ( String ) aValue );
176 
177 		fireTableRowsUpdated( rowIndex, rowIndex );
178 	}
179 
180 	/***
181 	 * Update table when attachments or response changes
182 	 */
183 
184 	public void propertyChange( PropertyChangeEvent evt )
185 	{
186 		fireTableDataChanged();
187 	}
188 }