/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.maven.dom.model.completion;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElementWeigher;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlText;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomManager;
import com.intellij.util.xml.GenericDomValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.MavenVersionComparable;
import org.jetbrains.idea.maven.dom.converters.MavenArtifactCoordinatesVersionConverter;
import org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates;
import org.jetbrains.idea.maven.indices.MavenProjectIndicesManager;

import java.util.Set;

/**
 * @author Sergey Evdokimov
 */
public class MavenVersionCompletionContributor extends CompletionContributor {

  @Override
  public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
    if (parameters.getCompletionType() != CompletionType.BASIC) return;

    PsiElement element = parameters.getPosition();

    PsiElement xmlText = element.getParent();
    if (!(xmlText instanceof XmlText)) return;

    PsiElement tagElement = xmlText.getParent();

    if (!(tagElement instanceof XmlTag)) return;

    XmlTag tag = (XmlTag)tagElement;

    Project project = element.getProject();

    DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);

    if (!(domElement instanceof GenericDomValue)) return;

    DomElement parent = domElement.getParent();

    if (parent instanceof MavenDomArtifactCoordinates
        && ((GenericDomValue)domElement).getConverter() instanceof MavenArtifactCoordinatesVersionConverter) {
      MavenDomArtifactCoordinates coordinates = (MavenDomArtifactCoordinates)parent;

      String groupId = coordinates.getGroupId().getStringValue();
      String artifactId = coordinates.getArtifactId().getStringValue();

      if (!StringUtil.isEmptyOrSpaces(groupId) && !StringUtil.isEmptyOrSpaces(artifactId)) {
        Set<String> versions = MavenProjectIndicesManager.getInstance(project).getVersions(groupId, artifactId);

        CompletionResultSet newResultSet = result.withRelevanceSorter(CompletionService.getCompletionService().emptySorter().weigh(
          new LookupElementWeigher("mavenVersionWeigher") {
            @Nullable
            @Override
            public Comparable weigh(@NotNull LookupElement element) {
              return new ReverseComparable(new MavenVersionComparable(element.getLookupString()));
            }
          }));

        for (String version : versions) {
          newResultSet.addElement(LookupElementBuilder.create(version));
        }
      }
    }
  }

  private static class ReverseComparable implements Comparable<ReverseComparable> {

    private final Comparable myElement;

    private ReverseComparable(Comparable element) {
      myElement = element;
    }

    @Override
    public int compareTo(ReverseComparable o) {
      return -myElement.compareTo(o.myElement);
    }
  }
}

@@ -696,16 +696,84 @@
 tion.*;%0A
+import com.intellij.codeInsight.completion.impl.NegatingComparable;%0A
 import c
@@ -3418,23 +3418,24 @@
 urn new 
-Reverse
+Negating
 Comparab
@@ -3671,338 +3671,6 @@
  %7D%0A%0A
-  private static class ReverseComparable implements Comparable%3CReverseComparable%3E %7B%0A%0A    private final Comparable myElement;%0A%0A    private ReverseComparable(Comparable element) %7B%0A      myElement = element;%0A    %7D%0A%0A    @Override%0A    public int compareTo(ReverseComparable o) %7B%0A      return -myElement.compareTo(o.myElement);%0A    %7D%0A  %7D%0A
 %7D%0A

Use NegatingComparable