update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / intention / impl / CreateFieldFromParameterDialog.java
blobb5fa301890ca42697f45ff45c2cd3adf98c8ef39
1 /*
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;
30 import javax.swing.*;
31 import javax.swing.event.DocumentEvent;
32 import java.awt.*;
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) {
47 super(project, true);
48 myProject = project;
49 myNames = names;
50 myType = type;
51 myTargetClass = targetClass;
52 myFieldMayBeFinal = fieldMayBeFinal;
54 setTitle(CodeInsightBundle.message("dialog.create.field.from.parameter.title"));
56 init();
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(
68 getContentPane(),
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());
72 if (result == 0) {
73 close(OK_EXIT_CODE);
75 else {
76 return;
81 close(OK_EXIT_CODE);
84 protected void init() {
85 super.init();
86 updateOkStatus();
89 public String getEnteredName() {
90 if (myNameField instanceof JComboBox) {
91 JComboBox combobox = (JComboBox) myNameField;
92 return (String) combobox.getEditor().getItem();
94 else {
95 return ((JTextField) myNameField).getText();
99 public boolean isDeclareFinal() {
100 if (myCbFinal.isEnabled()) {
101 return myCbFinal.isSelected();
104 return false;
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);
121 else {
122 doCancelAction();
126 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
127 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
130 combobox.addItemListener(
131 new ItemListener() {
132 public void itemStateChanged(ItemEvent e) {
133 updateOkStatus();
137 combobox.getEditor().getEditorComponent().addKeyListener(
138 new KeyAdapter() {
139 public void keyPressed(KeyEvent e) {
140 updateOkStatus();
143 public void keyReleased(KeyEvent e) {
144 updateOkStatus();
147 public void keyTyped(KeyEvent e) {
148 updateOkStatus();
153 else {
154 JTextField field = new JTextField() {
155 public Dimension getPreferredSize() {
156 Dimension size = super.getPreferredSize();
157 return new Dimension(200, size.height);
160 myNameField = field;
161 field.setText(myNames[0]);
163 field.getDocument().addDocumentListener(new DocumentAdapter() {
164 protected void textChanged(DocumentEvent e) {
165 updateOkStatus();
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);
200 return panel;
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));
217 } else {
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()) {
232 return panel;
235 private void requestFocusInNameWindow() {
236 if (myNameField instanceof JTextField) {
237 myNameField.requestFocusInWindow();
239 else {
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() {
250 return myNameField;