summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/options/newEditor/IdeSettingsDialog.java
blob: c38bf6ff20331c229c83f3b3e746fa2d4c23c18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.options.newEditor;

import com.intellij.CommonBundle;
import com.intellij.ide.ui.search.SearchUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.help.HelpManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurableGroup;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.Gray;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.JBColor;
import com.intellij.ui.border.CustomLineBorder;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author Konstantin Bulenkov
 */
public class IdeSettingsDialog extends DialogWrapper implements DataProvider {
  private Project myProject;
  private ConfigurableGroup[] myGroups;
  private Configurable myPreselected;
  private OptionsEditor myEditor;

  private ApplyAction myApplyAction;
  public static final String DIMENSION_KEY = "OptionsEditor";
  @NonNls static final String LAST_SELECTED_CONFIGURABLE = "options.lastSelected";

  /**
   * This constructor should be eliminated after the new modality approach
   * will have been checked. See a {@code Registry} key ide.perProjectModality
   *
   * @deprecated
   */
  public IdeSettingsDialog(Project project, ConfigurableGroup[] groups,
                           @Nullable Configurable preselectedConfigurable, boolean applicationModalIfPossible) {
    super(project, true, applicationModalIfPossible);
    init(project, groups, preselectedConfigurable != null ? preselectedConfigurable : findLastSavedConfigurable(groups, project));
  }

  /**
   * This constructor should be eliminated after the new modality approach
   * will have been checked. See a {@code Registry} key ide.perProjectModality
   *
   * @deprecated
   */
  public IdeSettingsDialog(Project project, ConfigurableGroup[] groups,
                           @NotNull String preselectedConfigurableDisplayName, boolean applicationModalIfPossible) {
    super(project, true, applicationModalIfPossible);
    init(project, groups, getPreselectedByDisplayName(groups, preselectedConfigurableDisplayName, project));
  }

  public IdeSettingsDialog(Project project, ConfigurableGroup[] groups, @Nullable Configurable preselectedConfigurable) {
    super(project, true);
    init(project, groups, preselectedConfigurable != null ? preselectedConfigurable : findLastSavedConfigurable(groups, project));
  }

  public IdeSettingsDialog(Project project, ConfigurableGroup[] groups, @NotNull String preselectedConfigurableDisplayName) {
    super(project, true);
    init(project, groups, getPreselectedByDisplayName(groups, preselectedConfigurableDisplayName, project));
  }

  @Nullable
  @Override
  protected Border createContentPaneBorder() {
    return IdeBorderFactory.createEmptyBorder(0);
  }

  private void init(final Project project, final ConfigurableGroup[] groups, @Nullable final Configurable preselected) {
    myProject = project;
    myGroups = groups;
    myPreselected = preselected;

    setTitle(CommonBundle.settingsTitle());

    init();
  }

  @Nullable
  private static Configurable getPreselectedByDisplayName(final ConfigurableGroup[] groups, final String preselectedConfigurableDisplayName,
                                                          final Project project) {
    Configurable result = findPreselectedByDisplayName(preselectedConfigurableDisplayName, groups);

    return result == null ? findLastSavedConfigurable(groups, project) : result;
  }

  @Override
  public boolean isTypeAheadEnabled() {
    return true;
  }

  @Nullable
  @Override
  protected JComponent createSouthPanel() {
    final JComponent panel = super.createSouthPanel();
    CustomLineBorder line = new CustomLineBorder(new JBColor(Gray._153.withAlpha(128), Gray._100.withAlpha(128)), 1, 0, 0, 0);
    panel.setBorder(new CompoundBorder(line, new EmptyBorder(8, 12, 8, 12)));
    return panel;
  }

  protected JComponent createCenterPanel() {
    myEditor = new OptionsEditor(myProject, myGroups, myPreselected);
    myEditor.getContext().addColleague(new OptionsEditorColleague.Adapter() {
      @Override
      public ActionCallback onModifiedAdded(final Configurable configurable) {
        updateStatus();
        return new ActionCallback.Done();
      }

      @Override
      public ActionCallback onModifiedRemoved(final Configurable configurable) {
        updateStatus();
        return new ActionCallback.Done();
      }

      @Override
      public ActionCallback onErrorsChanged() {
        updateStatus();
        return new ActionCallback.Done();
      }
    });
    Disposer.register(myDisposable, myEditor);
    return myEditor;
  }

