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
.codeInsight
.intention
.impl
;
18 import com
.intellij
.codeInsight
.CodeInsightBundle
;
19 import com
.intellij
.ide
.util
.PropertiesComponent
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.ui
.ComboBox
;
22 import com
.intellij
.openapi
.ui
.DialogWrapper
;
23 import com
.intellij
.openapi
.ui
.Messages
;
24 import com
.intellij
.psi
.JavaPsiFacade
;
25 import com
.intellij
.psi
.PsiClass
;
26 import com
.intellij
.psi
.PsiField
;
27 import com
.intellij
.ui
.DocumentAdapter
;
28 import org
.jetbrains
.annotations
.NonNls
;
31 import javax
.swing
.event
.DocumentEvent
;
33 import java
.awt
.event
.*;
35 class CreateFieldFromParameterDialog
extends DialogWrapper
{
36 private final Project myProject
;
37 private final String
[] myNames
;
38 private final String myType
;
39 private final PsiClass myTargetClass
;
40 private final boolean myFieldMayBeFinal
;
42 private JComponent myNameField
;
43 private JCheckBox myCbFinal
;
44 private static final @NonNls String PROPERTY_NAME
= "CREATE_FIELD_FROM_PARAMETER_DECLARE_FINAL";
46 public CreateFieldFromParameterDialog(Project project
, String
[] names
, String type
, PsiClass targetClass
, final boolean fieldMayBeFinal
) {
51 myTargetClass
= targetClass
;
52 myFieldMayBeFinal
= fieldMayBeFinal
;
54 setTitle(CodeInsightBundle
.message("dialog.create.field.from.parameter.title"));
59 protected void doOKAction() {
60 if (myCbFinal
.isEnabled()) {
61 PropertiesComponent
.getInstance().setValue(PROPERTY_NAME
, ""+myCbFinal
.isSelected());
64 final PsiField
[] fields
= myTargetClass
.getFields();
65 for (PsiField field
: fields
) {
66 if (field
.getName().equals(getEnteredName())) {
67 int result
= Messages
.showOkCancelDialog(
69 CodeInsightBundle
.message("dialog.create.field.from.parameter.already.exists.text", getEnteredName()),
70 CodeInsightBundle
.message("dialog.create.field.from.parameter.already.exists.title"),
71 Messages
.getQuestionIcon());
84 protected void init() {
89 public String
getEnteredName() {
90 if (myNameField
instanceof JComboBox
) {
91 JComboBox combobox
= (JComboBox
) myNameField
;
92 return (String
) combobox
.getEditor().getItem();
95 return ((JTextField
) myNameField
).getText();
99 public boolean isDeclareFinal() {
100 if (myCbFinal
.isEnabled()) {
101 return myCbFinal
.isSelected();
107 protected JComponent
createNorthPanel() {
108 if (myNames
.length
> 1) {
109 final ComboBox combobox
= new ComboBox(myNames
, 200);
110 myNameField
= combobox
;
111 combobox
.setEditable(true);
112 combobox
.setSelectedIndex(0);
113 combobox
.setMaximumRowCount(8);
115 combobox
.registerKeyboardAction(
116 new ActionListener() {
117 public void actionPerformed(ActionEvent e
) {
118 if (combobox
.isPopupVisible()) {
119 combobox
.setPopupVisible(false);
126 KeyStroke
.getKeyStroke(KeyEvent
.VK_ESCAPE
, 0),
127 JComponent
.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
130 combobox
.addItemListener(
132 public void itemStateChanged(ItemEvent e
) {
137 combobox
.getEditor().getEditorComponent().addKeyListener(
139 public void keyPressed(KeyEvent e
) {
143 public void keyReleased(KeyEvent e
) {
147 public void keyTyped(KeyEvent e
) {
154 JTextField field
= new JTextField() {
155 public Dimension
getPreferredSize() {
156 Dimension size
= super.getPreferredSize();
157 return new Dimension(200, size
.height
);
161 field
.setText(myNames
[0]);
163 field
.getDocument().addDocumentListener(new DocumentAdapter() {
164 protected void textChanged(DocumentEvent e
) {
170 JPanel panel
= new JPanel();
171 panel
.setLayout(new GridBagLayout());
172 GridBagConstraints gbConstraints
= new GridBagConstraints();
174 gbConstraints
.insets
= new Insets(4, 4, 4, 4);
175 gbConstraints
.anchor
= GridBagConstraints
.EAST
;
176 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
178 gbConstraints
.gridwidth
= 2;
179 gbConstraints
.weightx
= 1;
180 gbConstraints
.weighty
= 1;
181 gbConstraints
.gridx
= 0;
182 gbConstraints
.gridy
= 0;
183 JLabel type
= new JLabel(CodeInsightBundle
.message("dialog.create.field.from.parameter.field.type.label", myType
));
184 panel
.add(type
, gbConstraints
);
186 gbConstraints
.gridwidth
= 1;
187 gbConstraints
.weightx
= 0;
188 gbConstraints
.weighty
= 1;
189 gbConstraints
.gridx
= 0;
190 gbConstraints
.gridy
= 1;
191 JLabel namePrompt
= new JLabel(CodeInsightBundle
.message("dialog.create.field.from.parameter.field.name.label"));
192 panel
.add(namePrompt
, gbConstraints
);
194 gbConstraints
.gridwidth
= 1;
195 gbConstraints
.weightx
= 1;
196 gbConstraints
.gridx
= 1;
197 gbConstraints
.gridy
= 1;
198 panel
.add(myNameField
, gbConstraints
);
203 protected JComponent
createCenterPanel() {
204 JPanel panel
= new JPanel(new GridBagLayout());
205 GridBagConstraints gbConstraints
= new GridBagConstraints();
206 gbConstraints
.fill
= GridBagConstraints
.HORIZONTAL
;
207 gbConstraints
.weightx
= 1;
208 gbConstraints
.weighty
= 0;
209 gbConstraints
.gridwidth
= 1;
210 gbConstraints
.gridx
= 0;
211 gbConstraints
.gridy
= 0;
212 gbConstraints
.insets
= new Insets(0, 0, 0, 0);
214 myCbFinal
= new JCheckBox(CodeInsightBundle
.message("dialog.create.field.from.parameter.declare.final.checkbox"));
215 if (myFieldMayBeFinal
) {
216 myCbFinal
.setSelected(PropertiesComponent
.getInstance().isTrueValue(PROPERTY_NAME
));
218 myCbFinal
.setSelected(false);
219 myCbFinal
.setEnabled(false);
222 gbConstraints
.gridy
++;
223 panel
.add(myCbFinal
, gbConstraints
);
224 myCbFinal
.addActionListener(new ActionListener() {
225 public void actionPerformed(ActionEvent e
) {
226 requestFocusInNameWindow();
227 if (myCbFinal
.isEnabled()) {
235 private void requestFocusInNameWindow() {
236 if (myNameField
instanceof JTextField
) {
237 myNameField
.requestFocusInWindow();
240 ((JComboBox
) myNameField
).getEditor().getEditorComponent().requestFocusInWindow();
244 private void updateOkStatus() {
245 String text
= getEnteredName();
246 setOKActionEnabled(JavaPsiFacade
.getInstance(myProject
).getNameHelper().isIdentifier(text
));
249 public JComponent
getPreferredFocusedComponent() {