NPE (18686)
[fedora-idea.git] / plugins / properties / src / com / intellij / lang / properties / uml / PropertyChangeTracker.java
blob8c6ec5f863913ce9b84eb8f6c692b340b8a77b1f
1 /*
2 * Copyright 2000-2010 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.lang.properties.uml;
18 import com.intellij.lang.properties.psi.PropertiesFile;
19 import com.intellij.lang.properties.psi.Property;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vcs.FileStatus;
22 import com.intellij.openapi.vfs.LocalFileSystem;
23 import com.intellij.openapi.vfs.VirtualFile;
24 import com.intellij.psi.PsiElement;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.psi.PsiManager;
27 import com.intellij.psi.PsiNamedElement;
28 import com.intellij.psi.util.PsiFilter;
29 import com.intellij.uml.UmlChangeTracker;
30 import org.jetbrains.annotations.Nullable;
32 import java.io.File;
33 import java.util.HashMap;
34 import java.util.Map;
36 /**
37 * @author Konstantin Bulenkov
39 public class PropertyChangeTracker extends UmlChangeTracker<PropertiesFile, Property, PsiElement> {
40 private static final PsiFilter<Property> PROPERTY_FILTER =
41 new PsiFilter<Property>(Property.class) {
42 @Override
43 public boolean areEquivalent(Property e1, Property e2) {
44 final String key1 = e1.getKey();
45 return key1 != null && key1.equals(e2.getKey());
48 private HashMap<PropertiesFile, FileStatus> map;
50 public PropertyChangeTracker(Project project, @Nullable PsiFile before, @Nullable PsiFile after) {
51 super(project, before, after);
54 @Override
55 public PsiFilter<PropertiesFile>[] getNodeFilters() {
56 return new PsiFilter[] {new PsiFilter(PropertiesFile.class)};
59 @Override
60 public PsiFilter<Property>[] getNodeContentFilters() {
61 return new PsiFilter[] {PROPERTY_FILTER};
64 @Override
65 public Map<PropertiesFile, FileStatus> getNodeElements() {
66 if (map == null) {
67 map = new HashMap<PropertiesFile, FileStatus>();
68 final PropertiesFile after = (PropertiesFile)getAfter();
69 final PropertiesFile before = (PropertiesFile)getBefore();
70 if (after == null) {
71 map.put(before, FileStatus.DELETED);
72 } else if (before == null) {
73 map.put(after, FileStatus.ADDED);
74 } else {
75 map.put(after, FileStatus.MODIFIED);
78 return map;
81 @Override
82 public RelationshipInfo[] getRelationships() {
83 return RelationshipInfo.EMPTY;
86 @Override
87 public PsiNamedElement findElementByFQN(Project project, String fqn) {
88 final File file = new File(fqn);
89 if (file.exists()) {
90 final VirtualFile vf = LocalFileSystem.getInstance().findFileByIoFile(file);
91 if (vf != null) {
92 final PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
93 if (psiFile instanceof PropertiesFile) {
94 return psiFile;
98 return null;
101 @Override
102 public String getPresentableName(PsiNamedElement e) {
103 return e instanceof Property ? ((Property)e).getKey() : super.getPresentableName(e);