  public boolean updateStatus() {
    myApplyAction.setEnabled(myEditor.canApply());

    final Map<Configurable, ConfigurationException> errors = myEditor.getContext().getErrors();
    if (errors.size() == 0) {
      setErrorText(null);
    }
    else {
      String text = "Changes were not applied because of an error";

      final String errorMessage = getErrorMessage(errors);
      if (errorMessage != null) {
        text += "<br>" + errorMessage;
      }

      setErrorText(text);
    }

    return errors.size() == 0;
  }

  @Nullable
  private static String getErrorMessage(final Map<Configurable, ConfigurationException> errors) {
    final Collection<ConfigurationException> values = errors.values();
    final ConfigurationException[] exceptions = values.toArray(new ConfigurationException[values.size()]);
    if (exceptions.length > 0) {
      return exceptions[0].getMessage();
    }
    return null;
  }

  @Override
  protected String getDimensionServiceKey() {
    return DIMENSION_KEY;
  }

  @Override
  protected void doOKAction() {
    myEditor.flushModifications();

    if (myEditor.canApply()) {
      myEditor.apply();
      if (!updateStatus()) return;
    }

    saveCurrentConfigurable();

    ApplicationManager.getApplication().saveAll();

    super.doOKAction();
  }


  private void saveCurrentConfigurable() {
    final Configurable current = myEditor.getContext().getCurrentConfigurable();
    if (current == null) return;

    final PropertiesComponent props = PropertiesComponent.getInstance(myProject);

    if (current instanceof SearchableConfigurable) {
      props.setValue(LAST_SELECTED_CONFIGURABLE, ((SearchableConfigurable)current).getId());
    }
    else {
      props.setValue(LAST_SELECTED_CONFIGURABLE, current.getClass().getName());
    }
  }

  @Nullable
  private static Configurable findLastSavedConfigurable(ConfigurableGroup[] groups, final Project project) {
    final String id = PropertiesComponent.getInstance(project).getValue(LAST_SELECTED_CONFIGURABLE);
    if (id == null) return null;

    return findConfigurableInGroups(id, groups);
  }

  @Nullable
  private static Configurable findConfigurableInGroups(String id, Configurable.Composite... groups) {
    // avoid unnecessary group expand: check top-level configurables in all groups before looking at children
    for (Configurable.Composite group : groups) {
      final Configurable[] configurables = group.getConfigurables();
      for (Configurable c : configurables) {
        if (c instanceof SearchableConfigurable && id.equals(((SearchableConfigurable)c).getId())) {
          return c;
        }
        else if (id.equals(c.getClass().getName())) {
          return c;
        }
      }
    }
    for (Configurable.Composite group : groups) {
      final Configurable[] configurables = group.getConfigurables();
      for (Configurable c : configurables) {
        if (c instanceof Configurable.Composite) {
          Configurable result = findConfigurableInGroups(id, (Configurable.Composite)c);
          if (result != null) {
            return result;
          }
        }
      }
    }
    return null;
  }

  @Nullable
  private static Configurable findPreselectedByDisplayName(final String preselectedConfigurableDisplayName, ConfigurableGroup[] groups) {
    final List<Configurable> all = SearchUtil.expand(groups);
    for (Configurable each : all) {
      if (preselectedConfigurableDisplayName.equals(each.getDisplayName())) return each;
    }
    return null;
  }

  @Override
  public void doCancelAction(final AWTEvent source) {
    if (source instanceof KeyEvent || source instanceof ActionEvent) {
      if (myEditor.getContext().isHoldingFilter()) {
        myEditor.clearFilter();
        return;
      }
    }

    super.doCancelAction(source);
  }

  @Override
  public void doCancelAction() {
    saveCurrentConfigurable();
    super.doCancelAction();
  }

  @NotNull
  @Override
  protected Action[] createActions() {
    myApplyAction = new ApplyAction();
    return new Action[]{getOKAction(), getCancelAction(), myApplyAction, getHelpAction()};
  }

  @Override
  protected void doHelpAction() {
    final String topic = myEditor.getHelpTopic();
    if (topic != null) {
      HelpManager.getInstance().invokeHelp(topic);
    }
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myEditor.getPreferredFocusedComponent();
  }

  public Object getData(@NonNls String dataId) {
    if (OptionsEditor.KEY.is(dataId)) {
      return myEditor;
    }
    return null;
  }

  private class ApplyAction extends AbstractAction {
    public ApplyAction() {
      super(CommonBundle.getApplyButtonText());
      setEnabled(false);
    }

    public void actionPerformed(final ActionEvent e) {
      myEditor.apply();
      myEditor.revalidate();
      myEditor.repaint();
    }
  }
}