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 org.jetbrains.idea.maven.dom.intentions;
18 import com.intellij.codeInsight.intention.IntentionAction;
19 import com.intellij.codeInsight.intention.LowPriorityAction;
20 import com.intellij.openapi.application.Result;
21 import com.intellij.openapi.command.WriteCommandAction;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.roots.ProjectRootManager;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.psi.PsiElement;
27 import com.intellij.psi.PsiFile;
28 import com.intellij.psi.PsiJavaCodeReferenceElement;
29 import com.intellij.util.IncorrectOperationException;
30 import com.intellij.util.xml.DomUtil;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 import org.jetbrains.idea.maven.dom.MavenDomBundle;
34 import org.jetbrains.idea.maven.dom.MavenDomUtil;
35 import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
36 import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
37 import org.jetbrains.idea.maven.indices.MavenArtifactSearchDialog;
38 import org.jetbrains.idea.maven.model.MavenId;
39 import org.jetbrains.idea.maven.project.MavenProject;
41 import java.util.List;
42 import java.util.regex.Pattern;
44 public class AddMavenDependencyQuickFix implements IntentionAction, LowPriorityAction {
46 private static final Pattern CLASSNAME_PATTERN = Pattern.compile("(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.)*\\p{Lu}\\p{javaJavaIdentifierPart}+");
48 private final PsiJavaCodeReferenceElement myRef;
50 public AddMavenDependencyQuickFix(PsiJavaCodeReferenceElement ref) {
55 public String getText() {
56 return "Add Maven Dependency...";
60 public String getFamilyName() {
61 return MavenDomBundle.message("inspection.group");
64 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
65 return myRef.isValid() && MavenDomUtil.findContainingProject(file) != null && looksLikeClassName(getReferenceText());
68 private static boolean looksLikeClassName(@Nullable String text) {
69 if (text == null) return false;
70 //if (true) return true;
71 return CLASSNAME_PATTERN.matcher(text).matches();
74 public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
75 if (!myRef.isValid()) return;
77 MavenProject mavenProject = MavenDomUtil.findContainingProject(file);
78 if (mavenProject == null) return;
80 final List<MavenId> ids = MavenArtifactSearchDialog.searchForClass(project, getReferenceText());
81 if (ids.isEmpty()) return;
83 final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
84 if (model == null) return;
86 new WriteCommandAction(project, "Add Maven Dependency", DomUtil.getFile(model)) {
88 protected void run(Result result) throws Throwable {
89 boolean isTestSource = false;
91 VirtualFile virtualFile = file.getOriginalFile().getVirtualFile();
92 if (virtualFile != null) {
93 isTestSource = ProjectRootManager.getInstance(project).getFileIndex().isInSourceContent(virtualFile);
96 for (MavenId each : ids) {
97 MavenDomDependency dependency = MavenDomUtil.createDomDependency(model, null, each);
99 dependency.getScope().setStringValue("test");
106 public String getReferenceText() {
107 PsiJavaCodeReferenceElement result = myRef;
109 PsiElement parent = result.getParent();
110 if (!(parent instanceof PsiJavaCodeReferenceElement)) {
114 result = (PsiJavaCodeReferenceElement)parent;
117 return result.getQualifiedName();
120 public boolean startInWriteAction() {