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
;
28 import javax
.swing
.event
.DocumentEvent
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Collections
;
31 import java
.util
.List
;
34 public class ExtractMethodDialog
extends DialogWrapper
implements ExtractMethodSettings
{
35 private JPanel myContentPane
;
36 private ParameterTablePanel 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 VariableData
[] myVariableData
;
45 private Map
<String
, VariableData
> myVariablesMap
;
47 private final List
<String
> myArguments
;
48 private final ArrayList
<String
> myOutputVariables
;
50 public ExtractMethodDialog(final Project project
,
51 final String defaultName
,
52 final CodeFragment fragment
,
53 final ExtractMethodValidator validator
,
54 final ExtractMethodDecorator decorator
) {
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
);
65 setTitle(RefactoringBundle
.message("extract.method.title"));
70 protected void 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() {
78 protected void textChanged(DocumentEvent e
) {
79 updateOutputVariables();
86 myVariableData
= createVariableData(myArguments
);
87 myVariablesMap
= createVariableMap(myVariableData
);
88 myParametersPanel
.setVariableData(myVariableData
);
89 myParametersPanel
.init();
91 updateOutputVariables();
96 public JComponent
getPreferredFocusedComponent() {
97 return myMethodNameTextField
;
100 public static VariableData
[] createVariableData(final List
<String
> args
) {
101 final VariableData
[] datas
= new VariableData
[args
.size()];
102 for (int i
= 0; i
< args
.size(); i
++) {
103 final VariableData data
= new VariableData();
104 final String name
= args
.get(i
);
105 data
.originalName
= name
;
107 data
.passAsParameter
= true;
113 public static Map
<String
, VariableData
> createVariableMap(final VariableData
[] data
) {
114 final HashMap
<String
, VariableData
> map
= new HashMap
<String
, VariableData
>();
115 for (VariableData variableData
: data
) {
116 map
.put(variableData
.getOriginalName(), variableData
);
122 protected Action
[] createActions() {
123 return new Action
[]{getOKAction(), getCancelAction(), getHelpAction()};
127 protected void doOKAction() {
128 final String error
= myValidator
.check(getMethodName());
130 Messages
.showInfoMessage(error
, RefactoringBundle
.message("error.title"));
137 protected void doHelpAction() {
138 HelpManager
.getInstance().invokeHelp("refactoring.extractMethod");
141 protected JComponent
createCenterPanel() {
142 return myContentPane
;
145 private void createUIComponents() {
146 myParametersPanel
= new ParameterTablePanel(myValidator
){
147 protected void doCancelAction() {
148 ExtractMethodDialog
.this.doCancelAction();
151 protected void doEnterAction() {
155 protected void updateSignature() {
156 updateOutputVariables();
157 ExtractMethodDialog
.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 VariableData data
= myVariablesMap
.get(variable
);
168 final String outputName
= data
!= null ? data
.getName() : variable
;
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 VariableData
[] getVariableData() {
193 return myVariableData
;