intelliLang injector redesign p1
[fedora-idea.git] / plugins / IntelliLang / src / org / intellij / plugins / intelliLang / SettingsUI.java
blobfc747464b675a3d44b83d8ca9a8a0c1087d3d00a
1 /*
2 * Copyright 2006 Sascha Weinreuter
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.components.PersistentStateComponent;
22 import com.intellij.openapi.editor.Document;
23 import com.intellij.openapi.options.ConfigurationException;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.MasterDetailsComponent;
26 import com.intellij.psi.JavaPsiFacade;
27 import com.intellij.psi.PsiClass;
28 import com.intellij.psi.search.GlobalSearchScope;
29 import com.intellij.ui.ReferenceEditorWithBrowseButton;
30 import com.intellij.util.Function;
31 import org.intellij.plugins.intelliLang.inject.config.ui.ConfigurationPage;
32 import org.intellij.plugins.intelliLang.util.ShiftTabAction;
33 import org.intellij.plugins.intelliLang.util.PsiUtilEx;
34 import org.jetbrains.annotations.NotNull;
36 import javax.swing.*;
37 import java.awt.*;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.util.Arrays;
42 /**
43 * Provides user interface for editing configuration settings.
45 class SettingsUI implements PersistentStateComponent<MasterDetailsComponent.UIState> {
46 private final ConfigurationPage myConfigurationPage;
47 private final Configuration myConfiguration;
49 @SuppressWarnings({"UnusedDeclaration", "FieldCanBeLocal"})
50 private JPanel myRoot;
52 private JPanel myInjectionPanel;
53 private JRadioButton myNoInstrumentation;
54 private JRadioButton myAssertInstrumentation;
55 private JRadioButton myExceptionInstrumentation;
56 private JPanel myLanguageAnnotationPanel;
57 private JPanel myPatternAnnotationPanel;
58 private JPanel mySubstAnnotationPanel;
60 private ReferenceEditorWithBrowseButton myAnnotationField;
61 private ReferenceEditorWithBrowseButton myPatternField;
62 private ReferenceEditorWithBrowseButton mySubstField;
64 SettingsUI(@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);
97 myConfigurationPage = new ConfigurationPage(myConfiguration, project);
98 myInjectionPanel.add(Box.createVerticalStrut(5), BorderLayout.NORTH);
99 myInjectionPanel.add(myConfigurationPage.createComponent(), BorderLayout.CENTER);
103 * Adds textfield into placeholder panel and assigns a directly preceding label
105 private static void addField(JPanel panel, ReferenceEditorWithBrowseButton field) {
106 panel.add(field, BorderLayout.CENTER);
108 final Component[] components = panel.getParent().getComponents();
109 final int index = Arrays.asList(components).indexOf(panel);
110 if (index > 0) {
111 final Component component = components[index - 1];
112 if (component instanceof JLabel) {
113 ((JLabel)component).setLabelFor(field);
118 public JComponent createComponent() {
119 return myRoot;
122 @SuppressWarnings({"SimplifiableIfStatement"})
123 public boolean isModified() {
124 if (getInstrumentation() != myConfiguration.getInstrumentation()) {
125 return true;
127 if (!myAnnotationField.getText().equals(myConfiguration.getLanguageAnnotationClass())) {
128 return true;
130 if (!myPatternField.getText().equals(myConfiguration.getPatternAnnotationClass())) {
131 return true;
133 if (!mySubstField.getText().equals(myConfiguration.getSubstAnnotationClass())) {
134 return true;
136 return myConfigurationPage.isModified();
139 @NotNull
140 private Configuration.InstrumentationType getInstrumentation() {
141 if (myNoInstrumentation.isSelected()) return Configuration.InstrumentationType.NONE;
142 if (myAssertInstrumentation.isSelected()) return Configuration.InstrumentationType.ASSERT;
143 if (myExceptionInstrumentation.isSelected()) return Configuration.InstrumentationType.EXCEPTION;
145 assert false;
146 return null;
149 public void apply() throws ConfigurationException {
150 myConfiguration.setInstrumentationType(getInstrumentation());
151 myConfiguration.setLanguageAnnotation(myAnnotationField.getText());
152 myConfiguration.setPatternAnnotation(myPatternField.getText());
153 myConfiguration.setSubstAnnotation(mySubstField.getText());
155 myConfigurationPage.apply();
158 public void reset() {
159 myAnnotationField.setText(myConfiguration.getLanguageAnnotationClass());
160 myPatternField.setText(myConfiguration.getPatternAnnotationClass());
161 mySubstField.setText(myConfiguration.getSubstAnnotationClass());
163 myNoInstrumentation.setSelected(myConfiguration.getInstrumentation() == Configuration.InstrumentationType.NONE);
164 myAssertInstrumentation.setSelected(myConfiguration.getInstrumentation() == Configuration.InstrumentationType.ASSERT);
165 myExceptionInstrumentation.setSelected(myConfiguration.getInstrumentation() == Configuration.InstrumentationType.EXCEPTION);
167 myConfigurationPage.reset();
170 public void disposeUIResources() {
171 myConfigurationPage.disposeUIResources();
174 public MasterDetailsComponent.UIState getState() {
175 return myConfigurationPage.getState();
178 public void loadState(MasterDetailsComponent.UIState uiState) {
179 myConfigurationPage.loadState(uiState);
182 private static class BrowseClassListener implements ActionListener {
183 private final Project myProject;
184 private final ReferenceEditorWithBrowseButton myField;
186 public BrowseClassListener(Project project, ReferenceEditorWithBrowseButton annotationField) {
187 myProject = project;
188 myField = annotationField;
191 public void actionPerformed(ActionEvent e) {
192 final TreeClassChooserFactory factory = TreeClassChooserFactory.getInstance(myProject);
194 final GlobalSearchScope scope = GlobalSearchScope.allScope(myProject);
195 final PsiClass aClass = JavaPsiFacade.getInstance(myProject).findClass(myField.getText(), scope);
196 final TreeClassChooser chooser =
197 factory.createNoInnerClassesScopeChooser("Select Annotation Class", scope, new TreeClassChooser.ClassFilter() {
198 public boolean isAccepted(PsiClass aClass) {
199 return aClass.isAnnotationType();
201 }, aClass);
203 chooser.showDialog();
204 final PsiClass psiClass = chooser.getSelectedClass();
205 if (psiClass != null) {
206 myField.setText(psiClass.getQualifiedName());