public String getName() {
return "Find OSGi-ready version";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
final MavenDomDependency dependency = getDependency((XmlTag)problemDescriptor.getPsiElement());
final ObrMavenResult mavenResult =
ObrSearchDialog.queryForMavenArtifact(project, dependency.getArtifactId().toString());
if (mavenResult != null) {
final PsiFile psiFile = problemDescriptor.getPsiElement().getContainingFile();
new WriteCommandAction(project, psiFile) {
protected void run(Result result) throws Throwable
{
MavenDomProjectModel model =
MavenDomUtil.getMavenDomProjectModel(getProject(), psiFile.getVirtualFile());
// adds a new dependency to the end of the list
MavenDomDependency dummy = model.getDependencies().addDependency();
dummy.getArtifactId().setStringValue(mavenResult.getArtifactId());
dummy.getVersion().setStringValue(mavenResult.getVersion());
dummy.getGroupId().setStringValue(mavenResult.getGroupId());
// copy over scope from old item
if (!"".equals(dependency.getScope().getStringValue())) {
dummy.getScope().setStringValue(dependency.getScope().getStringValue());
}
PsiElement newDep = dummy.getXmlElement();
PsiElement oldDep = dependency.getXmlElement();
// add after the old element a copy of our dummy element (which is at the end of the list so far)
oldDep.getParent().addAfter(newDep.copy(), oldDep);
// kill old dependency and the dummy
oldDep.delete();