2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.ide
;
19 import com
.intellij
.openapi
.actionSystem
.DataContext
;
20 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
21 import com
.intellij
.openapi
.ide
.CopyPasteManager
;
22 import com
.intellij
.openapi
.project
.Project
;
23 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.psi
.PsiDirectory
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.PsiFileSystemItem
;
28 import com
.intellij
.psi
.PsiManager
;
29 import com
.intellij
.refactoring
.copy
.CopyFilesOrDirectoriesHandler
;
31 import java
.awt
.datatransfer
.DataFlavor
;
32 import java
.awt
.datatransfer
.Transferable
;
33 import java
.awt
.datatransfer
.UnsupportedFlavorException
;
35 import java
.io
.IOException
;
36 import java
.util
.ArrayList
;
37 import java
.util
.List
;
42 public class FileListPasteProvider
implements PasteProvider
{
43 public void performPaste(DataContext dataContext
) {
44 Project project
= LangDataKeys
.PROJECT
.getData(dataContext
);
45 final IdeView ideView
= LangDataKeys
.IDE_VIEW
.getData(dataContext
);
46 if (project
== null || ideView
== null) return;
49 final Transferable contents
= CopyPasteManager
.getInstance().getContents();
50 //noinspection unchecked
51 fileList
= (List
<File
>)contents
.getTransferData(DataFlavor
.javaFileListFlavor
);
53 catch (UnsupportedFlavorException e
) {
56 catch (IOException e
) {
59 if (fileList
== null) return;
60 List
<PsiElement
> elements
= new ArrayList
<PsiElement
>();
61 for (File file
: fileList
) {
62 final VirtualFile vFile
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(file
);
64 final PsiManager instance
= PsiManager
.getInstance(project
);
65 PsiFileSystemItem item
= vFile
.isDirectory() ? instance
.findDirectory(vFile
) : instance
.findFile(vFile
);
71 if (elements
.size() > 0) {
72 final PsiDirectory dir
= ideView
.getOrChooseDirectory();
74 new CopyFilesOrDirectoriesHandler().doCopy(elements
.toArray(new PsiElement
[elements
.size()]), dir
);
79 public boolean isPastePossible(DataContext dataContext
) {
83 public boolean isPasteEnabled(DataContext dataContext
) {
84 final Transferable contents
= CopyPasteManager
.getInstance().getContents();
85 final IdeView ideView
= LangDataKeys
.IDE_VIEW
.getData(dataContext
);
86 return contents
.isDataFlavorSupported(DataFlavor
.javaFileListFlavor
) && ideView
!= null;