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
.move
.moveInstanceMethod
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.openapi
.ui
.Messages
;
20 import com
.intellij
.psi
.PsiClass
;
21 import com
.intellij
.psi
.PsiMethod
;
22 import com
.intellij
.psi
.PsiSubstitutor
;
23 import com
.intellij
.psi
.PsiVariable
;
24 import com
.intellij
.psi
.search
.searches
.ClassInheritorsSearch
;
25 import com
.intellij
.psi
.util
.PsiFormatUtil
;
26 import com
.intellij
.refactoring
.RefactoringBundle
;
27 import com
.intellij
.refactoring
.ui
.RefactoringDialog
;
28 import com
.intellij
.refactoring
.ui
.VisibilityPanel
;
29 import com
.intellij
.ui
.ScrollPaneFactory
;
30 import com
.intellij
.usageView
.UsageViewUtil
;
33 import javax
.swing
.event
.ListSelectionEvent
;
34 import javax
.swing
.event
.ListSelectionListener
;
40 public abstract class MoveInstanceMethodDialogBase
extends RefactoringDialog
{
41 protected final PsiMethod myMethod
;
42 protected final PsiVariable
[] myVariables
;
44 public JComponent
getPreferredFocusedComponent() {
48 protected JList myList
;
49 protected VisibilityPanel myVisibilityPanel
;
50 protected final String myRefactoringName
;
52 public MoveInstanceMethodDialogBase(PsiMethod method
, PsiVariable
[] variables
, String refactoringName
) {
53 super(method
.getProject(), true);
55 myVariables
= variables
;
56 myRefactoringName
= refactoringName
;
57 setTitle(myRefactoringName
);
60 protected JPanel
createListAndVisibilityPanels() {
61 myList
= createTargetVariableChooser();
62 final JScrollPane scrollPane
= ScrollPaneFactory
.createScrollPane(myList
);
63 final JPanel hBox
= new JPanel(new GridBagLayout());
64 final GridBagConstraints gbConstraints
= new GridBagConstraints();
66 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
67 gbConstraints
.weightx
= 1;
68 gbConstraints
.weighty
= 0;
69 gbConstraints
.gridheight
= 1;
70 gbConstraints
.gridx
= 0;
71 gbConstraints
.gridy
= 0;
72 gbConstraints
.insets
= new Insets(0, 0, 0, 0);
73 hBox
.add(scrollPane
, gbConstraints
);
74 hBox
.add(Box
.createHorizontalStrut(4));
75 gbConstraints
.weightx
= 0;
76 gbConstraints
.fill
= GridBagConstraints
.VERTICAL
;
77 gbConstraints
.gridx
++;
78 myVisibilityPanel
= createVisibilityPanel();
79 hBox
.add (myVisibilityPanel
, gbConstraints
);
83 protected JList
createTargetVariableChooser() {
84 final JList list
= new JList(new MyListModel());
85 list
.setCellRenderer(new MyListCellRenderer());
86 list
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
87 list
.setSelectedIndex(0);
88 list
.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
89 public void valueChanged(ListSelectionEvent e
) {
90 getOKAction().setEnabled(!list
.getSelectionModel().isSelectionEmpty());
96 protected static VisibilityPanel
createVisibilityPanel() {
97 final VisibilityPanel visibilityPanel
= new VisibilityPanel(false, true);
98 visibilityPanel
.setVisibility(null);
99 return visibilityPanel
;
102 protected boolean verifyTargetClass (PsiClass targetClass
) {
103 if (targetClass
.isInterface()) {
104 final Project project
= getProject();
105 if (ClassInheritorsSearch
.search(targetClass
, targetClass
.getUseScope(), false).findFirst() == null) {
106 final String message
= RefactoringBundle
.message("0.is.an.interface.that.has.no.implementing.classes", UsageViewUtil
.getDescriptiveName(targetClass
));
108 Messages
.showErrorDialog(project
, message
, myRefactoringName
);
112 final String message
= RefactoringBundle
.message("0.is.an.interface.method.implementation.will.be.added.to.all.directly.implementing.classes",
113 UsageViewUtil
.getDescriptiveName(targetClass
));
115 final int result
= Messages
.showYesNoDialog(project
, message
, myRefactoringName
,
116 Messages
.getQuestionIcon());
117 if (result
!= 0) return false;
123 private class MyListModel
extends AbstractListModel
{
124 public int getSize() {
125 return myVariables
.length
;
128 public Object
getElementAt(int index
) {
129 return myVariables
[index
];
133 private static class MyListCellRenderer
extends DefaultListCellRenderer
{
134 public Component
getListCellRendererComponent(JList list
, Object value
, int index
, boolean isSelected
, boolean cellHasFocus
) {
135 super.getListCellRendererComponent(list
, value
, index
, isSelected
, cellHasFocus
);
136 final PsiVariable psiVariable
= (PsiVariable
)value
;
137 final String text
= PsiFormatUtil
.formatVariable(psiVariable
,
138 PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
,
139 PsiSubstitutor
.EMPTY
);
140 setIcon(psiVariable
.getIcon(0));