Fix compilation for case insensitive file systems
[fedora-idea.git] / platform / lang-impl / src / com / intellij / refactoring / extractMethod / AbstractExtractMethodDialog.java
blob41fbf33750fdf2117baa42a2b83836ec78e1d785
1 /*
2 * Copyright 2000-2010 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.extractMethod;
18 import com.intellij.codeInsight.codeFragment.CodeFragment;
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.openapi.ui.Messages;
23 import com.intellij.refactoring.RefactoringBundle;
24 import com.intellij.ui.DocumentAdapter;
25 import com.intellij.util.containers.HashMap;
27 import javax.swing.*;
28 import javax.swing.event.DocumentEvent;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
34 public class AbstractExtractMethodDialog extends DialogWrapper implements ExtractMethodSettings {
35 private JPanel myContentPane;
36 private AbstractParameterTablePanel myParametersPanel;
37 private JTextField myMethodNameTextField;
38 private JTextArea mySignaturePreviewTextArea;
39 private JTextArea myOutputVariablesTextArea;
40 private final String myDefaultName;
41 private final ExtractMethodValidator myValidator;
42 private final ExtractMethodDecorator myDecorator;
44 private AbstractVariableData[] myVariableData;
45 private Map<String, AbstractVariableData> myVariablesMap;
47 private final List<String> myArguments;
48 private final ArrayList<String> myOutputVariables;
50 public AbstractExtractMethodDialog(final Project project,
51 final String defaultName,
52 final CodeFragment fragment,
53 final ExtractMethodValidator validator,
54 final ExtractMethodDecorator decorator) {
55 super(project, true);
56 myDefaultName = defaultName;
57 CodeFragment fragment1 = fragment;
58 myValidator = validator;
59 myDecorator = decorator;
60 myArguments = new ArrayList<String>(fragment1.getInputVariables());
61 Collections.sort(myArguments);
62 myOutputVariables = new ArrayList<String>(fragment1.getOutputVariables());
63 Collections.sort(myOutputVariables);
64 setModal(true);
65 setTitle(RefactoringBundle.message("extract.method.title"));
66 init();
69 @Override
70 protected void init() {
71 super.init();
72 // Set default name and select it
73 myMethodNameTextField.setText(myDefaultName);
74 myMethodNameTextField.setSelectionStart(0);
75 myMethodNameTextField.setSelectionStart(myDefaultName.length());
76 myMethodNameTextField.getDocument().addDocumentListener(new DocumentAdapter() {
77 @Override
78 protected void textChanged(DocumentEvent e) {
79 updateOutputVariables();
80 updateSignature();
81 updateOkStatus();
83 });
86 myVariableData = createVariableData(myArguments);
87 myVariablesMap = createVariableMap(myVariableData);
88 myParametersPanel.setVariableData(myVariableData);
89 myParametersPanel.init();
91 updateOutputVariables();
92 updateSignature();
93 updateOkStatus();
96 public JComponent getPreferredFocusedComponent() {
97 return myMethodNameTextField;
100 public static AbstractVariableData[] createVariableData(final List<String> args) {
101 final AbstractVariableData[] datas = new AbstractVariableData[args.size()];
102 for (int i = 0; i < args.size(); i++) {
103 final AbstractVariableData data = new AbstractVariableData();
104 final String name = args.get(i);
105 data.originalName = name;
106 data.name = name;
107 data.passAsParameter = true;
108 datas[i] = data;
110 return datas;
113 public static Map<String, AbstractVariableData> createVariableMap(final AbstractVariableData[] data) {
114 final HashMap<String, AbstractVariableData> map = new HashMap<String, AbstractVariableData>();
115 for (AbstractVariableData variableData : data) {
116 map.put(variableData.getOriginalName(), variableData);
118 return map;
121 @Override
122 protected Action[] createActions() {
123 return new Action[]{getOKAction(), getCancelAction(), getHelpAction()};
126 @Override
127 protected void doOKAction() {
128 final String error = myValidator.check(getMethodName());
129 if (error!=null){
130 Messages.showInfoMessage(error, RefactoringBundle.message("error.title"));
131 return;
133 super.doOKAction();
136 @Override
137 protected void doHelpAction() {
138 HelpManager.getInstance().invokeHelp("refactoring.extractMethod");
141 protected JComponent createCenterPanel() {
142 return myContentPane;
145 private void createUIComponents() {
146 myParametersPanel = new AbstractParameterTablePanel(myValidator){
147 protected void doCancelAction() {
148 AbstractExtractMethodDialog.this.doCancelAction();
151 protected void doEnterAction() {
152 doOKAction();
155 protected void updateSignature() {
156 updateOutputVariables();
157 AbstractExtractMethodDialog.this.updateSignature();
162 private void updateOutputVariables() {
163 final StringBuilder builder = new StringBuilder();
164 boolean first = true;
165 for (String variable : myOutputVariables) {
166 if (myVariablesMap!=null){
167 final AbstractVariableData data = myVariablesMap.get(variable);
168 final String outputName = data != null ? data.getName() : variable;
169 if (first){
170 first = false;
171 } else {
172 builder.append(", ");
174 builder.append(outputName);
177 myOutputVariablesTextArea.setText(builder.length() > 0 ? builder.toString() : "None");
180 private void updateSignature() {
181 mySignaturePreviewTextArea.setText(myDecorator.createMethodPreview(getMethodName(), myVariableData));
184 private void updateOkStatus() {
185 setOKActionEnabled(myValidator.isValidName(getMethodName()));
188 public String getMethodName() {
189 return myMethodNameTextField.getText().trim();
192 public AbstractVariableData[] getVariableData() {
193 return myVariableData;