PageRenderTime 89ms CodeModel.GetById 79ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/datatransfer/ListVFSFileTransferable.java

#
Java | 93 lines | 60 code | 8 blank | 25 comment | 11 complexity | 782394fe319abd8f7d8efad287a60f48 MD5 | raw file
 1/*
 2 * ListVFSFileTransferable.java
 3 * :tabSize=8:indentSize=8:noTabs=false:
 4 * :folding=explicit:collapseFolds=1:
 5 *
 6 * Copyright (C) 2010 Matthieu Casanova
 7 *
 8 * This program is free software; you can redistribute it and/or
 9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 */
22package org.gjt.sp.jedit.datatransfer;
23
24import org.gjt.sp.jedit.io.FileVFS;
25import org.gjt.sp.jedit.io.VFSFile;
26
27import java.awt.datatransfer.DataFlavor;
28import java.awt.datatransfer.Transferable;
29import java.awt.datatransfer.UnsupportedFlavorException;
30import java.io.File;
31import java.io.IOException;
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.Collections;
35import java.util.List;
36
37/**
38 * @author Matthieu Casanova
39 * @since jEdit 4.4.x
40 */
41public class ListVFSFileTransferable implements Transferable
42{
43	public static final DataFlavor jEditFileList = new DataFlavor(List.class, "application/x-java-jEdit-list-vfsfile");
44	public static final DataFlavor[] supported = {jEditFileList, DataFlavor.stringFlavor, DataFlavor.javaFileListFlavor};
45	
46	private final List<VFSFile> files;
47
48	public ListVFSFileTransferable(VFSFile[] files)
49	{
50		this.files = Collections.unmodifiableList(Arrays.asList(files));
51	}
52
53	public DataFlavor[] getTransferDataFlavors()
54	{
55		return supported;
56	}
57
58	public boolean isDataFlavorSupported(DataFlavor flavor)
59	{
60		return jEditFileList.equals(flavor) || DataFlavor.stringFlavor.equals(flavor);
61	}
62
63	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
64	{
65		if (jEditFileList.equals(flavor))
66		{
67			return files;
68		}
69		else if (DataFlavor.stringFlavor.equals(flavor))
70		{
71			StringBuilder builder = new StringBuilder();
72			for (int i = 0; i < files.size(); i++)
73			{
74				VFSFile vfsFile = files.get(i);
75				if (i != 0)
76					builder.append('\n');
77				builder.append(vfsFile);
78			}
79			return builder.toString();
80		}
81		else if (DataFlavor.javaFileListFlavor.equals(flavor))
82		{
83			List<File> files = new ArrayList<File>(this.files.size());
84			for (VFSFile file : this.files)
85			{
86				if (file.getVFS() instanceof FileVFS)
87					files.add(new File(file.getPath()));
88			}
89			return files;
90		}
91		throw new UnsupportedFlavorException(flavor);
92	}
93}