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
.uiDesigner
;
18 import com
.intellij
.codeInsight
.CodeInsightUtil
;
19 import com
.intellij
.openapi
.application
.ApplicationManager
;
20 import com
.intellij
.openapi
.command
.CommandProcessor
;
21 import com
.intellij
.openapi
.diagnostic
.Logger
;
22 import com
.intellij
.openapi
.options
.SearchableConfigurable
;
23 import com
.intellij
.openapi
.progress
.ProgressManager
;
24 import com
.intellij
.openapi
.progress
.util
.DispatchThreadProgressWindow
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.util
.Comparing
;
27 import com
.intellij
.openapi
.util
.IconLoader
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import com
.intellij
.psi
.*;
30 import com
.intellij
.psi
.search
.GlobalSearchScope
;
31 import com
.intellij
.psi
.search
.PsiShortNamesCache
;
32 import com
.intellij
.ui
.ColoredListCellRenderer
;
33 import com
.intellij
.ui
.SimpleTextAttributes
;
34 import com
.intellij
.uiDesigner
.compiler
.AsmCodeGenerator
;
35 import com
.intellij
.uiDesigner
.make
.FormSourceCodeGenerator
;
36 import com
.intellij
.uiDesigner
.radComponents
.LayoutManagerRegistry
;
37 import com
.intellij
.util
.IncorrectOperationException
;
38 import org
.jetbrains
.annotations
.NotNull
;
39 import org
.jetbrains
.annotations
.Nullable
;
44 * @author Anton Katilin
45 * @author Vladimir Kondratyev
47 public final class GuiDesignerConfigurable
implements SearchableConfigurable
{
48 private static final Logger LOG
= Logger
.getInstance("#com.intellij.uiDesigner.GuiDesignerConfigurable");
49 private final Project myProject
;
50 private MyGeneralUI myGeneralUI
;
53 * Invoked by reflection
55 public GuiDesignerConfigurable(final Project project
) {
59 public String
getDisplayName() {
60 return UIDesignerBundle
.message("title.gui.designer");
63 public Icon
getIcon() {
64 return IconLoader
.getIcon("/general/uiDesigner.png");
68 public String
getHelpTopic() {
69 return "project.propGUI";
72 public JComponent
createComponent() {
73 if (myGeneralUI
== null) {
74 myGeneralUI
= new MyGeneralUI();
77 return myGeneralUI
.myPanel
;
80 public boolean isModified() {
81 final GuiDesignerConfiguration configuration
= GuiDesignerConfiguration
.getInstance(myProject
);
83 if (myGeneralUI
.myChkCopyFormsRuntime
.isSelected() != configuration
.COPY_FORMS_RUNTIME_TO_OUTPUT
) {
87 if (!Comparing
.equal(configuration
.DEFAULT_LAYOUT_MANAGER
, myGeneralUI
.myLayoutManagerCombo
.getSelectedItem())) {
91 if (!Comparing
.equal(configuration
.DEFAULT_FIELD_ACCESSIBILITY
, myGeneralUI
.myDefaultFieldAccessibilityCombo
.getSelectedItem())) {
95 if (configuration
.INSTRUMENT_CLASSES
!= myGeneralUI
.myRbInstrumentClasses
.isSelected()) {
102 public void apply() {
103 final GuiDesignerConfiguration configuration
= GuiDesignerConfiguration
.getInstance(myProject
);
104 configuration
.COPY_FORMS_RUNTIME_TO_OUTPUT
= myGeneralUI
.myChkCopyFormsRuntime
.isSelected();
105 configuration
.DEFAULT_LAYOUT_MANAGER
= (String
)myGeneralUI
.myLayoutManagerCombo
.getSelectedItem();
106 configuration
.INSTRUMENT_CLASSES
= myGeneralUI
.myRbInstrumentClasses
.isSelected();
107 configuration
.DEFAULT_FIELD_ACCESSIBILITY
= (String
)myGeneralUI
.myDefaultFieldAccessibilityCombo
.getSelectedItem();
109 if (configuration
.INSTRUMENT_CLASSES
&& !myProject
.isDefault()) {
110 final DispatchThreadProgressWindow progressWindow
= new DispatchThreadProgressWindow(false, myProject
);
111 progressWindow
.setRunnable(new MyApplyRunnable(progressWindow
));
112 progressWindow
.setTitle(UIDesignerBundle
.message("title.converting.project"));
113 progressWindow
.start();
117 public void reset() {
118 final GuiDesignerConfiguration configuration
= GuiDesignerConfiguration
.getInstance(myProject
);
121 if (configuration
.INSTRUMENT_CLASSES
) {
122 myGeneralUI
.myRbInstrumentClasses
.setSelected(true);
125 myGeneralUI
.myRbInstrumentSources
.setSelected(true);
127 myGeneralUI
.myChkCopyFormsRuntime
.setSelected(configuration
.COPY_FORMS_RUNTIME_TO_OUTPUT
);
129 myGeneralUI
.myLayoutManagerCombo
.setModel(new DefaultComboBoxModel(LayoutManagerRegistry
.getNonDeprecatedLayoutManagerNames()));
130 myGeneralUI
.myLayoutManagerCombo
.setRenderer(new ColoredListCellRenderer() {
131 protected void customizeCellRenderer(JList list
, Object value
, int index
, boolean selected
, boolean hasFocus
) {
132 append(LayoutManagerRegistry
.getLayoutManagerDisplayName((String
) value
), SimpleTextAttributes
.REGULAR_ATTRIBUTES
);
135 myGeneralUI
.myLayoutManagerCombo
.setSelectedItem(configuration
.DEFAULT_LAYOUT_MANAGER
);
136 myGeneralUI
.myDefaultFieldAccessibilityCombo
.setSelectedItem(configuration
.DEFAULT_FIELD_ACCESSIBILITY
);
139 public void disposeUIResources() {
141 } /*UI for "General" tab*/
143 private static final class MyGeneralUI
{
144 public JPanel myPanel
;
145 public JRadioButton myRbInstrumentClasses
;
146 public JRadioButton myRbInstrumentSources
;
147 public JCheckBox myChkCopyFormsRuntime
;
148 private JComboBox myLayoutManagerCombo
;
149 private JComboBox myDefaultFieldAccessibilityCombo
;
152 private final class MyApplyRunnable
implements Runnable
{
153 private final DispatchThreadProgressWindow myProgressWindow
;
155 public MyApplyRunnable(final DispatchThreadProgressWindow progressWindow
) {
156 myProgressWindow
= progressWindow
;
160 * Removes all generated sources
162 private void vanishGeneratedSources() {
163 final PsiShortNamesCache cache
= JavaPsiFacade
.getInstance(myProject
).getShortNamesCache();
164 final PsiMethod
[] methods
= cache
.getMethodsByName(AsmCodeGenerator
.SETUP_METHOD_NAME
, GlobalSearchScope
.projectScope(myProject
));
166 CodeInsightUtil
.preparePsiElementsForWrite(methods
);
168 for (int i
= 0; i
< methods
.length
; i
++) {
169 final PsiMethod method
= methods
[i
];
170 final PsiClass aClass
= method
.getContainingClass();
171 if (aClass
!= null) {
173 final PsiFile psiFile
= aClass
.getContainingFile();
174 LOG
.assertTrue(psiFile
!= null);
175 final VirtualFile vFile
= psiFile
.getVirtualFile();
176 LOG
.assertTrue(vFile
!= null);
177 myProgressWindow
.setText(UIDesignerBundle
.message("progress.converting", vFile
.getPresentableUrl()));
178 myProgressWindow
.setFraction(((double)i
) / ((double)methods
.length
));
179 if (vFile
.isWritable()) {
180 FormSourceCodeGenerator
.cleanup(aClass
);
183 catch (IncorrectOperationException e
) {
191 * Launches vanish/generate sources processes
193 private void applyImpl() {
194 CommandProcessor
.getInstance().executeCommand(myProject
, new Runnable() {
196 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
198 PsiDocumentManager
.getInstance(myProject
).commitAllDocuments();
199 vanishGeneratedSources();
207 ProgressManager
.getInstance().runProcess(new Runnable() {
211 }, myProgressWindow
);
215 public String
getId() {
216 return getHelpTopic();
220 public Runnable
enableSearch(String option
) {