ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / ide / FileListPasteProvider.java
blobe1a5463e49777530ba0d34d955ebac7ac6c76c20
1 /*
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;
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.List;
39 /**
40 * @author yole
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;
47 List<File> fileList;
48 try {
49 final Transferable contents = CopyPasteManager.getInstance().getContents();
50 //noinspection unchecked
51 fileList = (List<File>)contents.getTransferData(DataFlavor.javaFileListFlavor);
53 catch (UnsupportedFlavorException e) {
54 return;
56 catch (IOException e) {
57 return;
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);
63 if (vFile != null) {
64 final PsiManager instance = PsiManager.getInstance(project);
65 PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
66 if (item != null) {
67 elements.add(item);
71 if (elements.size() > 0) {
72 final PsiDirectory dir = ideView.getOrChooseDirectory();
73 if (dir != null) {
74 new CopyFilesOrDirectoriesHandler().doCopy(elements.toArray(new PsiElement[elements.size()]), dir);
79 public boolean isPastePossible(DataContext dataContext) {
80 return true;
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;