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.psi.PsiElement;
25 import com.intellij.psi.PsiFile;
26 import com.intellij.psi.PsiJavaCodeReferenceElement;
27 import com.intellij.util.IncorrectOperationException;
28 import com.intellij.util.xml.DomUtil;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 import org.jetbrains.idea.maven.dom.MavenDomBundle;
32 import org.jetbrains.idea.maven.dom.MavenDomUtil;
33 import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
34 import org.jetbrains.idea.maven.indices.MavenArtifactSearchDialog;
35 import org.jetbrains.idea.maven.model.MavenId;
36 import org.jetbrains.idea.maven.project.MavenProject;
38 import java.util.List;
39 import java.util.regex.Pattern;
41 public class AddMavenDependencyQuickFix implements IntentionAction, LowPriorityAction {
43 private static final Pattern CLASSNAME_PATTERN = Pattern.compile("(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.)*\\p{Lu}\\p{javaJavaIdentifierPart}+");
45 private final PsiJavaCodeReferenceElement myRef;
47 public AddMavenDependencyQuickFix(PsiJavaCodeReferenceElement ref) {
52 public String getText() {
53 return "Add Maven Dependency...";
57 public String getFamilyName() {
58 return MavenDomBundle.message("inspection.group");
61 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
62 return myRef.isValid() && MavenDomUtil.findContainingProject(file) != null && looksLikeClassName(getReferenceText());
65 private static boolean looksLikeClassName(@Nullable String text) {
66 if (text == null) return false;
67 //if (true) return true;
68 return CLASSNAME_PATTERN.matcher(text).matches();
71 public void invoke(@NotNull final Project project, Editor editor, final PsiFile file) throws IncorrectOperationException {
72 if (!myRef.isValid()) return;
74 MavenProject mavenProject = MavenDomUtil.findContainingProject(file);
75 if (mavenProject == null) return;
77 final List<MavenId> ids = MavenArtifactSearchDialog.searchForClass(project, getReferenceText());
78 if (ids.isEmpty()) return;
80 final MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(project, mavenProject.getFile());
81 if (model == null) return;
83 new WriteCommandAction(project, "Add Maven Dependency", DomUtil.getFile(model)) {
85 protected void run(Result result) throws Throwable {
86 for (MavenId each : ids) {
87 MavenDomUtil.createDomDependency(model, null, each);
93 public String getReferenceText() {
94 PsiJavaCodeReferenceElement result = myRef;
96 PsiElement parent = result.getParent();
97 if (!(parent instanceof PsiJavaCodeReferenceElement)) {
101 result = (PsiJavaCodeReferenceElement)parent;
104 return result.getQualifiedName();
107 public boolean startInWriteAction() {