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
.typeCook
;
18 import com
.intellij
.openapi
.help
.HelpManager
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.psi
.*;
22 import com
.intellij
.refactoring
.HelpID
;
23 import com
.intellij
.refactoring
.JavaRefactoringSettings
;
24 import com
.intellij
.refactoring
.RefactoringBundle
;
25 import com
.intellij
.refactoring
.ui
.RefactoringDialog
;
26 import com
.intellij
.ui
.IdeBorderFactory
;
27 import com
.intellij
.usageView
.UsageViewUtil
;
33 * Created by IntelliJ IDEA.
37 * To change this template use Options | File Templates.
39 public class TypeCookDialog
extends RefactoringDialog
{
40 public static final String REFACTORING_NAME
= RefactoringBundle
.message("generify.title");
42 private final PsiElement
[] myElements
;
43 private final JLabel myClassNameLabel
= new JLabel();
44 private final JCheckBox myCbDropCasts
= new JCheckBox();
45 private final JCheckBox myCbPreserveRawArrays
= new JCheckBox();
46 private final JCheckBox myCbLeaveObjectParameterizedTypesRaw
= new JCheckBox();
47 private final JCheckBox myCbExhaustive
= new JCheckBox();
48 private final JCheckBox myCbCookObjects
= new JCheckBox();
49 private final JCheckBox myCbCookToWildcards
= new JCheckBox();
51 @SuppressWarnings({"HardCodedStringLiteral"})
52 public TypeCookDialog(Project project
, PsiElement
[] elements
) {
55 setTitle(REFACTORING_NAME
);
59 StringBuffer name
= new StringBuffer("<html>");
61 myElements
= elements
;
62 for (int i
= 0; i
< elements
.length
; i
++) {
63 PsiElement element
= elements
[i
];
64 name
.append(StringUtil
.capitalize(UsageViewUtil
.getType(element
)));
66 name
.append(UsageViewUtil
.getDescriptiveName(element
));
67 if (i
< elements
.length
- 1) {
72 name
.append("</html>");
74 myClassNameLabel
.setText(name
.toString());
77 protected JComponent
createCenterPanel() {
81 protected void doHelpAction() {
82 HelpManager
.getInstance().invokeHelp(HelpID
.TYPE_COOK
);
85 protected JComponent
createNorthPanel() {
86 JPanel optionsPanel
= new JPanel(new GridBagLayout());
87 GridBagConstraints gbConstraints
= new GridBagConstraints();
89 optionsPanel
.setBorder(IdeBorderFactory
.createBorder());
91 if (myCbDropCasts
.isEnabled()) {
92 myCbDropCasts
.setSelected(JavaRefactoringSettings
.getInstance().TYPE_COOK_DROP_CASTS
);
95 if (myCbPreserveRawArrays
.isEnabled()) {
96 myCbPreserveRawArrays
.setSelected(JavaRefactoringSettings
.getInstance().TYPE_COOK_PRESERVE_RAW_ARRAYS
);
99 if (myCbLeaveObjectParameterizedTypesRaw
.isEnabled()) {
100 myCbLeaveObjectParameterizedTypesRaw
.setSelected(
101 JavaRefactoringSettings
.getInstance().TYPE_COOK_LEAVE_OBJECT_PARAMETERIZED_TYPES_RAW
);
104 if (myCbExhaustive
.isEnabled()) {
105 myCbExhaustive
.setSelected(
106 JavaRefactoringSettings
.getInstance().TYPE_COOK_EXHAUSTIVE
);
109 if (myCbCookObjects
.isEnabled()) {
110 myCbCookObjects
.setSelected(
111 JavaRefactoringSettings
.getInstance().TYPE_COOK_COOK_OBJECTS
);
114 if (myCbCookToWildcards
.isEnabled()) {
115 myCbCookToWildcards
.setSelected(
116 JavaRefactoringSettings
.getInstance().TYPE_COOK_PRODUCE_WILDCARDS
);
119 myCbDropCasts
.setText(RefactoringBundle
.message("type.cook.drop.obsolete.casts"));
120 myCbPreserveRawArrays
.setText(RefactoringBundle
.message("type.cook.preserve.raw.arrays"));
121 myCbLeaveObjectParameterizedTypesRaw
.setText(RefactoringBundle
.message("type.cook.leave.object.parameterized.types.raw"));
122 myCbExhaustive
.setText(RefactoringBundle
.message("type.cook.perform.exhaustive.search"));
123 myCbCookObjects
.setText(RefactoringBundle
.message("type.cook.generify.objects"));
124 myCbCookToWildcards
.setText(RefactoringBundle
.message("type.cook.produce.wildcard.types"));
126 gbConstraints
.insets
= new Insets(4, 8, 4, 8);
128 gbConstraints
.weighty
= 1;
129 gbConstraints
.weightx
= 1;
130 gbConstraints
.gridwidth
= GridBagConstraints
.REMAINDER
;
131 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
132 optionsPanel
.add(myClassNameLabel
, gbConstraints
);
134 gbConstraints
.gridx
= 0;
135 gbConstraints
.weightx
= 1;
136 gbConstraints
.gridwidth
= 1;
137 gbConstraints
.gridy
= 1;
138 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
139 optionsPanel
.add(myCbDropCasts
, gbConstraints
);
141 gbConstraints
.gridx
= 1;
142 gbConstraints
.weightx
= 1;
143 gbConstraints
.gridwidth
= GridBagConstraints
.REMAINDER
;
144 gbConstraints
.fill
= GridBagConstraints
.BOTH
;
145 optionsPanel
.add(myCbPreserveRawArrays
, gbConstraints
);
147 gbConstraints
.gridx
= 0;
148 gbConstraints
.gridwidth
= 2;
149 gbConstraints
.gridy
= 2;
150 optionsPanel
.add(myCbLeaveObjectParameterizedTypesRaw
, gbConstraints
);
152 gbConstraints
.gridy
++;
153 optionsPanel
.add(myCbExhaustive
, gbConstraints
);
155 gbConstraints
.gridy
++;
156 optionsPanel
.add(myCbCookObjects
, gbConstraints
);
158 gbConstraints
.gridy
++;
159 optionsPanel
.add(myCbCookToWildcards
, gbConstraints
);
164 protected void doAction() {
165 JavaRefactoringSettings settings
= JavaRefactoringSettings
.getInstance();
166 settings
.TYPE_COOK_DROP_CASTS
= myCbDropCasts
.isSelected();
167 settings
.TYPE_COOK_PRESERVE_RAW_ARRAYS
= myCbPreserveRawArrays
.isSelected();
168 settings
.TYPE_COOK_LEAVE_OBJECT_PARAMETERIZED_TYPES_RAW
= myCbLeaveObjectParameterizedTypesRaw
.isSelected();
169 settings
.TYPE_COOK_EXHAUSTIVE
= myCbExhaustive
.isSelected();
170 settings
.TYPE_COOK_COOK_OBJECTS
= myCbCookObjects
.isSelected();
171 settings
.TYPE_COOK_PRODUCE_WILDCARDS
= myCbCookToWildcards
.isSelected();
173 invokeRefactoring(new TypeCookProcessor(getProject(), myElements
, getSettings()));
176 public Settings
getSettings() {
177 final boolean dropCasts
= myCbDropCasts
.isSelected();
178 final boolean preserveRawArrays
= true; //myCbPreserveRawArrays.isSelected();
179 final boolean leaveObjectParameterizedTypesRaw
= myCbLeaveObjectParameterizedTypesRaw
.isSelected();
180 final boolean exhaustive
= myCbExhaustive
.isSelected();
181 final boolean cookObjects
= myCbCookObjects
.isSelected();
182 final boolean cookToWildcards
= myCbCookToWildcards
.isSelected();
184 return new Settings() {
185 public boolean dropObsoleteCasts() {
189 public boolean preserveRawArrays() {
190 return preserveRawArrays
;
193 public boolean leaveObjectParameterizedTypesRaw() {
194 return leaveObjectParameterizedTypesRaw
;
197 public boolean exhaustive() {
201 public boolean cookObjects() {
205 public boolean cookToWildcards() {
206 return cookToWildcards
;