temporary workaround for [IDEADEV-39362] enabled before RubyMine EAP
[fedora-idea.git] / platform-impl / src / com / intellij / openapi / options / newEditor / OptionsEditorDialog.java
blobb6995b96deedaab4266613202c687801851b6ac7
1 package com.intellij.openapi.options.newEditor;
3 import com.intellij.CommonBundle;
4 import com.intellij.ide.ui.search.SearchUtil;
5 import com.intellij.ide.util.PropertiesComponent;
6 import com.intellij.openapi.actionSystem.DataProvider;
7 import com.intellij.openapi.help.HelpManager;
8 import com.intellij.openapi.options.Configurable;
9 import com.intellij.openapi.options.ConfigurableGroup;
10 import com.intellij.openapi.options.ConfigurationException;
11 import com.intellij.openapi.options.SearchableConfigurable;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.ui.DialogWrapper;
14 import com.intellij.openapi.util.ActionCallback;
15 import com.intellij.openapi.util.Disposer;
16 import com.intellij.openapi.application.ApplicationManager;
17 import org.jetbrains.annotations.NonNls;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.KeyEvent;
25 import java.util.Map;
27 public class OptionsEditorDialog extends DialogWrapper implements DataProvider{
29 private Project myProject;
30 private ConfigurableGroup[] myGroups;
31 private Configurable myPreselected;
32 private OptionsEditor myEditor;
34 private ApplyAction myApplyAction;
35 public static final String DIMENSION_KEY = "OptionsEditor";
36 @NonNls static final String LAST_SELECTED_CONFIGURABLE = "options.lastSelected";
38 public OptionsEditorDialog(Project project, ConfigurableGroup[] groups, Configurable preselectedConfigurable) {
39 super(project, true);
40 init(project, groups, preselectedConfigurable != null ? preselectedConfigurable : findLastSavedConfigurable(groups, project));
43 public OptionsEditorDialog(Project project, ConfigurableGroup[] groups, @NotNull String preselectedConfigurableDisplayName) {
44 super(project, true);
45 init(project, groups, getPreselectedByDisplayName(groups, preselectedConfigurableDisplayName, project));
48 private void init(final Project project, final ConfigurableGroup[] groups, final Configurable preselected) {
49 myProject = project;
50 myGroups = groups;
51 myPreselected = preselected;
53 setTitle("Settings");
55 init();
58 private static Configurable getPreselectedByDisplayName(final ConfigurableGroup[] groups, final String preselectedConfigurableDisplayName,
59 final Project project) {
60 Configurable result = findPreselectedByDisplyName(preselectedConfigurableDisplayName, groups);
62 return result == null ? findLastSavedConfigurable(groups, project) : result;
65 protected JComponent createCenterPanel() {
66 myEditor = new OptionsEditor(myProject, myGroups, myPreselected);
67 myEditor.setMinimumSize(new Dimension(500,400));
68 myEditor.setPreferredSize(new Dimension(1000,700));
69 myEditor.getContext().addColleague(new OptionsEditorColleague.Adapter() {
70 @Override
71 public ActionCallback onModifiedAdded(final Configurable configurable) {
72 updateStatus();
73 return new ActionCallback.Done();
76 @Override
77 public ActionCallback onModifiedRemoved(final Configurable configurable) {
78 updateStatus();
79 return new ActionCallback.Done();
82 @Override
83 public ActionCallback onErrorsChanged() {
84 updateStatus();
85 return new ActionCallback.Done();
87 });
88 Disposer.register(myDisposable, myEditor);
89 return myEditor;
92 public boolean updateStatus() {
93 myApplyAction.setEnabled(myEditor.canApply());
95 final Map<Configurable,ConfigurationException> errors = myEditor.getContext().getErrors();
96 if (errors.size() == 0) {
97 setErrorText(null);
98 } else {
99 setErrorText("Changes were not applied because of an error");
102 return errors.size() == 0;
105 @Override
106 protected String getDimensionServiceKey() {
107 return DIMENSION_KEY;
110 @Override
111 protected void doOKAction() {
112 myEditor.flushModifications();
114 if (myEditor.canApply()) {
115 myEditor.apply();
116 if (!updateStatus()) return;
119 saveCurrentConfigurable();
121 ApplicationManager.getApplication().saveAll();
123 super.doOKAction();
127 private void saveCurrentConfigurable() {
128 final Configurable current = myEditor.getContext().getCurrentConfigurable();
129 if (current == null) return;
131 final PropertiesComponent props = PropertiesComponent.getInstance(myProject);
133 if (current instanceof SearchableConfigurable) {
134 props.setValue(LAST_SELECTED_CONFIGURABLE, ((SearchableConfigurable)current).getId());
135 } else {
136 props.setValue(LAST_SELECTED_CONFIGURABLE, current.getClass().getName());
140 @Nullable
141 private static Configurable findLastSavedConfigurable(ConfigurableGroup[] groups, final Project project) {
142 final String id = PropertiesComponent.getInstance(project).getValue(LAST_SELECTED_CONFIGURABLE);
143 if (id == null) return null;
145 final java.util.List<Configurable> all = SearchUtil.expand(groups);
146 for (Configurable each : all) {
147 if (each instanceof SearchableConfigurable) {
148 if (id.equals(((SearchableConfigurable)each).getId())) return each;
150 if (id.equals(each.getClass().getName())) return each;
153 return null;
156 @Nullable
157 private static Configurable findPreselectedByDisplyName(final String preselectedConfigurableDisplayName,ConfigurableGroup[] groups) {
158 final java.util.List<Configurable> all = SearchUtil.expand(groups);
159 for (Configurable each : all) {
160 if (preselectedConfigurableDisplayName.equals(each.getDisplayName())) return each;
163 return null;
169 @Override
170 public void doCancelAction(final AWTEvent source) {
171 if (source instanceof KeyEvent || source instanceof ActionEvent) {
172 if (myEditor.getContext().isHoldingFilter()) {
173 myEditor.clearFilter();
174 return;
178 super.doCancelAction(source);
181 @Override
182 public void doCancelAction() {
183 saveCurrentConfigurable();
184 super.doCancelAction();
187 @Override
188 protected Action[] createActions() {
189 myApplyAction = new ApplyAction();
190 return new Action[] {getOKAction(), getCancelAction(), myApplyAction, getHelpAction()};
193 @Override
194 protected void doHelpAction() {
195 final String topic = myEditor.getHelpTopic();
196 if (topic != null) {
197 HelpManager.getInstance().invokeHelp(topic);
201 @Override
202 public JComponent getPreferredFocusedComponent() {
203 return myEditor.getPreferredFocusedComponent();
206 public Object getData(@NonNls String dataId) {
207 if (dataId.equals(OptionsEditor.KEY.getName())) {
208 return myEditor;
210 return null;
213 private class ApplyAction extends AbstractAction {
214 public ApplyAction() {
215 super(CommonBundle.getApplyButtonText());
216 setEnabled(false);
219 public void actionPerformed(final ActionEvent e) {
220 myEditor.apply();