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.
17 package org
.intellij
.plugins
.intelliLang
;
19 import com
.intellij
.ide
.util
.TreeClassChooser
;
20 import com
.intellij
.ide
.util
.TreeClassChooserFactory
;
21 import com
.intellij
.openapi
.editor
.Document
;
22 import com
.intellij
.openapi
.options
.Configurable
;
23 import com
.intellij
.openapi
.options
.ConfigurationException
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.JavaPsiFacade
;
26 import com
.intellij
.psi
.PsiClass
;
27 import com
.intellij
.psi
.search
.GlobalSearchScope
;
28 import com
.intellij
.ui
.ReferenceEditorWithBrowseButton
;
29 import com
.intellij
.util
.Function
;
30 import org
.intellij
.plugins
.intelliLang
.util
.PsiUtilEx
;
31 import org
.intellij
.plugins
.intelliLang
.util
.ShiftTabAction
;
32 import org
.jetbrains
.annotations
.Nls
;
33 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.awt
.event
.ActionEvent
;
38 import java
.awt
.event
.ActionListener
;
39 import java
.util
.Arrays
;
42 * @author Gregory.Shrago
44 public class AdvancedSettingsUI
implements Configurable
{
45 private final Configuration myConfiguration
;
47 @SuppressWarnings({"UnusedDeclaration", "FieldCanBeLocal"})
48 private JPanel myRoot
;
50 private JRadioButton myNoInstrumentation
;
51 private JRadioButton myAssertInstrumentation
;
52 private JRadioButton myExceptionInstrumentation
;
53 private JPanel myLanguageAnnotationPanel
;
54 private JPanel myPatternAnnotationPanel
;
55 private JPanel mySubstAnnotationPanel
;
56 private JCheckBox myAnalyzeReferencesCheckBox
;
57 private JCheckBox myUseDataflowAnalysisIfCheckBox
;
58 private JCheckBox myIncludeUncomputableOperandsAsCheckBox
;
60 private final ReferenceEditorWithBrowseButton myAnnotationField
;
61 private final ReferenceEditorWithBrowseButton myPatternField
;
62 private final ReferenceEditorWithBrowseButton mySubstField
;
64 public AdvancedSettingsUI(@NotNull final Project project
, Configuration configuration
) {
65 myConfiguration
= configuration
;
67 myAnnotationField
= new ReferenceEditorWithBrowseButton(null, project
, new Function
<String
, Document
>() {
68 public Document
fun(String s
) {
69 return PsiUtilEx
.createDocument(s
, project
);
71 }, myConfiguration
.getLanguageAnnotationClass());
72 myAnnotationField
.addActionListener(new BrowseClassListener(project
, myAnnotationField
));
73 myAnnotationField
.setEnabled(!project
.isDefault());
74 ShiftTabAction
.attachTo(myAnnotationField
.getEditorTextField());
75 addField(myLanguageAnnotationPanel
, myAnnotationField
);
77 myPatternField
= new ReferenceEditorWithBrowseButton(null, project
, new Function
<String
, Document
>() {
78 public Document
fun(String s
) {
79 return PsiUtilEx
.createDocument(s
, project
);
81 }, myConfiguration
.getPatternAnnotationClass());
82 myPatternField
.addActionListener(new BrowseClassListener(project
, myPatternField
));
83 myPatternField
.setEnabled(!project
.isDefault());
84 ShiftTabAction
.attachTo(myPatternField
.getEditorTextField());
85 addField(myPatternAnnotationPanel
, myPatternField
);
87 mySubstField
= new ReferenceEditorWithBrowseButton(null, project
, new Function
<String
, Document
>() {
88 public Document
fun(String s
) {
89 return PsiUtilEx
.createDocument(s
, project
);
91 }, myConfiguration
.getPatternAnnotationClass());
92 mySubstField
.addActionListener(new BrowseClassListener(project
, mySubstField
));
93 mySubstField
.setEnabled(!project
.isDefault());
94 ShiftTabAction
.attachTo(mySubstField
.getEditorTextField());
95 addField(mySubstAnnotationPanel
, mySubstField
);
99 * Adds textfield into placeholder panel and assigns a directly preceding label
101 private static void addField(JPanel panel
, ReferenceEditorWithBrowseButton field
) {
102 panel
.add(field
, BorderLayout
.CENTER
);
104 final Component
[] components
= panel
.getParent().getComponents();
105 final int index
= Arrays
.asList(components
).indexOf(panel
);
107 final Component component
= components
[index
- 1];
108 if (component
instanceof JLabel
) {
109 ((JLabel
)component
).setLabelFor(field
);
114 public JComponent
createComponent() {
118 @SuppressWarnings({"SimplifiableIfStatement"})
119 public boolean isModified() {
120 if (getInstrumentation() != myConfiguration
.getInstrumentation()) {
123 if (!myAnnotationField
.getText().equals(myConfiguration
.getLanguageAnnotationClass())) {
126 if (!myPatternField
.getText().equals(myConfiguration
.getPatternAnnotationClass())) {
129 if (!mySubstField
.getText().equals(myConfiguration
.getSubstAnnotationClass())) {
132 if (myConfiguration
.isResolveReferences() != myAnalyzeReferencesCheckBox
.isSelected()) {
135 if (myConfiguration
.isUseDfaIfAvailable() != myUseDataflowAnalysisIfCheckBox
.isSelected()) {
138 if (myConfiguration
.isIncludeUncomputablesAsLiterals() != myIncludeUncomputableOperandsAsCheckBox
.isSelected()) {
145 private Configuration
.InstrumentationType
getInstrumentation() {
146 if (myNoInstrumentation
.isSelected()) return Configuration
.InstrumentationType
.NONE
;
147 if (myAssertInstrumentation
.isSelected()) return Configuration
.InstrumentationType
.ASSERT
;
148 if (myExceptionInstrumentation
.isSelected()) return Configuration
.InstrumentationType
.EXCEPTION
;
154 public void apply() throws ConfigurationException
{
155 myConfiguration
.setInstrumentationType(getInstrumentation());
156 myConfiguration
.setLanguageAnnotation(myAnnotationField
.getText());
157 myConfiguration
.setPatternAnnotation(myPatternField
.getText());
158 myConfiguration
.setSubstAnnotation(mySubstField
.getText());
160 myConfiguration
.setResolveReferences(myAnalyzeReferencesCheckBox
.isSelected());
161 myConfiguration
.setUseDfaIfAvailable(myUseDataflowAnalysisIfCheckBox
.isSelected());
162 myConfiguration
.setIncludeUncomputablesAsLiterals(myIncludeUncomputableOperandsAsCheckBox
.isSelected());
165 public void reset() {
166 myAnnotationField
.setText(myConfiguration
.getLanguageAnnotationClass());
167 myPatternField
.setText(myConfiguration
.getPatternAnnotationClass());
168 mySubstField
.setText(myConfiguration
.getSubstAnnotationClass());
170 myNoInstrumentation
.setSelected(myConfiguration
.getInstrumentation() == Configuration
.InstrumentationType
.NONE
);
171 myAssertInstrumentation
.setSelected(myConfiguration
.getInstrumentation() == Configuration
.InstrumentationType
.ASSERT
);
172 myExceptionInstrumentation
.setSelected(myConfiguration
.getInstrumentation() == Configuration
.InstrumentationType
.EXCEPTION
);
174 myAnalyzeReferencesCheckBox
.setSelected(myConfiguration
.isResolveReferences());
175 myUseDataflowAnalysisIfCheckBox
.setSelected(myConfiguration
.isUseDfaIfAvailable());
176 myIncludeUncomputableOperandsAsCheckBox
.setSelected(myConfiguration
.isIncludeUncomputablesAsLiterals());
179 public void disposeUIResources() {
183 public String
getDisplayName() {
187 public Icon
getIcon() {
191 public String
getHelpTopic() {
192 return "reference.settings.injection.advanced";
195 private static class BrowseClassListener
implements ActionListener
{
196 private final Project myProject
;
197 private final ReferenceEditorWithBrowseButton myField
;
199 private BrowseClassListener(Project project
, ReferenceEditorWithBrowseButton annotationField
) {
201 myField
= annotationField
;
204 public void actionPerformed(ActionEvent e
) {
205 final TreeClassChooserFactory factory
= TreeClassChooserFactory
.getInstance(myProject
);
207 final GlobalSearchScope scope
= GlobalSearchScope
.allScope(myProject
);
208 final PsiClass aClass
= JavaPsiFacade
.getInstance(myProject
).findClass(myField
.getText(), scope
);
209 final TreeClassChooser chooser
=
210 factory
.createNoInnerClassesScopeChooser("Select Annotation Class", scope
, new TreeClassChooser
.ClassFilter() {
211 public boolean isAccepted(PsiClass aClass
) {
212 return aClass
.isAnnotationType();
216 chooser
.showDialog();
217 final PsiClass psiClass
= chooser
.getSelectedClass();
218 if (psiClass
!= null) {
219 myField
.setText(psiClass
.getQualifiedName());