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.
18 * created at Nov 12, 2001
21 package com
.intellij
.refactoring
.move
.moveInner
;
23 import com
.intellij
.ide
.util
.PackageChooserDialog
;
24 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.psi
.*;
27 import com
.intellij
.refactoring
.RefactoringBundle
;
28 import com
.intellij
.refactoring
.move
.MoveCallback
;
29 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
30 import org
.jetbrains
.annotations
.Nullable
;
32 public class MoveInnerImpl
{
33 private static final Logger LOG
= Logger
.getInstance("#com.intellij.refactoring.move.moveInner.MoveInnerImpl");
35 public static final String REFACTORING_NAME
= RefactoringBundle
.message("move.inner.to.upper.level.title");
37 public static void doMove(final Project project
, PsiElement
[] elements
, final MoveCallback moveCallback
) {
38 if (elements
.length
!= 1) return;
39 final PsiClass aClass
= (PsiClass
) elements
[0];
40 boolean condition
= aClass
.getContainingClass() != null;
41 LOG
.assertTrue(condition
);
43 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, aClass
)) return;
44 final PsiElement targetContainer
= getTargetContainer(aClass
, true);
45 if (targetContainer
== null) return;
47 final MoveInnerDialog dialog
= new MoveInnerDialog(
50 new MoveInnerProcessor(project
, moveCallback
),
57 * must be called in atomic action
60 public static PsiElement
getTargetContainer(PsiClass innerClass
, final boolean chooseIfNotUnderSource
) {
61 final PsiClass outerClass
= innerClass
.getContainingClass();
62 assert outerClass
!= null; // Only inner classes allowed.
64 PsiElement outerClassParent
= outerClass
.getParent();
65 while (outerClassParent
!= null) {
66 if (outerClassParent
instanceof PsiClass
&& !(outerClassParent
instanceof PsiAnonymousClass
)) {
67 return outerClassParent
;
69 else if (outerClassParent
instanceof PsiFile
) {
70 final PsiDirectory directory
= innerClass
.getContainingFile().getContainingDirectory();
71 final PsiPackage aPackage
= JavaDirectoryService
.getInstance().getPackage(directory
);
72 if (aPackage
== null) {
73 if (chooseIfNotUnderSource
) {
74 PackageChooserDialog chooser
= new PackageChooserDialog("Select Target Package", innerClass
.getProject());
76 if (!chooser
.isOK()) return null;
77 final PsiPackage chosenPackage
= chooser
.getSelectedPackage();
78 if (chosenPackage
== null) return null;
79 return chosenPackage
.getDirectories()[0];
86 outerClassParent
= outerClassParent
.getParent();
89 LOG
.assertTrue(false);