update copyright
[fedora-idea.git] / plugins / devkit / src / util / ComponentType.java
bloba913756ca282bd87c4858ebc2f85629ae797034b
1 /*
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 org.jetbrains.idea.devkit.util;
18 import com.intellij.openapi.components.ApplicationComponent;
19 import com.intellij.openapi.components.BaseComponent;
20 import com.intellij.openapi.components.ProjectComponent;
21 import com.intellij.openapi.module.ModuleComponent;
22 import com.intellij.psi.*;
23 import com.intellij.psi.xml.XmlFile;
24 import com.intellij.psi.xml.XmlTag;
25 import com.intellij.psi.xml.XmlTagValue;
26 import com.intellij.util.IncorrectOperationException;
27 import org.jetbrains.annotations.Nullable;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.PropertyKey;
31 /**
32 * @author swr
34 public enum ComponentType {
35 APPLICATION(ApplicationComponent.class, "application-components", "new.menu.application.component.text"),
36 PROJECT(ProjectComponent.class, "project-components", "new.menu.project.component.text"),
37 MODULE(ModuleComponent.class, "module-components", "new.menu.module.component.text");
39 public final String myClassName;
40 public final String myPropertyKey;
41 private final String myName;
43 public interface Processor {
44 boolean process(ComponentType type, XmlTag component, @Nullable XmlTagValue impl, @Nullable XmlTagValue intf);
47 ComponentType(Class<? extends BaseComponent> clazz, @NonNls String name,
48 @PropertyKey(resourceBundle = "org.jetbrains.idea.devkit.DevKitBundle") String propertyKey)
50 myPropertyKey = propertyKey;
51 myClassName = clazz.getName();
52 myName = name;
55 public void patchPluginXml(XmlFile pluginXml, PsiClass klass) throws IncorrectOperationException {
56 final XmlTag rootTag = pluginXml.getDocument().getRootTag();
57 if (rootTag != null && "idea-plugin".equals(rootTag.getName())) {
58 XmlTag components = rootTag.findFirstSubTag(myName);
59 if (components == null) {
60 components = (XmlTag)rootTag.add(rootTag.createChildTag(myName, rootTag.getNamespace(), null, false));
63 XmlTag cmp = (XmlTag)components.add(components.createChildTag("component", components.getNamespace(), null, false));
64 cmp.add(cmp.createChildTag("implementation-class", cmp.getNamespace(), klass.getQualifiedName(), false));
66 // some magic to figure out interface-class
67 final PsiMethod[] methods = klass.findMethodsByName("getInstance", true);
68 for (PsiMethod method : methods) {
69 final PsiParameter[] parameters = method.getParameterList().getParameters();
70 if (parameters.length <= 1) {
71 final PsiType returnType = method.getReturnType();
72 if (returnType instanceof PsiClassType) {
73 final String intf = returnType.getCanonicalText();
74 cmp.add(cmp.createChildTag("interface-class", cmp.getNamespace(), intf, false));
75 break;
82 public void process(XmlTag rootTag, Processor processor) {
83 final XmlTag[] compGroup = rootTag.findSubTags(myName);
84 for (XmlTag tag : compGroup) {
85 final XmlTag[] components = tag.findSubTags("component");
86 for (XmlTag component : components) {
87 final XmlTag impl = component.findFirstSubTag("implementation-class");
88 final XmlTag intf = component.findFirstSubTag("interface-class");
89 if (!processor.process(this, component,
90 impl != null ? impl.getValue() : null,
91 intf != null ? intf.getValue() : null))
93 return;