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
.codeInspection
;
18 import com
.intellij
.CommonBundle
;
19 import com
.intellij
.codeInsight
.CodeInsightUtilBase
;
20 import com
.intellij
.codeInsight
.daemon
.QuickFixBundle
;
21 import com
.intellij
.ide
.util
.PackageUtil
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.ui
.Messages
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.refactoring
.PackageWrapper
;
27 import com
.intellij
.refactoring
.move
.moveClassesOrPackages
.MoveClassesOrPackagesProcessor
;
28 import com
.intellij
.refactoring
.move
.moveClassesOrPackages
.SingleSourceRootMoveDestination
;
29 import com
.intellij
.refactoring
.util
.RefactoringMessageUtil
;
30 import com
.intellij
.util
.IncorrectOperationException
;
31 import org
.jetbrains
.annotations
.NotNull
;
33 public class MoveToPackageFix
implements LocalQuickFix
{
34 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInspection.MoveToPackageFix");
35 private final PsiFile myFile
;
36 private final PsiPackage myTargetPackage
;
38 public MoveToPackageFix(PsiFile file
, PsiPackage targetPackage
) {
40 myTargetPackage
= targetPackage
;
44 public String
getName() {
45 return QuickFixBundle
.message("move.class.to.package.text", myTargetPackage
.getQualifiedName());
49 public String
getFamilyName() {
50 return QuickFixBundle
.message("move.class.to.package.family");
53 public boolean isAvailable() {
56 && myFile
.getManager().isInProject(myFile
)
57 && myFile
instanceof PsiJavaFile
58 && ((PsiJavaFile
) myFile
).getClasses().length
!= 0
59 && myTargetPackage
!= null
60 && myTargetPackage
.isValid()
64 public void applyFix(@NotNull final Project project
, @NotNull final ProblemDescriptor descriptor
) {
65 if (!CodeInsightUtilBase
.prepareFileForWrite(myFile
)) return;
68 String packageName
= myTargetPackage
.getQualifiedName();
69 PsiDirectory directory
= PackageUtil
.findOrCreateDirectoryForPackage(project
, packageName
, null, true);
71 if (directory
== null) {
74 String error
= RefactoringMessageUtil
.checkCanCreateFile(directory
, myFile
.getName());
76 Messages
.showMessageDialog(project
, error
, CommonBundle
.getErrorTitle(), Messages
.getErrorIcon());
79 new MoveClassesOrPackagesProcessor(
81 new PsiElement
[]{((PsiJavaFile
) myFile
).getClasses()[0]},
82 new SingleSourceRootMoveDestination(PackageWrapper
.create(JavaDirectoryService
.getInstance().getPackage(directory
)), directory
), false,
86 catch (IncorrectOperationException e
) {
91 public boolean startInWriteAction() {