update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / PsiPropertiesProvider.java
blob5b8518f0d70bbdc7c8e2ca47a41f9687f2602197
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 com.intellij.uiDesigner;
18 import com.intellij.openapi.module.Module;
19 import com.intellij.psi.*;
20 import com.intellij.psi.search.GlobalSearchScope;
21 import com.intellij.psi.util.ClassUtil;
22 import com.intellij.psi.util.InheritanceUtil;
23 import com.intellij.psi.util.PropertyUtil;
24 import com.intellij.uiDesigner.lw.*;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
28 import javax.swing.*;
29 import java.awt.*;
30 import java.util.HashMap;
32 /**
33 * @author Anton Katilin
34 * @author Vladimir Kondratyev
36 public final class PsiPropertiesProvider implements PropertiesProvider {
37 private final Module myModule;
38 private final HashMap<String, HashMap> myCache;
40 public PsiPropertiesProvider(@NotNull final Module module) {
41 myModule = module;
42 myCache = new HashMap<String, HashMap>();
45 @Nullable
46 public HashMap getLwProperties(final String className) {
47 if (myCache.containsKey(className)) {
48 return myCache.get(className);
51 final PsiManager psiManager = PsiManager.getInstance(myModule.getProject());
52 final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule);
53 final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(className, scope);
54 if (aClass == null) {
55 return null;
58 final HashMap result = new HashMap();
60 final PsiMethod[] methods = aClass.getAllMethods();
61 for (final PsiMethod method : methods) {
62 // it's a setter candidate.. try to find getter
64 if (!PropertyUtil.isSimplePropertySetter(method)) {
65 continue;
67 final String name = PropertyUtil.getPropertyName(method);
68 if (name == null) {
69 throw new IllegalStateException();
71 final PsiMethod getter = PropertyUtil.findPropertyGetter(aClass, name, false, true);
72 if (getter == null) {
73 continue;
76 final PsiType type = getter.getReturnType();
77 final String propertyClassName = type.getCanonicalText();
79 LwIntrospectedProperty property = CompiledClassPropertiesProvider.propertyFromClassName(propertyClassName, name);
80 if (property == null) {
81 PsiClass propClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(propertyClassName, scope);
82 if (propClass == null) continue;
83 if (propClass.isEnum()) {
84 final String enumClassName = ClassUtil.getJVMClassName(propClass);
85 final ClassLoader loader = LoaderFactory.getInstance(myModule.getProject()).getLoader(myModule);
86 try {
87 property = new LwIntroEnumProperty(name, loader.loadClass(enumClassName));
89 catch (ClassNotFoundException e) {
90 continue;
93 else {
94 PsiClass componentClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(Component.class.getName(), scope);
95 PsiClass listModelClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(ListModel.class.getName(), scope);
96 if (componentClass != null && InheritanceUtil.isInheritorOrSelf(propClass, componentClass, true)) {
97 property = new LwIntroComponentProperty(name, propertyClassName);
99 else if (componentClass != null && listModelClass != null && InheritanceUtil.isInheritorOrSelf(propClass, listModelClass, true)) {
100 property = new LwIntroListModelProperty(name, propertyClassName);
102 else {
103 // type is not supported
104 continue;
109 result.put(name, property);
112 myCache.put(className, result);
113 return result;