summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/actionSystem/impl/AbbreviationManagerImpl.java
blob: 0d116c7a927f99bf73808a1e10b66cf4b58d2a99 (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
/*
 * 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.actionSystem.impl;

import com.intellij.openapi.actionSystem.AbbreviationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.*;
import gnu.trove.THashMap;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.*;

/**
 * @author Konstantin Bulenkov
 */
@State(
  name = "AbbreviationManager",
  roamingType = RoamingType.PER_PLATFORM,
  storages = {
    @Storage(
      file = StoragePathMacros.APP_CONFIG + "/abbreviations.xml"
    )}
)
public class AbbreviationManagerImpl extends AbbreviationManager implements
                                                                 ExportableComponent, PersistentStateComponent<Element> {
  private final Map<String, List<String>> myAbbreviation2ActionId = new THashMap<String, List<String>>();
  private final Map<String, LinkedHashSet<String>> myActionId2Abbreviations = new THashMap<String, LinkedHashSet<String>>();
  private final Map<String, LinkedHashSet<String>> myPluginsActionId2Abbreviations = new THashMap<String, LinkedHashSet<String>>();

  @Nullable
  @Override
  public Element getState() {
    final Element actions = new Element("actions");
    if (myActionId2Abbreviations.isEmpty()) {
      return actions;
    }

    Element abbreviations = null;
    for (String key : myActionId2Abbreviations.keySet()) {
      final LinkedHashSet<String> abbrs = myActionId2Abbreviations.get(key);
      final LinkedHashSet<String> pluginAbbrs = myPluginsActionId2Abbreviations.get(key);
      if (abbrs == pluginAbbrs || (abbrs != null && abbrs.equals(pluginAbbrs))) {
        continue;
      }

      if (abbrs != null) {
        if (abbreviations == null) {
          abbreviations = new Element("abbreviations");
          actions.addContent(abbreviations);
        }

        final Element action = new Element("action");
        action.setAttribute("id", key);
        abbreviations.addContent(action);
        for (String abbr : abbrs) {
          final Element abbreviation = new Element("abbreviation");
          abbreviation.setAttribute("name", abbr);
          action.addContent(abbreviation);
        }
      }
    }

    return actions;
  }

  @Override
  public void loadState(Element state) {
    final List<Element> abbreviations = state.getChildren("abbreviations");
    if (abbreviations != null && abbreviations.size() == 1) {
      final List<Element> actions = abbreviations.get(0).getChildren("action");
      if (actions != null && actions.size() > 0) {
        for (Element action : actions) {
          final String actionId = action.getAttributeValue("id");
          LinkedHashSet<String> values = myActionId2Abbreviations.get(actionId);
          if (values == null) {
            values = new LinkedHashSet<String>(1);
            myActionId2Abbreviations.put(actionId, values);
          }

          final List<Element> abbreviation = action.getChildren("abbreviation");
          if (abbreviation != null) {
            for (Element abbr : abbreviation) {
              final String abbrValue = abbr.getAttributeValue("name");
              if (abbrValue != null) {
                values.add(abbrValue);
                List<String> actionIds = myAbbreviation2ActionId.get(abbrValue);
                if (actionIds == null) {
                  actionIds = new ArrayList<String>();
                  myAbbreviation2ActionId.put(abbrValue, actionIds);
                }
                actionIds.add(actionId);
              }
            }
          }
        }
      }
    }
  }

  @NotNull
  @Override
  public File[] getExportFiles() {
    return new File[]{PathManager.getOptionsFile("abbreviations")};
  }

  @NotNull
  @Override
  public String getPresentableName() {
    return "Actions";
  }

  @Override
  public Set<String> getAbbreviations() {
    final Set<String> result = new HashSet<String>();
    for (Set<String> abbrs : myActionId2Abbreviations.values()) {
      result.addAll(abbrs);
    }
    return result;
  }

  @Override
  public Set<String> getAbbreviations(String actionId) {
    final LinkedHashSet<String> abbreviations = myActionId2Abbreviations.get(actionId);
    if (abbreviations == null) {
      return Collections.emptySet();
    }
    return Collections.unmodifiableSet(abbreviations);
  }

  @Override
  public List<String> findActions(String abbreviation) {
    final List<String> actions = myAbbreviation2ActionId.get(abbreviation);
    return actions == null ? Collections.<String>emptyList() : Collections.unmodifiableList(actions);
  }


  public void register(String abbreviation, String actionId, Map<String, LinkedHashSet<String>> storage) {
    LinkedHashSet<String> abbreviations = storage.get(actionId);
    if (abbreviations == null) {
      abbreviations = new LinkedHashSet<String>(1);
      storage.put(actionId, abbreviations);
    }
    abbreviations.add(abbreviation);
  }

  public void register(String abbreviation, String actionId, boolean fromPluginXml) {
    if (fromPluginXml && myActionId2Abbreviations.containsKey(actionId)) {
      register(abbreviation, actionId, myPluginsActionId2Abbreviations);
      return;
    }
    register(abbreviation, actionId, myActionId2Abbreviations);
    if (fromPluginXml) {
      register(abbreviation, actionId, myPluginsActionId2Abbreviations);
    }

    List<String> ids = myAbbreviation2ActionId.get(abbreviation);
    if (ids == null) {
      ids = new ArrayList<String>(0);
      myAbbreviation2ActionId.put(abbreviation, ids);
    }

    if (!ids.contains(actionId)) {
      ids.add(actionId);
    }
  }

  @Override
  public void register(String abbreviation, String actionId) {
    register(abbreviation, actionId, false);
  }

  @Override
  public void remove(String abbreviation, String actionId) {
    final List<String> actions = myAbbreviation2ActionId.get(abbreviation);
    if (actions != null) {
      actions.remove(actionId);
    }
    final LinkedHashSet<String> abbreviations = myActionId2Abbreviations.get(actionId);
    if (abbreviations != null) {
      abbreviations.remove(abbreviation);
    } else {
      final LinkedHashSet<String> abbrs = myActionId2Abbreviations.get(actionId);
      if (abbrs != null) {
        final LinkedHashSet<String> customValues = new LinkedHashSet<String>(abbrs);
        customValues.remove(abbreviation);
        myActionId2Abbreviations.put(actionId, customValues);
      }
    }
  }
}