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
.introduceVariable
;
18 import com
.intellij
.codeInsight
.completion
.JavaCompletionUtil
;
19 import com
.intellij
.openapi
.help
.HelpManager
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.ui
.DialogWrapper
;
22 import com
.intellij
.psi
.JavaPsiFacade
;
23 import com
.intellij
.psi
.PsiExpression
;
24 import com
.intellij
.psi
.PsiType
;
25 import com
.intellij
.psi
.codeStyle
.CodeStyleSettingsManager
;
26 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
27 import com
.intellij
.psi
.codeStyle
.SuggestedNameInfo
;
28 import com
.intellij
.psi
.codeStyle
.VariableKind
;
29 import com
.intellij
.refactoring
.HelpID
;
30 import com
.intellij
.refactoring
.JavaRefactoringSettings
;
31 import com
.intellij
.refactoring
.RefactoringBundle
;
32 import com
.intellij
.refactoring
.ui
.*;
33 import com
.intellij
.ui
.NonFocusableCheckBox
;
34 import com
.intellij
.ui
.StateRestoringCheckBox
;
38 import java
.awt
.event
.ItemEvent
;
39 import java
.awt
.event
.ItemListener
;
41 class IntroduceVariableDialog
extends DialogWrapper
implements IntroduceVariableSettings
{
42 private final Project myProject
;
43 private final PsiExpression myExpression
;
44 private final int myOccurrencesCount
;
45 private final boolean myAnyLValueOccurences
;
46 private final boolean myDeclareFinalIfAll
;
47 private final TypeSelectorManager myTypeSelectorManager
;
48 private final IntroduceVariableHandler
.Validator myValidator
;
50 private NameSuggestionsField myNameField
;
51 private JCheckBox myCbReplaceAll
;
52 private StateRestoringCheckBox myCbReplaceWrite
= null;
53 private JCheckBox myCbFinal
;
54 private boolean myCbFinalState
;
55 private TypeSelector myTypeSelector
;
56 private NameSuggestionsManager myNameSuggestionsManager
;
57 private static final String REFACTORING_NAME
= RefactoringBundle
.message("introduce.variable.title");
58 private NameSuggestionsField
.DataChanged myNameChangedListener
;
59 private ItemListener myReplaceAllListener
;
60 private ItemListener myFinalListener
;
62 public IntroduceVariableDialog(Project project
,
63 PsiExpression expression
, int occurrencesCount
, boolean anyLValueOccurences
,
64 boolean declareFinalIfAll
, TypeSelectorManager typeSelectorManager
,
65 IntroduceVariableHandler
.Validator validator
) {
68 myExpression
= expression
;
69 myOccurrencesCount
= occurrencesCount
;
70 myAnyLValueOccurences
= anyLValueOccurences
;
71 myDeclareFinalIfAll
= declareFinalIfAll
;
72 myTypeSelectorManager
= typeSelectorManager
;
73 myValidator
= validator
;
75 setTitle(REFACTORING_NAME
);
79 protected void dispose() {
80 myNameField
.removeDataChangedListener(myNameChangedListener
);
81 if (myCbReplaceAll
!= null) {
82 myCbReplaceAll
.removeItemListener(myReplaceAllListener
);
84 myCbFinal
.removeItemListener(myFinalListener
);
88 protected Action
[] createActions() {
89 return new Action
[]{getOKAction(), getCancelAction(), getHelpAction()};
92 protected void init() {
97 public String
getEnteredName() {
98 return myNameField
.getEnteredName();
101 public boolean isReplaceAllOccurrences() {
102 if (myOccurrencesCount
<= 1) return false;
103 return myCbReplaceAll
.isSelected();
106 public boolean isDeclareFinal() {
107 if (myCbFinal
.isEnabled()) {
108 return myCbFinalState
;
114 public boolean isReplaceLValues() {
115 if (myOccurrencesCount
<= 1 || !myAnyLValueOccurences
|| myCbReplaceWrite
== null) {
119 return myCbReplaceWrite
.isSelected();
123 public PsiType
getSelectedType() {
124 return myTypeSelector
.getSelectedType();
127 protected JComponent
createNorthPanel() {
128 myNameField
= new NameSuggestionsField(myProject
);
129 myNameChangedListener
= new NameSuggestionsField
.DataChanged() {
130 public void dataChanged() {
134 myNameField
.addDataChangedListener(myNameChangedListener
);
136 JPanel panel
= new JPanel(new GridBagLayout());
137 GridBagConstraints gbConstraints
= new GridBagConstraints();
139 gbConstraints
.insets
= new Insets(4, 4, 4, 4);
140 gbConstraints
.anchor
= GridBagConstraints
.WEST
;
141 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
143 gbConstraints
.gridwidth
= 1;
144 gbConstraints
.weightx
= 0;
145 gbConstraints
.weighty
= 0;
146 gbConstraints
.gridx
= 0;
147 gbConstraints
.gridy
= 0;
148 JLabel type
= new JLabel(RefactoringBundle
.message("variable.of.type"));
149 panel
.add(type
, gbConstraints
);
151 gbConstraints
.gridx
++;
152 myTypeSelector
= myTypeSelectorManager
.getTypeSelector();
153 panel
.add(myTypeSelector
.getComponent(), gbConstraints
);
155 gbConstraints
.gridwidth
= 1;
156 gbConstraints
.weightx
= 0;
157 gbConstraints
.weighty
= 0;
158 gbConstraints
.gridx
= 0;
159 gbConstraints
.gridy
= 1;
160 JLabel namePrompt
= new JLabel(RefactoringBundle
.message("name.prompt"));
161 namePrompt
.setLabelFor(myNameField
.getComponent());
162 panel
.add(namePrompt
, gbConstraints
);
164 gbConstraints
.gridwidth
= 1;
165 gbConstraints
.weightx
= 1;
166 gbConstraints
.gridx
= 1;
167 gbConstraints
.gridy
= 1;
168 panel
.add(myNameField
.getComponent(), gbConstraints
);
170 myNameSuggestionsManager
= new NameSuggestionsManager(myTypeSelector
, myNameField
,
171 new NameSuggestionsGenerator() {
172 public SuggestedNameInfo
getSuggestedNameInfo(PsiType type
) {
173 final JavaCodeStyleManager codeStyleManager
= JavaCodeStyleManager
.getInstance(myProject
);
174 final SuggestedNameInfo nameInfo
= codeStyleManager
.suggestVariableName(VariableKind
.LOCAL_VARIABLE
, null, myExpression
, type
);
175 final String
[] strings
= JavaCompletionUtil
.completeVariableNameForRefactoring(codeStyleManager
, type
, VariableKind
.LOCAL_VARIABLE
, nameInfo
);
176 final SuggestedNameInfo
.Delegate delegate
= new SuggestedNameInfo
.Delegate(strings
, nameInfo
);
177 return codeStyleManager
.suggestUniqueVariableName(delegate
, myExpression
, true);
180 myNameSuggestionsManager
.setLabelsFor(type
, namePrompt
);
185 protected JComponent
createCenterPanel() {
186 JPanel panel
= new JPanel(new GridBagLayout());
187 GridBagConstraints gbConstraints
= new GridBagConstraints();
188 gbConstraints
.fill
= GridBagConstraints
.HORIZONTAL
;
189 gbConstraints
.weightx
= 1;
190 gbConstraints
.weighty
= 0;
191 gbConstraints
.gridwidth
= 1;
192 gbConstraints
.gridx
= 0;
193 gbConstraints
.gridy
= 0;
194 gbConstraints
.insets
= new Insets(0, 0, 0, 0);
196 if (myOccurrencesCount
> 1) {
197 myCbReplaceAll
= new NonFocusableCheckBox();
198 myCbReplaceAll
.setText(RefactoringBundle
.message("replace.all.occurences", myOccurrencesCount
));
200 panel
.add(myCbReplaceAll
, gbConstraints
);
201 myReplaceAllListener
= new ItemListener() {
202 public void itemStateChanged(ItemEvent e
) {
206 myCbReplaceAll
.addItemListener(myReplaceAllListener
);
208 if (myAnyLValueOccurences
) {
209 myCbReplaceWrite
= new StateRestoringCheckBox();
210 myCbReplaceWrite
.setText(RefactoringBundle
.message("replace.write.access.occurrences"));
211 gbConstraints
.insets
= new Insets(0, 8, 0, 0);
212 gbConstraints
.gridy
++;
213 panel
.add(myCbReplaceWrite
, gbConstraints
);
217 myCbFinal
= new NonFocusableCheckBox();
218 myCbFinal
.setText(RefactoringBundle
.message("declare.final"));
219 final Boolean createFinals
= JavaRefactoringSettings
.getInstance().INTRODUCE_LOCAL_CREATE_FINALS
;
220 myCbFinalState
= createFinals
== null ?
221 CodeStyleSettingsManager
.getSettings(myProject
).GENERATE_FINAL_LOCALS
:
222 createFinals
.booleanValue();
224 gbConstraints
.insets
= new Insets(0, 0, 0, 0);
225 gbConstraints
.gridy
++;
226 panel
.add(myCbFinal
, gbConstraints
);
227 myFinalListener
= new ItemListener() {
228 public void itemStateChanged(ItemEvent e
) {
229 if (myCbFinal
.isEnabled()) {
230 myCbFinalState
= myCbFinal
.isSelected();
234 myCbFinal
.addItemListener(myFinalListener
);
241 private void updateControls() {
242 if (myCbReplaceWrite
!= null) {
243 if (myCbReplaceAll
.isSelected()) {
244 myCbReplaceWrite
.makeSelectable();
246 myCbReplaceWrite
.makeUnselectable(true);
250 if (myCbReplaceAll
!= null) {
251 myTypeSelectorManager
.setAllOccurences(myCbReplaceAll
.isSelected());
253 myTypeSelectorManager
.setAllOccurences(false);
256 if (myDeclareFinalIfAll
&& myCbReplaceAll
!= null && myCbReplaceAll
.isSelected()) {
257 myCbFinal
.setEnabled(false);
258 myCbFinal
.setSelected(true);
260 myCbFinal
.setEnabled(true);
261 myCbFinal
.setSelected(myCbFinalState
);
265 protected void doOKAction() {
266 if (!myValidator
.isOK(this)) return;
267 myNameSuggestionsManager
.nameSelected();
268 myTypeSelectorManager
.typeSelected(getSelectedType());
269 if (myCbFinal
.isEnabled()) {
270 JavaRefactoringSettings
.getInstance().INTRODUCE_LOCAL_CREATE_FINALS
= myCbFinalState
;
275 private void updateOkStatus() {
276 String text
= getEnteredName();
277 setOKActionEnabled(JavaPsiFacade
.getInstance(myProject
).getNameHelper().isIdentifier(text
));
280 public JComponent
getPreferredFocusedComponent() {
281 return myNameField
.getFocusableComponent();
284 protected void doHelpAction() {
285 HelpManager
.getInstance().invokeHelp(HelpID
.INTRODUCE_VARIABLE
);