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
.extractInterface
;
18 import com
.intellij
.openapi
.project
.Project
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.refactoring
.HelpID
;
21 import com
.intellij
.refactoring
.JavaRefactoringSettings
;
22 import com
.intellij
.refactoring
.RefactoringBundle
;
23 import com
.intellij
.refactoring
.classMembers
.DelegatingMemberInfoModel
;
24 import com
.intellij
.refactoring
.classMembers
.MemberInfoBase
;
25 import com
.intellij
.refactoring
.extractSuperclass
.ExtractSuperBaseDialog
;
26 import com
.intellij
.refactoring
.extractSuperclass
.ExtractSuperBaseProcessor
;
27 import com
.intellij
.refactoring
.ui
.MemberSelectionPanel
;
28 import com
.intellij
.refactoring
.util
.DocCommentPolicy
;
29 import com
.intellij
.refactoring
.util
.classMembers
.MemberInfo
;
33 import java
.util
.List
;
35 class ExtractInterfaceDialog
extends ExtractSuperBaseDialog
{
36 private JLabel myInterfaceNameLabel
;
37 private JLabel myPackageLabel
;
39 public ExtractInterfaceDialog(Project project
, PsiClass sourceClass
) {
40 super(project
, sourceClass
, collectMembers(sourceClass
), ExtractInterfaceHandler
.REFACTORING_NAME
);
44 private static List
<MemberInfo
> collectMembers(PsiClass c
) {
45 return MemberInfo
.extractClassMembers(c
, new MemberInfoBase
.Filter
<PsiMember
>() {
46 public boolean includeMember(PsiMember element
) {
47 if (element
instanceof PsiMethod
) {
48 return element
.hasModifierProperty(PsiModifier
.PUBLIC
)
49 && !element
.hasModifierProperty(PsiModifier
.STATIC
);
51 else if (element
instanceof PsiField
) {
52 return element
.hasModifierProperty(PsiModifier
.FINAL
)
53 && element
.hasModifierProperty(PsiModifier
.STATIC
)
54 && element
.hasModifierProperty(PsiModifier
.PUBLIC
);
56 else if (element
instanceof PsiClass
) {
57 return ((PsiClass
)element
).isInterface() || element
.hasModifierProperty(PsiModifier
.STATIC
);
64 public MemberInfo
[] getSelectedMembers() {
65 int[] rows
= getCheckedRows();
66 MemberInfo
[] selectedMethods
= new MemberInfo
[rows
.length
];
67 for (int idx
= 0; idx
< rows
.length
; idx
++) {
68 selectedMethods
[idx
] = myMemberInfos
.get(rows
[idx
]);
70 return selectedMethods
;
73 private int[] getCheckedRows() {
75 for (MemberInfo info
: myMemberInfos
) {
76 if (info
.isChecked()) {
80 int[] rows
= new int[count
];
82 for (int idx
= 0; idx
< myMemberInfos
.size(); idx
++) {
83 if (myMemberInfos
.get(idx
).isChecked()) {
84 rows
[currentRow
++] = idx
;
90 protected JComponent
createNorthPanel() {
91 Box box
= Box
.createVerticalBox();
93 JPanel _panel
= new JPanel(new BorderLayout());
94 _panel
.add(new JLabel(RefactoringBundle
.message("extract.interface.from")), BorderLayout
.NORTH
);
95 _panel
.add(mySourceClassField
, BorderLayout
.CENTER
);
98 box
.add(Box
.createVerticalStrut(10));
100 box
.add(createActionComponent());
102 box
.add(Box
.createVerticalStrut(10));
104 myInterfaceNameLabel
= new JLabel();
105 myInterfaceNameLabel
.setText(RefactoringBundle
.message("interface.name.prompt"));
107 _panel
= new JPanel(new BorderLayout());
108 _panel
.add(myInterfaceNameLabel
, BorderLayout
.NORTH
);
109 _panel
.add(myExtractedSuperNameField
, BorderLayout
.CENTER
);
111 box
.add(Box
.createVerticalStrut(5));
113 _panel
= new JPanel(new BorderLayout());
114 myPackageLabel
= new JLabel();
115 myPackageLabel
.setText(RefactoringBundle
.message("package.for.new.interface"));
117 _panel
.add(myPackageLabel
, BorderLayout
.NORTH
);
118 _panel
.add(myPackageNameField
, BorderLayout
.CENTER
);
120 box
.add(Box
.createVerticalStrut(10));
122 JPanel panel
= new JPanel(new BorderLayout());
123 panel
.add(box
, BorderLayout
.CENTER
);
128 protected void updateDialogForExtractSubclass() {
129 super.updateDialogForExtractSubclass();
130 myInterfaceNameLabel
.setText(RefactoringBundle
.message("rename.implementation.class.to"));
134 protected void updateDialogForExtractSuperclass() {
135 super.updateDialogForExtractSuperclass();
136 myInterfaceNameLabel
.setText(RefactoringBundle
.message("interface.name.prompt"));
139 protected String
getClassNameLabelText() {
140 return RefactoringBundle
.message("superinterface.name");
143 protected JLabel
getClassNameLabel() {
144 return myInterfaceNameLabel
;
147 protected JLabel
getPackageNameLabel() {
148 return myPackageLabel
;
151 protected String
getEntityName() {
152 return RefactoringBundle
.message("extractSuperInterface.interface");
155 protected JComponent
createCenterPanel() {
156 JPanel panel
= new JPanel(new BorderLayout());
157 final MemberSelectionPanel memberSelectionPanel
= new MemberSelectionPanel(RefactoringBundle
.message("members.to.form.interface"),
158 myMemberInfos
, null);
159 memberSelectionPanel
.getTable().setMemberInfoModel(new DelegatingMemberInfoModel
<PsiMember
, MemberInfo
>(memberSelectionPanel
.getTable().getMemberInfoModel()) {
160 public Boolean
isFixedAbstract(MemberInfo member
) {
164 panel
.add(memberSelectionPanel
, BorderLayout
.CENTER
);
166 panel
.add(myJavaDocPanel
, BorderLayout
.EAST
);
172 protected String
getJavaDocPanelName() {
173 return RefactoringBundle
.message("extractSuperInterface.javadoc");
177 protected String
getExtractedSuperNameNotSpecifiedKey() {
178 return RefactoringBundle
.message("no.interface.name.specified");
182 protected int getJavaDocPolicySetting() {
183 return JavaRefactoringSettings
.getInstance().EXTRACT_INTERFACE_JAVADOC
;
187 protected void setJavaDocPolicySetting(int policy
) {
188 JavaRefactoringSettings
.getInstance().EXTRACT_INTERFACE_JAVADOC
= policy
;
192 protected ExtractSuperBaseProcessor
createProcessor() {
193 return new ExtractInterfaceProcessor(myProject
, false, getTargetDirectory(), getExtractedSuperName(),
194 mySourceClass
, getSelectedMembers(),
195 new DocCommentPolicy(getJavaDocPolicy()));
199 protected String
getHelpId() {
200 return HelpID
.EXTRACT_INTERFACE
;