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.
17 package com
.intellij
.ide
.impl
;
19 import com
.intellij
.ide
.SelectInContext
;
20 import com
.intellij
.ide
.SelectInTarget
;
21 import com
.intellij
.openapi
.editor
.Document
;
22 import com
.intellij
.openapi
.fileEditor
.FileDocumentManager
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.psi
.*;
26 import org
.jetbrains
.annotations
.NotNull
;
29 public abstract class SelectInTargetPsiWrapper
implements SelectInTarget
{
30 protected final Project myProject
;
32 protected SelectInTargetPsiWrapper(@NotNull final Project project
) {
36 public abstract String
toString();
38 protected abstract boolean canSelect(PsiFileSystemItem file
);
40 public final boolean canSelect(SelectInContext context
) {
41 if (myProject
.isDisposed()) return false;
43 VirtualFile virtualFile
= context
.getVirtualFile();
44 if (!virtualFile
.isValid()) return false;
46 final Document document
= FileDocumentManager
.getInstance().getDocument(virtualFile
);
47 final PsiFileSystemItem psiFile
;
48 if (document
!= null) {
49 psiFile
= PsiDocumentManager
.getInstance(myProject
).getPsiFile(document
);
51 else if (context
.getSelectorInFile() instanceof PsiFile
) {
52 psiFile
= (PsiFile
)context
.getSelectorInFile();
54 else if (virtualFile
.isDirectory()) {
55 psiFile
= PsiManager
.getInstance(myProject
).findDirectory(virtualFile
);
58 psiFile
= PsiManager
.getInstance(myProject
).findFile(virtualFile
);
60 return psiFile
!= null && canSelect(psiFile
) || canWorkWithCustomObjects();
63 public final void selectIn(SelectInContext context
, final boolean requestFocus
) {
64 VirtualFile file
= context
.getVirtualFile();
65 Object selector
= context
.getSelectorInFile();
66 if (selector
== null) {
67 PsiManager psiManager
= PsiManager
.getInstance(myProject
);
68 selector
= file
.isDirectory() ? psiManager
.findDirectory(file
) : psiManager
.findFile(file
);
71 if (selector
instanceof PsiElement
) {
72 select(((PsiElement
)selector
).getOriginalElement(), requestFocus
);
74 select(selector
, file
, requestFocus
);
78 protected abstract void select(final Object selector
, VirtualFile virtualFile
, final boolean requestFocus
);
80 protected abstract boolean canWorkWithCustomObjects();
82 protected abstract void select(PsiElement element
, boolean requestFocus
);