View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.awt.datatransfer.DataFlavor;
16  import java.awt.datatransfer.Transferable;
17  import java.awt.datatransfer.UnsupportedFlavorException;
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.List;
21  
22  import javax.swing.JComponent;
23  import javax.swing.TransferHandler;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.support.UISupport;
27  
28  /***
29   * Handles drop of files on the AttachmentPanel
30   * 
31   * @author emibre
32   */
33  
34  public class FileTransferHandler extends TransferHandler
35  {
36  	private DataFlavor fileFlavor;
37  	private AttachmentTableModel attachmentModel;
38  
39  	/*** Creates a new instance of FileTransferHandler */
40  	public FileTransferHandler( AttachmentTableModel attachmentModel )
41  	{
42  		fileFlavor = DataFlavor.javaFileListFlavor;
43  		this.attachmentModel = attachmentModel;
44  	}
45  
46  	public boolean canImport( JComponent c, DataFlavor[] flavors )
47  	{
48  		return hasFileFlavor( flavors );
49  	}
50  
51  	private boolean hasFileFlavor( DataFlavor[] flavors )
52  	{
53  		for( int i = 0; i < flavors.length; i++ )
54  		{
55  			if( fileFlavor.equals( flavors[i] ) )
56  			{
57  				return true;
58  			}
59  		}
60  		return false;
61  	}
62  
63  	@SuppressWarnings( "unchecked" )
64  	public boolean importData( JComponent c, Transferable t )
65  	{
66  		try
67  		{
68  			List<File> files = ( List<File> )t.getTransferData( fileFlavor );
69  			for( File f : files )
70  			{
71  				System.out.println( "Got a file: " + f.getName() );
72  				Boolean retval = UISupport.confirmOrCancel( "Cache attachment in request?", "Att Attachment" );
73  				if( retval == null )
74  					return false;
75  
76  				attachmentModel.addFile( f, retval );
77  			}
78  
79  		}
80  		catch( IOException ex )
81  		{
82  			SoapUI.logError( ex );
83  		}
84  		catch( UnsupportedFlavorException ex )
85  		{
86  			SoapUI.logError( ex );
87  		}
88  		return false;
89  	}
90  
91  	public int getSourceActions( JComponent c )
92  	{
93  		return COPY_OR_MOVE;
94  	}
95  }