ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / lang-impl / src / com / intellij / psi / DummyHolderViewProvider.java
blobb11e0b3ade664ded2457e88c848375841a26d667
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.psi;
18 import com.intellij.lang.Language;
19 import com.intellij.openapi.editor.Document;
20 import com.intellij.openapi.fileEditor.FileDocumentManager;
21 import com.intellij.openapi.util.UserDataHolderBase;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.psi.impl.PsiManagerEx;
24 import com.intellij.psi.impl.SharedPsiElementImplUtil;
25 import com.intellij.psi.impl.source.DummyHolder;
26 import com.intellij.psi.impl.source.PsiFileImpl;
27 import com.intellij.psi.impl.source.tree.LeafElement;
28 import com.intellij.testFramework.LightVirtualFile;
29 import com.intellij.util.LocalTimeCounter;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Set;
37 public class DummyHolderViewProvider extends UserDataHolderBase implements FileViewProvider{
38 private DummyHolder myHolder;
39 private final PsiManager myManager;
40 private final long myModificationStamp;
41 private final LightVirtualFile myLightVirtualFile = new LightVirtualFile("DummyHolder");
43 public DummyHolderViewProvider(final PsiManager manager) {
44 myManager = manager;
45 myModificationStamp = LocalTimeCounter.currentTime();
48 @NotNull
49 public PsiManager getManager() {
50 return myManager;
53 @Nullable
54 public Document getDocument() {
55 return FileDocumentManager.getInstance().getDocument(getVirtualFile());
58 @NotNull
59 public CharSequence getContents() {
60 return myHolder != null ? myHolder.getNode().getText() : "";
63 @NotNull
64 public VirtualFile getVirtualFile() {
65 return myLightVirtualFile;
68 @NotNull
69 public Language getBaseLanguage() {
70 return myHolder.getLanguage();
73 @NotNull
74 public Set<Language> getLanguages() {
75 return Collections.singleton(getBaseLanguage());
78 public PsiFile getPsi(@NotNull Language target) {
79 ((PsiManagerEx)myManager).getFileManager().setViewProvider(getVirtualFile(), this);
80 return target == getBaseLanguage() ? myHolder : null;
83 @NotNull
84 public List<PsiFile> getAllFiles() {
85 return Collections.singletonList(getPsi(getBaseLanguage()));
88 public void beforeContentsSynchronized() {}
90 public void contentsSynchronized() {}
92 public boolean isEventSystemEnabled() {
93 return false;
96 public boolean isPhysical() {
97 return false;
100 public long getModificationStamp() {
101 return myModificationStamp;
104 public boolean supportsIncrementalReparse(final Language rootLanguage) {
105 return true;
108 public void rootChanged(PsiFile psiFile) {
111 public void setDummyHolder(final DummyHolder dummyHolder) {
112 myHolder = dummyHolder;
113 //myLightVirtualFile.setContent(null, getContents(), false);
116 public FileViewProvider clone(){
117 throw new RuntimeException("Clone is not supported for DummyHolderProviders. Use DummyHolder clone directly.");
120 public PsiReference findReferenceAt(final int offset) {
121 return SharedPsiElementImplUtil.findReferenceAt(getPsi(getBaseLanguage()), offset);
124 @Nullable
125 public PsiElement findElementAt(final int offset, final Language language) {
126 return language == getBaseLanguage() ? findElementAt(offset) : null;
130 public PsiElement findElementAt(int offset, Class<? extends Language> lang) {
131 if (!lang.isAssignableFrom(getBaseLanguage().getClass())) return null;
132 return findElementAt(offset);
135 public PsiReference findReferenceAt(final int offsetInElement, @NotNull final Language language) {
136 return language == getBaseLanguage() ? findReferenceAt(offsetInElement) : null;
139 public boolean isLockedByPsiOperations() {
140 return false;
143 @NotNull
144 public FileViewProvider createCopy(final LightVirtualFile copy) {
145 throw new RuntimeException("Clone is not supported for DummyHolderProviders. Use DummyHolder clone directly.");
148 public PsiElement findElementAt(final int offset) {
149 final LeafElement element = ((PsiFileImpl)getPsi(getBaseLanguage())).calcTreeElement().findLeafElementAt(offset);
150 return element != null ? element.getPsi() : null;