update copyright
[fedora-idea.git] / plugins / ui-designer / src / com / intellij / uiDesigner / binding / FormClassIndex.java
blob62c28bbd17e9dab1a15d0155b293a21dc7001969
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.binding;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.fileTypes.StdFileTypes;
20 import com.intellij.openapi.project.IndexNotReadyException;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.util.Computable;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.psi.PsiClass;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.psi.PsiManager;
27 import com.intellij.psi.search.GlobalSearchScope;
28 import com.intellij.psi.search.ProjectScope;
29 import com.intellij.uiDesigner.compiler.Utils;
30 import com.intellij.util.indexing.*;
31 import com.intellij.util.io.EnumeratorStringDescriptor;
32 import com.intellij.util.io.KeyDescriptor;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
36 import java.util.*;
38 /**
39 * @author yole
41 public class FormClassIndex extends ScalarIndexExtension<String> {
42 @NonNls public static final ID<String, Void> NAME = ID.create("FormClassIndex");
43 private final EnumeratorStringDescriptor myKeyDescriptor = new EnumeratorStringDescriptor();
44 private final MyInputFilter myInputFilter = new MyInputFilter();
45 private final MyDataIndexer myDataIndexer = new MyDataIndexer();
47 public ID<String, Void> getName() {
48 return NAME;
51 public DataIndexer<String, Void, FileContent> getIndexer() {
52 return myDataIndexer;
55 public KeyDescriptor<String> getKeyDescriptor() {
56 return myKeyDescriptor;
59 public FileBasedIndex.InputFilter getInputFilter() {
60 return myInputFilter;
63 public boolean dependsOnFileContent() {
64 return true;
67 public int getVersion() {
68 return 0;
71 private static class MyDataIndexer implements DataIndexer<String, Void, FileContent> {
72 @NotNull
73 public Map<String, Void> map(final FileContent inputData) {
74 String className = null;
75 try {
76 className = Utils.getBoundClassName(inputData.getContentAsText().toString());
78 catch (Exception e) {
79 // ignore
81 if (className != null) {
82 return Collections.singletonMap(className, null);
84 return Collections.emptyMap();
88 private static class MyInputFilter implements FileBasedIndex.InputFilter {
89 public boolean acceptInput(final VirtualFile file) {
90 return file.getFileType() == StdFileTypes.GUI_DESIGNER_FORM;
94 public static List<PsiFile> findFormsBoundToClass(Project project, String className) {
95 return findFormsBoundToClass(project, className, ProjectScope.getAllScope(project));
98 public static List<PsiFile> findFormsBoundToClass(final Project project,
99 final String className,
100 final GlobalSearchScope scope) {
101 return ApplicationManager.getApplication().runReadAction(new Computable<List<PsiFile>>() {
102 public List<PsiFile> compute() {
103 final Collection<VirtualFile> files;
104 try {
105 files = FileBasedIndex.getInstance().getContainingFiles(NAME, className,
106 GlobalSearchScope.projectScope(project).intersectWith(scope));
108 catch (IndexNotReadyException e) {
109 return Collections.emptyList();
111 if (files.isEmpty()) return Collections.emptyList();
112 List<PsiFile> result = new ArrayList<PsiFile>();
113 for(VirtualFile file: files) {
114 if (!file.isValid()) continue;
115 PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
116 if (psiFile != null) {
117 result.add(psiFile);
120 return result;
125 public static List<PsiFile> findFormsBoundToClass(PsiClass psiClass) {
126 String qName = FormReferencesSearcher.getQualifiedName(psiClass);
127 if (qName == null) return Collections.emptyList();
128 return findFormsBoundToClass(psiClass.getProject(), qName);
131 public static List<PsiFile> findFormsBoundToClass(PsiClass psiClass, GlobalSearchScope scope) {
132 String qName = FormReferencesSearcher.getQualifiedName(psiClass);
133 if (qName == null) return Collections.emptyList();
134 return findFormsBoundToClass(psiClass.getProject(), qName, scope);