2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.keymap.impl;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.components.ApplicationComponent;
20 import com.intellij.openapi.keymap.Keymap;
21 import com.intellij.openapi.keymap.KeymapManager;
22 import com.intellij.openapi.util.InvalidDataException;
23 import com.intellij.openapi.util.JDOMExternalizable;
24 import com.intellij.openapi.util.WriteExternalException;
25 import com.intellij.openapi.util.JDOMUtil;
26 import com.intellij.openapi.extensions.Extensions;
27 import com.intellij.openapi.diagnostic.Logger;
28 import org.jdom.Element;
29 import org.jdom.Document;
30 import org.jetbrains.annotations.NonNls;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
38 * @author Eugene Belyaev
40 public class DefaultKeymap implements JDOMExternalizable, ApplicationComponent {
41 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.keymap.impl.DefaultKeymap");
44 private static final String KEY_MAP = "keymap";
46 private static final String NAME_ATTRIBUTE = "name";
48 private ArrayList<Keymap> myKeymaps = new ArrayList<Keymap>();
50 public static DefaultKeymap getInstance() {
51 return ApplicationManager.getApplication().getComponent(DefaultKeymap.class);
54 public void disposeComponent() {
57 public void initComponent() { }
59 public void readExternal(Element element) throws InvalidDataException{
60 myKeymaps = new ArrayList<Keymap>();
61 loadKeymapsFromElement(element);
63 for(BundledKeymapProvider provider: Extensions.getExtensions(BundledKeymapProvider.EP_NAME)) {
64 final List<String> fileNames = provider.getKeymapFileNames();
65 for (String fileName : fileNames) {
67 final Document document = JDOMUtil.loadResourceDocument(new URL("file:///idea/" + fileName));
68 loadKeymapsFromElement(document.getRootElement());
77 private void loadKeymapsFromElement(final Element element) throws InvalidDataException {
78 for (Iterator i = element.getChildren().iterator(); i.hasNext();) {
79 Element child=(Element)i.next();
80 if (KEY_MAP.equals(child.getName())) {
81 String keymapName = child.getAttributeValue(NAME_ATTRIBUTE);
82 DefaultKeymapImpl keymap = KeymapManager.MAC_OS_X_KEYMAP.equals(keymapName)
83 ? new MacOSDefaultKeymap()
84 : new DefaultKeymapImpl();
85 keymap.readExternal(child, myKeymaps.toArray(new Keymap[myKeymaps.size()]));
86 keymap.setName(keymapName);
87 myKeymaps.add(keymap);
93 * We override this method to disable saving the keymap.
95 public void writeExternal(Element element) throws WriteExternalException{
96 throw new WriteExternalException();
99 public Keymap[] getKeymaps() {
100 return myKeymaps.toArray(new Keymap[myKeymaps.size()]);
103 public String getComponentName() {
104 return "DefaultKeymap";