update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInspection / ui / InspectionResultsViewComparator.java
blob84435173e767e006e9b4e43dbf93927f9779d710
1 /*
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.
18 * Created by IntelliJ IDEA.
19 * User: max
20 * Date: Nov 23, 2001
21 * Time: 10:31:03 PM
22 * To change template for new class use
23 * Code Style | Class Templates options (Tools | IDE Options).
25 package com.intellij.codeInspection.ui;
27 import com.intellij.codeInsight.daemon.impl.SeverityRegistrar;
28 import com.intellij.codeInspection.CommonProblemDescriptor;
29 import com.intellij.codeInspection.ProblemDescriptor;
30 import com.intellij.codeInspection.offline.OfflineProblemDescriptor;
31 import com.intellij.codeInspection.offlineViewer.OfflineProblemDescriptorNode;
32 import com.intellij.codeInspection.offlineViewer.OfflineRefElementNode;
33 import com.intellij.codeInspection.reference.RefElement;
34 import com.intellij.codeInspection.reference.RefEntity;
35 import com.intellij.openapi.editor.Document;
36 import com.intellij.profile.codeInspection.ui.InspectionsConfigTreeComparator;
37 import com.intellij.psi.PsiDocumentManager;
38 import com.intellij.psi.PsiElement;
39 import com.intellij.psi.util.PsiUtilBase;
41 import java.util.Comparator;
43 public class InspectionResultsViewComparator implements Comparator {
44 public int compare(Object o1, Object o2) {
45 InspectionTreeNode node1 = (InspectionTreeNode)o1;
46 InspectionTreeNode node2 = (InspectionTreeNode)o2;
48 if (node1 instanceof InspectionSeverityGroupNode && node2 instanceof InspectionSeverityGroupNode) {
49 final InspectionSeverityGroupNode groupNode1 = (InspectionSeverityGroupNode)node1;
50 final InspectionSeverityGroupNode groupNode2 = (InspectionSeverityGroupNode)node2;
51 return -SeverityRegistrar.getInstance(groupNode1.getProject()).compare(groupNode1.getSeverityLevel().getSeverity(), groupNode2.getSeverityLevel().getSeverity());
54 if (node1 instanceof InspectionGroupNode && node2 instanceof InspectionGroupNode) {
55 return ((InspectionGroupNode)node1).getGroupTitle().compareToIgnoreCase(((InspectionGroupNode)node2).getGroupTitle());
58 if (node1 instanceof InspectionPackageNode && node2 instanceof InspectionPackageNode) {
59 return ((InspectionPackageNode)node1).getPackageName().compareToIgnoreCase(((InspectionPackageNode)node2).getPackageName());
62 if (node1 instanceof InspectionNode && node2 instanceof InspectionNode)
63 return InspectionsConfigTreeComparator.getDisplayTextToSort(node1.toString())
64 .compareToIgnoreCase(InspectionsConfigTreeComparator.getDisplayTextToSort(node2.toString()));
66 if (node1 instanceof InspectionNode) return -1;
67 if (node2 instanceof InspectionNode) return 1;
69 if (node1 instanceof OfflineRefElementNode && node2 instanceof OfflineRefElementNode ||
70 node1 instanceof OfflineProblemDescriptorNode && node2 instanceof OfflineProblemDescriptorNode) {
71 final Object userObject1 = node1.getUserObject();
72 final Object userObject2 = node2.getUserObject();
73 if (userObject1 instanceof OfflineProblemDescriptor && userObject2 instanceof OfflineProblemDescriptor) {
74 final OfflineProblemDescriptor descriptor1 = (OfflineProblemDescriptor)userObject1;
75 final OfflineProblemDescriptor descriptor2 = (OfflineProblemDescriptor)userObject2;
76 if (descriptor1.getLine() != descriptor2.getLine()) return descriptor1.getLine() - descriptor2.getLine();
77 return descriptor1.getFQName().compareTo(descriptor2.getFQName());
79 if (userObject1 instanceof OfflineProblemDescriptor) {
80 return compareLineNumbers(userObject2, (OfflineProblemDescriptor)userObject1);
82 if (userObject2 instanceof OfflineProblemDescriptor) {
83 return -compareLineNumbers(userObject1, (OfflineProblemDescriptor)userObject2);
87 if (node1 instanceof RefElementNode && node2 instanceof RefElementNode){ //sort by filename and inside file by start offset
88 return compareEntities(((RefElementNode)node1).getElement(), ((RefElementNode)node2).getElement());
90 if (node1 instanceof ProblemDescriptionNode && node2 instanceof ProblemDescriptionNode) {
91 final CommonProblemDescriptor descriptor1 = ((ProblemDescriptionNode)node1).getDescriptor();
92 final CommonProblemDescriptor descriptor2 = ((ProblemDescriptionNode)node2).getDescriptor();
93 if (descriptor1 instanceof ProblemDescriptor && descriptor2 instanceof ProblemDescriptor) {
94 return ((ProblemDescriptor)descriptor1).getLineNumber() - ((ProblemDescriptor)descriptor2).getLineNumber();
96 if (descriptor1 != null && descriptor2 != null) {
97 return descriptor1.getDescriptionTemplate().compareToIgnoreCase(descriptor2.getDescriptionTemplate());
99 return 0;
102 if (node1 instanceof RefElementNode && node2 instanceof ProblemDescriptionNode) {
103 final CommonProblemDescriptor descriptor = ((ProblemDescriptionNode)node2).getDescriptor();
104 if (descriptor instanceof ProblemDescriptor) {
105 return compareEntity(((RefElementNode)node1).getElement(), ((ProblemDescriptor)descriptor).getPsiElement());
107 return compareEntities(((RefElementNode)node1).getElement(), ((ProblemDescriptionNode)node2).getElement());
110 if (node2 instanceof RefElementNode && node1 instanceof ProblemDescriptionNode) {
111 final CommonProblemDescriptor descriptor = ((ProblemDescriptionNode)node1).getDescriptor();
112 if (descriptor instanceof ProblemDescriptor) {
113 return -compareEntity(((RefElementNode)node2).getElement(), ((ProblemDescriptor)descriptor).getPsiElement());
115 return -compareEntities(((RefElementNode)node2).getElement(), ((ProblemDescriptionNode)node1).getElement());
118 return 0;
121 private static int compareEntity(final RefEntity entity, final PsiElement element) {
122 if (entity instanceof RefElement) {
123 return PsiUtilBase.compareElementsByPosition(((RefElement)entity).getElement(), element);
125 return -1;
128 private static int compareEntities(final RefEntity entity1, final RefEntity entity2) {
129 if (entity1 instanceof RefElement && entity2 instanceof RefElement) {
130 return PsiUtilBase.compareElementsByPosition(((RefElement)entity1).getElement(), ((RefElement)entity2).getElement());
131 } else if (entity1 != null && entity2 != null) {
132 return entity1.getName().compareToIgnoreCase(entity2.getName());
134 return 0;
137 private static int compareLineNumbers(final Object userObject, final OfflineProblemDescriptor descriptor) {
138 if (userObject instanceof RefElement) {
139 final RefElement refElement = (RefElement)userObject;
140 final PsiElement psiElement = refElement.getElement();
141 if (psiElement != null) {
142 Document document = PsiDocumentManager.getInstance(psiElement.getProject()).getDocument(psiElement.getContainingFile());
143 if (document != null) {
144 return descriptor.getLine() - document.getLineNumber(psiElement.getTextOffset()) -1;
148 return -1;
151 private static class InspectionResultsViewComparatorHolder {
152 private static final InspectionResultsViewComparator ourInstance = new InspectionResultsViewComparator();
155 public static InspectionResultsViewComparator getInstance() {
157 return InspectionResultsViewComparatorHolder.ourInstance;