package com.intellij.ide.impl;

import com.intellij.ide.SelectInTarget;
import com.intellij.ide.SelectInContext;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;


public abstract class SelectInTargetPsiWrapper implements SelectInTarget {
  protected final Project myProject;

  protected SelectInTargetPsiWrapper(final Project project) {
    myProject = project;
  }

  public abstract String toString();

  protected abstract boolean canSelect(PsiFile file);

  public final boolean canSelect(SelectInContext context) {
    final Document document = FileDocumentManager.getInstance().getDocument(context.getVirtualFile());
    if (document != null) {
      final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
      if (psiFile != null) {
        if (canSelect(psiFile)){
          return true;
        }
      }
    }

    return canWorkWithCustomObjects();
  }

  public final void selectIn(SelectInContext context, final boolean requestFocus) {
    final Object selector = context.getSelectorInFile();
    if (selector instanceof PsiElement) {
      select(((PsiElement)selector).getOriginalElement(), requestFocus);
    } else {
      select(selector, context.getVirtualFile(), requestFocus);
    }
  }

  protected abstract void select(final Object selector, VirtualFile virtualFile, final boolean requestFocus);

  protected abstract boolean canWorkWithCustomObjects();

  protected abstract void select(PsiElement element, boolean requestFocus);
}

@@ -61,167 +61,54 @@
 ctIn
-Target;%0Aimport com.intellij.ide.SelectInContext;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiDocumentManager;%0Aimport com.intellij.psi.PsiElemen
+Context;%0Aimport com.intellij.ide.SelectInTarge
 t;%0Ai
@@ -301,16 +301,207 @@
 alFile;%0A
+import com.intellij.psi.PsiDocumentManager;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiManager;%0Aimport org.jetbrains.annotations.NotNull;%0A
 %0A%0Apublic
@@ -644,16 +644,25 @@
 Wrapper(
+@NotNull 
 final Pr
@@ -964,16 +964,43 @@
 ile());%0A
+    final PsiFile psiFile;%0A
     if (
@@ -1021,38 +1021,24 @@
 ll) %7B%0A      
-final PsiFile 
 psiFile = Ps
@@ -1107,29 +1107,69 @@
     
-  if (psiFile != null
+%7D%0A    else if (context.getSelectorInFile() instanceof PsiFile
 ) %7B%0A
@@ -1178,75 +1178,232 @@
     
-  
-if (can
+psiFile = (PsiFile)context.get
 Select
-(psi
+orIn
 File
-))%7B
+();
 %0A    
-      return true
+%7D%0A    else %7B%0A      psiFile = PsiManager.getInstance(myProject).findFile(context.getVirtualFile())
 ;%0A    
+%7D%0A
     
-%7D%0A      %7D
+if (psiFile != null && canSelect(psiFile)) %7B%0A      return true;
 %0A   

make 'select in' work for icons