summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/options/ex/SortedConfigurableGroup.java
blob: 3d87b7679359f7e4a957728c0fa1c2bd05ac0f4b (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
/*
 * 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.ex;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurableGroup;
import com.intellij.openapi.options.OptionsBundle;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 * @author Sergey.Malenkov
 */
public final class SortedConfigurableGroup
  extends SearchableConfigurable.Parent.Abstract
  implements SearchableConfigurable, ConfigurableGroup, Configurable.NoScroll {

  public static ConfigurableGroup getGroup(Configurable... configurables) {
    SortedConfigurableGroup root = new SortedConfigurableGroup("root");
    HashMap<String, SortedConfigurableGroup> map = new HashMap<String, SortedConfigurableGroup>();
    map.put("root", root);
    for (Configurable configurable : configurables) {
      int weight = 0;
      String groupId = null;
      if (configurable instanceof ConfigurableWrapper) {
        ConfigurableWrapper wrapper = (ConfigurableWrapper)configurable;
        weight = wrapper.getExtensionPoint().groupWeight;
        groupId = wrapper.getExtensionPoint().groupId;
      }
      SortedConfigurableGroup composite = map.get(groupId);
      if (composite == null) {
        composite = new SortedConfigurableGroup(groupId);
        map.put(groupId, composite);
      }
      composite.add(weight, configurable);
    }
    // process supported groups
    root.add(60, map.remove("appearance"));
    root.add(50, map.remove("editor"));
    root.add(40, map.remove("project"));
    SortedConfigurableGroup build = map.remove("build");
    if (build == null) {
      build = map.remove("build.tools");
    }
    else {
      build.add(1000, map.remove("build.tools"));
    }
    root.add(30, build);
    root.add(20, map.remove("language"));
    root.add(10, map.remove("tools"));
    root.add(-10, map.remove(null));
    // process unsupported groups
    if (1 < map.size()) {
      for (SortedConfigurableGroup group : map.values()) {
        if (root != group) {
          group.myDisplayName = "Category: " + group.myGroupId;
          root.add(0, group);
        }
      }
    }
    return root;
  }

  private final ArrayList<WeightConfigurable> myList = new ArrayList<WeightConfigurable>();
  private final String myGroupId;
  private String myDisplayName;

  private SortedConfigurableGroup(String groupId) {
    myGroupId = groupId;
  }

  public SortedConfigurableGroup(Configurable... configurables) {
    myGroupId = "root";
    // create groups from configurations
    HashMap<String, SortedConfigurableGroup> map = new HashMap<String, SortedConfigurableGroup>();
    map.put(myGroupId, this);
    for (Configurable configurable : configurables) {
      int weight = 0;
      String groupId = null;
      if (configurable instanceof ConfigurableWrapper) {
        ConfigurableWrapper wrapper = (ConfigurableWrapper)configurable;
        weight = wrapper.getExtensionPoint().groupWeight;
        groupId = wrapper.getExtensionPoint().groupId;
      }
      SortedConfigurableGroup composite = map.get(groupId);
      if (composite == null) {
        composite = new SortedConfigurableGroup(groupId);
        map.put(groupId, composite);
      }
      composite.add(weight, configurable);
    }
    // process supported groups
    add(60, map.remove("appearance"));
    add(50, map.remove("editor"));
    add(40, map.remove("project"));
    SortedConfigurableGroup build = map.remove("build");
    if (build == null) {
      build = map.remove("build.tools");
    }
    else {
      build.add(1000, map.remove("build.tools"));
    }
    add(30, build);
    add(20, map.remove("language"));
    add(10, map.remove("tools"));
    add(-10, map.remove(null));
    // process unsupported groups
    if (1 < map.size()) {
      for (SortedConfigurableGroup group : map.values()) {
        if (this != group) {
          group.myDisplayName = "Category: " + group.myGroupId;
          add(0, group);
        }
      }
    }
  }

  private void add(int weight, Configurable configurable) {
    if (configurable != null) {
      myList.add(new WeightConfigurable(configurable, weight));
    }
  }

  @Override
  protected Configurable[] buildConfigurables() {
    Collections.sort(myList);
    int length = myList.size();
    Configurable[] result = new Configurable[length];
    for (int i = 0; i < result.length; i++) {
      result[i] = myList.get(i).myConfigurable;
    }
    myList.clear();
    return result;
  }

  @NotNull
  @Override
  public String getId() {
    return "configurable.group." + myGroupId;
  }

  @Nullable
  @Override
  public String getHelpTopic() {
    return "configurable.group." + myGroupId + ".help.topic";
  }

  @Nls
  @Override
  public String getDisplayName() {
    return myDisplayName != null ? myDisplayName : OptionsBundle.message("configurable.group." + myGroupId + ".settings.display.name");
  }

  @Override
  public String getShortName() {
    return getDisplayName();
  }

  private static final class WeightConfigurable implements Comparable<WeightConfigurable> {
    private final Configurable myConfigurable;
    private final int myWeight;

    private WeightConfigurable(@NotNull Configurable configurable, int weight) {
      myConfigurable = configurable;
      myWeight = weight;
    }

    @Override
    public int compareTo(@NotNull WeightConfigurable pair) {
      return myWeight > pair.myWeight ? -1 :
             myWeight < pair.myWeight ? 1 :
             StringUtil.naturalCompare(myConfigurable.getDisplayName(), pair.myConfigurable.getDisplayName());
    }
  }
}