update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / vcsUtil / JavaVcsSelectionProvider.java
blob973d094b510a68412acd32b6432d29887c02d9fd
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.vcsUtil;
18 import com.intellij.codeInsight.TargetElementUtilBase;
19 import com.intellij.openapi.editor.Document;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.openapi.fileEditor.FileDocumentManager;
22 import com.intellij.openapi.util.TextRange;
23 import com.intellij.openapi.vcs.VcsBundle;
24 import com.intellij.openapi.vcs.actions.VcsContext;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.psi.*;
27 import org.jetbrains.annotations.Nullable;
29 /**
30 * @author yole
32 public class JavaVcsSelectionProvider implements VcsSelectionProvider {
33 @Nullable
34 public VcsSelection getSelection(final VcsContext context) {
35 final Editor editor = context.getEditor();
36 if (editor == null) return null;
37 PsiElement psiElement = TargetElementUtilBase.findTargetElement(editor, TargetElementUtilBase.ELEMENT_NAME_ACCEPTED);
38 if (psiElement == null) {
39 return null;
41 if (!psiElement.isValid()) {
42 return null;
44 if (psiElement instanceof PsiCompiledElement) {
45 return null;
48 final String actionName;
50 if (psiElement instanceof PsiClass) {
51 actionName = VcsBundle.message("action.name.show.history.for.class");
53 else if (psiElement instanceof PsiField) {
54 actionName = VcsBundle.message("action.name.show.history.for.field");
56 else if (psiElement instanceof PsiMethod) {
57 actionName = VcsBundle.message("action.name.show.history.for.method");
59 else if (psiElement instanceof PsiCodeBlock) {
60 actionName = VcsBundle.message("action.name.show.history.for.code.block");
62 else if (psiElement instanceof PsiStatement) {
63 actionName = VcsBundle.message("action.name.show.history.for.statement");
65 else {
66 return null;
69 TextRange textRange = psiElement.getTextRange();
70 if (textRange == null) {
71 return null;
74 VirtualFile virtualFile = psiElement.getContainingFile().getVirtualFile();
75 if (virtualFile == null) {
76 return null;
78 if (!virtualFile.isValid()) {
79 return null;
82 Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
83 return new VcsSelection(document, textRange, actionName);