summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/SearchContext.java
blob: 045c2749871ce150c77b16a6e837bf06780a6091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.intellij.structuralsearch.plugin.ui;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;

/**
 * Context of the search to be done
 */
public final class SearchContext implements Cloneable {
  private final PsiFile file;
  private final Project project;

  private SearchContext(Project project, PsiFile file) {
    this.project = project;
    this.file = file;
  }

  public PsiFile getFile() {
    return file;
  }

  public Project getProject() {
    return project;
  }

  public static SearchContext buildFromDataContext(DataContext context) {
    Project project = CommonDataKeys.PROJECT.getData(context);
    if (project == null) {
      project = ProjectManager.getInstance().getDefaultProject();
    }

    PsiFile file = CommonDataKeys.PSI_FILE.getData(context);
    final VirtualFile vFile = CommonDataKeys.VIRTUAL_FILE.getData(context);
    if (vFile != null && (file == null || !vFile.equals(file.getContainingFile().getVirtualFile()))) {
      file = PsiManager.getInstance(project).findFile(vFile);
    }
    return new SearchContext(project, file);
  }

  public Editor getEditor() {
    return FileEditorManager.getInstance(project).getSelectedTextEditor();
  }

  protected Object clone() {
    try {
      return super.clone();
    } catch(CloneNotSupportedException ex) {
      return null;
    }
  }
}