update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / rename / DirectoryAsPackageRenameHandler.java
blob979b6c427d5e5e1e9c0550b746fc4e2e2d5cdef1
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.refactoring.rename;
18 import com.intellij.CommonBundle;
19 import com.intellij.openapi.actionSystem.DataContext;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.openapi.diagnostic.Logger;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.editor.ScrollType;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.psi.*;
28 import com.intellij.refactoring.RefactoringBundle;
29 import org.jetbrains.annotations.NotNull;
31 /**
32 * @author yole
34 public class DirectoryAsPackageRenameHandler implements RenameHandler {
35 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.rename.DirectoryAsPackageRenameHandler");
37 public boolean isAvailableOnDataContext(final DataContext dataContext) {
38 final PsiElement element = PsiElementRenameHandler.getElement(dataContext);
39 return element instanceof PsiDirectory;
42 public boolean isRenaming(final DataContext dataContext) {
43 return isAvailableOnDataContext(dataContext);
46 public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
47 PsiElement element = PsiElementRenameHandler.getElement(dataContext);
48 editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
49 final PsiElement nameSuggestionContext = file.findElementAt(editor.getCaretModel().getOffset());
50 doRename(element, project, nameSuggestionContext, editor);
53 public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
54 PsiElement element = elements.length == 1 ? elements[0] : null;
55 if (element == null) element = PsiElementRenameHandler.getElement(dataContext);
56 LOG.assertTrue(element != null);
57 Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
58 doRename(element, project, element, editor);
61 public static void doRename(PsiElement element, Project project, PsiElement nameSuggestionContext, Editor editor) {
62 PsiDirectory psiDirectory = (PsiDirectory)element;
63 PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
64 final String qualifiedName = aPackage != null ? aPackage.getQualifiedName() : "";
65 if (aPackage == null || qualifiedName.length() == 0/*default package*/ ||
66 !JavaPsiFacade.getInstance(project).getNameHelper().isIdentifier(psiDirectory.getName())) {
67 PsiElementRenameHandler.rename(element, project, nameSuggestionContext, editor);
69 else {
70 PsiDirectory[] directories = aPackage.getDirectories();
71 final VirtualFile[] virtualFiles = aPackage.occursInPackagePrefixes();
72 if (virtualFiles.length == 0 && directories.length == 1) {
73 PsiElementRenameHandler.rename(aPackage, project, nameSuggestionContext, editor);
75 else { // the directory corresponds to a package that has multiple associated directories
76 StringBuffer message = new StringBuffer();
77 RenameUtil.buildPackagePrefixChangedMessage(virtualFiles, message, qualifiedName);
78 buildMultipleDirectoriesInPackageMessage(message, aPackage, directories);
79 message.append(RefactoringBundle.message("directories.and.all.references.to.package.will.be.renamed", psiDirectory.getVirtualFile().getPresentableUrl()));
80 int ret =
81 Messages.showDialog(project, message.toString(), RefactoringBundle.message("warning.title"),
82 new String[]{
83 RefactoringBundle.message("rename.package.button.text"),
84 RefactoringBundle.message("rename.directory.button.text"),
85 CommonBundle.getCancelButtonText()}, 0, Messages.getWarningIcon());
86 if (ret == 0) {
87 PsiElementRenameHandler.rename(aPackage, project, nameSuggestionContext, editor);
88 } else if (ret == 1){
89 PsiElementRenameHandler.rename(psiDirectory, project, nameSuggestionContext, editor);
95 public static void buildMultipleDirectoriesInPackageMessage(StringBuffer message,
96 PsiPackage aPackage,
97 PsiDirectory[] directories) {
98 message.append(RefactoringBundle.message("multiple.directories.correspond.to.package"));
99 message.append(aPackage.getQualifiedName());
100 message.append(" :\n\n");
101 for (int i = 0; i < directories.length; i++) {
102 PsiDirectory directory = directories[i];
103 if (i > 0) {
104 message.append("\n");
106 message.append(directory.getVirtualFile().getPresentableUrl());