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.testIntegration;
19 import com.intellij.codeInsight.CodeInsightBundle;
20 import com.intellij.codeInsight.navigation.GotoTargetHandler;
21 import com.intellij.codeInsight.navigation.NavigationUtil;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.util.IconLoader;
25 import com.intellij.pom.Navigatable;
26 import com.intellij.psi.PsiElement;
27 import com.intellij.psi.PsiFile;
28 import com.intellij.psi.util.PsiUtilBase;
29 import com.intellij.util.SmartList;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
34 import java.util.Collection;
35 import java.util.List;
37 public class GotoTestOrCodeHandler extends GotoTargetHandler {
38 protected String getFeatureUsedKey() {
39 return "navigation.goto.testOrCode";
43 protected GotoData getSourceAndTargetElements(final Editor editor, final PsiFile file) {
44 PsiElement selectedElement = getSelectedElement(editor, file);
45 PsiElement sourceElement = TestFinderHelper.findSourceElement(selectedElement);
46 if (sourceElement == null) return null;
48 List<AdditionalAction> actions = new SmartList<AdditionalAction>();
50 Collection<PsiElement> candidates;
51 if (TestFinderHelper.isTest(selectedElement)) {
52 candidates = TestFinderHelper.findClassesForTest(selectedElement);
55 candidates = TestFinderHelper.findTestsForClass(selectedElement);
56 final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage());
57 if (creator != null && creator.isAvailable(file.getProject(), editor, file)) {
58 actions.add(new AdditionalAction() {
60 public String getText() {
61 return "Create New Test...";
65 public Icon getIcon() {
66 return IconLoader.getIcon("/actions/intentionBulb.png");
70 public void execute() {
71 creator.createTest(file.getProject(), editor, file);
77 return new GotoData(sourceElement, candidates.toArray(new PsiElement[candidates.size()]), actions);
81 public static PsiElement getSelectedElement(Editor editor, PsiFile file) {
82 return PsiUtilBase.getElementAtOffset(file, editor.getCaretModel().getOffset());
86 protected boolean shouldSortTargets() {
90 protected String getChooserTitle(PsiElement sourceElement, String name, int length) {
91 if (TestFinderHelper.isTest(sourceElement)) {
92 return CodeInsightBundle.message("goto.test.chooserTitle.subject", name, length);
95 return CodeInsightBundle.message("goto.test.chooserTitle.test", name, length);
100 protected String getNotFoundMessage(Project project, Editor editor, PsiFile file) {
101 return CodeInsightBundle.message("goto.test.notFound");
105 protected void navigateToElement(Navigatable element) {
106 if (element instanceof PsiElement) {
107 NavigationUtil.activateFileWithPsiElement((PsiElement)element);
110 element.navigate(true);