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
;
23 import java
.awt
.event
.ActionEvent
;
24 import java
.awt
.event
.KeyEvent
;
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
) {
40 init(project
, groups
, preselectedConfigurable
!= null ? preselectedConfigurable
: findLastSavedConfigurable(groups
, project
));
43 public OptionsEditorDialog(Project project
, ConfigurableGroup
[] groups
, @NotNull String preselectedConfigurableDisplayName
) {
45 init(project
, groups
, getPreselectedByDisplayName(groups
, preselectedConfigurableDisplayName
, project
));
48 private void init(final Project project
, final ConfigurableGroup
[] groups
, final Configurable preselected
) {
51 myPreselected
= preselected
;
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() {
71 public ActionCallback
onModifiedAdded(final Configurable configurable
) {
73 return new ActionCallback
.Done();
77 public ActionCallback
onModifiedRemoved(final Configurable configurable
) {
79 return new ActionCallback
.Done();
83 public ActionCallback
onErrorsChanged() {
85 return new ActionCallback
.Done();
88 Disposer
.register(myDisposable
, myEditor
);
92 public boolean updateStatus() {
93 myApplyAction
.setEnabled(myEditor
.canApply());
95 final Map
<Configurable
,ConfigurationException
> errors
= myEditor
.getContext().getErrors();
96 if (errors
.size() == 0) {
99 setErrorText("Changes were not applied because of an error");
102 return errors
.size() == 0;
106 protected String
getDimensionServiceKey() {
107 return DIMENSION_KEY
;
111 protected void doOKAction() {
112 myEditor
.flushModifications();
114 if (myEditor
.canApply()) {
116 if (!updateStatus()) return;
119 saveCurrentConfigurable();
121 ApplicationManager
.getApplication().saveAll();
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());
136 props
.setValue(LAST_SELECTED_CONFIGURABLE
, current
.getClass().getName());
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
;
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
;
170 public void doCancelAction(final AWTEvent source
) {
171 if (source
instanceof KeyEvent
|| source
instanceof ActionEvent
) {
172 if (myEditor
.getContext().isHoldingFilter()) {
173 myEditor
.clearFilter();
178 super.doCancelAction(source
);
182 public void doCancelAction() {
183 saveCurrentConfigurable();
184 super.doCancelAction();
188 protected Action
[] createActions() {
189 myApplyAction
= new ApplyAction();
190 return new Action
[] {getOKAction(), getCancelAction(), myApplyAction
, getHelpAction()};
194 protected void doHelpAction() {
195 final String topic
= myEditor
.getHelpTopic();
197 HelpManager
.getInstance().invokeHelp(topic
);
202 public JComponent
getPreferredFocusedComponent() {
203 return myEditor
.getPreferredFocusedComponent();
206 public Object
getData(@NonNls String dataId
) {
207 if (dataId
.equals(OptionsEditor
.KEY
.getName())) {
213 private class ApplyAction
extends AbstractAction
{
214 public ApplyAction() {
215 super(CommonBundle
.getApplyButtonText());
219 public void actionPerformed(final ActionEvent e
) {