update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / ide / JavaFilePasteProvider.java
blob599dd6aa4fe6ba484e8c9b64154490673b4e984b
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.
16 package com.intellij.ide;
18 import com.intellij.lang.StdLanguages;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.actionSystem.DataKeys;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.command.CommandProcessor;
23 import com.intellij.openapi.editor.Document;
24 import com.intellij.openapi.fileEditor.OpenFileDescriptor;
25 import com.intellij.openapi.ide.CopyPasteManager;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.util.TextRange;
28 import com.intellij.psi.*;
29 import com.intellij.psi.codeStyle.CodeStyleManager;
30 import com.intellij.util.IncorrectOperationException;
31 import org.jetbrains.annotations.Nullable;
33 import java.awt.datatransfer.DataFlavor;
34 import java.awt.datatransfer.Transferable;
36 /**
37 * @author yole
39 public class JavaFilePasteProvider implements PasteProvider {
40 public void performPaste(final DataContext dataContext) {
41 final Project project = DataKeys.PROJECT.getData(dataContext);
42 final IdeView ideView = DataKeys.IDE_VIEW.getData(dataContext);
43 if (project == null || ideView == null) return;
44 final PsiJavaFile javaFile = createJavaFileFromClipboardContent(project);
45 if (javaFile == null) return;
46 final PsiClass[] classes = javaFile.getClasses();
47 if (classes.length != 1) return;
48 final PsiDirectory targetDir = ideView.getOrChooseDirectory();
49 if (targetDir == null) return;
50 ApplicationManager.getApplication().runWriteAction(new Runnable() {
51 public void run() {
52 PsiFile file;
53 try {
54 file = targetDir.createFile(classes[0].getName() + ".java");
56 catch (IncorrectOperationException e) {
57 return;
59 final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
60 document.setText(javaFile.getText());
61 PsiDocumentManager.getInstance(project).commitDocument(document);
62 if (file instanceof PsiJavaFile) {
63 updatePackageStatement((PsiJavaFile) file, targetDir);
65 new OpenFileDescriptor(project, file.getVirtualFile()).navigate(true);
67 });
70 private static void updatePackageStatement(final PsiJavaFile javaFile, final PsiDirectory targetDir) {
71 final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(targetDir);
72 if (aPackage == null) return;
73 final PsiPackageStatement oldStatement = javaFile.getPackageStatement();
74 final Project project = javaFile.getProject();
75 if ((oldStatement != null && !oldStatement.getPackageName().equals(aPackage.getQualifiedName()) ||
76 (oldStatement == null && aPackage.getQualifiedName().length() > 0))) {
77 CommandProcessor.getInstance().executeCommand(project, new Runnable() {
78 public void run() {
79 try {
80 PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
81 final PsiPackageStatement newStatement = factory.createPackageStatement(aPackage.getQualifiedName());
82 if (oldStatement != null) {
83 oldStatement.replace(newStatement);
85 else {
86 final PsiElement addedStatement = javaFile.addAfter(newStatement, null);
87 final TextRange textRange = addedStatement.getTextRange();
88 // ensure line break is added after the statement
89 CodeStyleManager.getInstance(project).reformatRange(javaFile, textRange.getStartOffset(), textRange.getEndOffset()+1);
92 catch (IncorrectOperationException e) {
93 // ignore
96 }, "Updating package statement", null);
100 public boolean isPastePossible(final DataContext dataContext) {
101 return true;
104 public boolean isPasteEnabled(final DataContext dataContext) {
105 final Project project = DataKeys.PROJECT.getData(dataContext);
106 final IdeView ideView = DataKeys.IDE_VIEW.getData(dataContext);
107 if (project == null || ideView == null || ideView.getDirectories().length == 0) {
108 return false;
110 PsiJavaFile file = createJavaFileFromClipboardContent(project);
111 return file != null && file.getClasses().length == 1;
114 @Nullable
115 private static PsiJavaFile createJavaFileFromClipboardContent(final Project project) {
116 PsiJavaFile file = null;
117 Transferable content = CopyPasteManager.getInstance().getContents();
118 if (content != null) {
119 String text = null;
120 try {
121 text = (String)content.getTransferData(DataFlavor.stringFlavor);
123 catch (Exception e) {
124 // ignore;
126 if (text != null) {
127 file = (PsiJavaFile) PsiFileFactory.getInstance(project).createFileFromText("A.java", StdLanguages.JAVA, text);
130 return file;