d003c34750b20228e8e43e369fa16f3b3e7af3f7
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / keymap / impl / DefaultKeymap.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.openapi.keymap.impl;
17
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;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.net.URL;
36
37 /**
38  * @author Eugene Belyaev
39  */
40 public class DefaultKeymap implements JDOMExternalizable, ApplicationComponent {
41   private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.keymap.impl.DefaultKeymap");
42
43   @NonNls
44   private static final String KEY_MAP = "keymap";
45   @NonNls
46   private static final String NAME_ATTRIBUTE = "name";
47
48   private ArrayList<Keymap> myKeymaps = new ArrayList<Keymap>();
49
50   public static DefaultKeymap getInstance() {
51     return ApplicationManager.getApplication().getComponent(DefaultKeymap.class);
52   }
53
54   public void disposeComponent() {
55   }
56
57   public void initComponent() { }
58
59   public void readExternal(Element element) throws InvalidDataException{
60     myKeymaps = new ArrayList<Keymap>();
61     loadKeymapsFromElement(element);
62
63     for(BundledKeymapProvider provider: Extensions.getExtensions(BundledKeymapProvider.EP_NAME)) {
64       final List<String> fileNames = provider.getKeymapFileNames();
65       for (String fileName : fileNames) {
66         try {
67           final Document document = JDOMUtil.loadResourceDocument(new URL("file:///idea/" + fileName));
68           loadKeymapsFromElement(document.getRootElement());
69         }
70         catch (Exception e) {
71           LOG.error(e);
72         }
73       }
74     }
75   }
76
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);
88       }
89     }
90   }
91
92   /**
93    * We override this method to disable saving the keymap.
94    */
95   public void writeExternal(Element element) throws WriteExternalException{
96     throw new WriteExternalException();
97   }
98
99   public Keymap[] getKeymaps() {
100     return myKeymaps.toArray(new Keymap[myKeymaps.size()]);
101   }
102
103   public String getComponentName() {
104     return "DefaultKeymap";
105   }
106
107 }