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.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.support.UISupport;
26  
27  /***
28   * 
29   * @author emibre
30   */
31  public class FileTransferHandler extends TransferHandler
32  {
33  	private DataFlavor fileFlavor;
34  	private AttachmentTableModel attachmentModel;
35  
36  	/*** Creates a new instance of FileTransferHandler */
37  	public FileTransferHandler(AttachmentTableModel attachmentModel)
38  	{
39  		fileFlavor = DataFlavor.javaFileListFlavor;
40  		this.attachmentModel = attachmentModel;
41  	}
42  
43  	public boolean canImport(JComponent c, DataFlavor[] flavors)
44  	{
45  		return hasFileFlavor(flavors);
46  	}
47  
48  	private boolean hasFileFlavor(DataFlavor[] flavors)
49  	{
50  		for (int i = 0; i < flavors.length; i++)
51  		{
52  			if (fileFlavor.equals(flavors[i]))
53  			{
54  				return true;
55  			}
56  		}
57  		return false;
58  	}
59  
60  	public boolean importData(JComponent c, Transferable t)
61  	{
62  		Object obj;
63  		try
64  		{
65  			List<File> files = (List<File>) t.getTransferData(fileFlavor);
66  			for (File f : files)
67  			{
68  				System.out.println("Got a file: " + f.getName());
69  				Boolean retval = UISupport.confirmOrCancel("Cache attachment in request?", "Att Attachment");
70  				if (retval == null)
71  					return false;
72  
73  				attachmentModel.addFile(f, retval);
74  			}
75  
76  		}
77  		catch (IOException ex)
78  		{
79  			ex.printStackTrace();
80  		}
81  		catch (UnsupportedFlavorException ex)
82  		{
83  			ex.printStackTrace();
84  		}
85  		return false;
86  	}
87  
88  	public int getSourceActions(JComponent c)
89  	{
90  		return COPY_OR_MOVE;
91  	}
92  }