update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / ide / util / treeView / SourceComparator.java
blob3b9e17b2fbb18c6b675041b3c0c6762d17a2e6bd
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.
16 package com.intellij.ide.util.treeView;
18 import com.intellij.ide.projectView.impl.nodes.ClassTreeNode;
19 import com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode;
20 import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
21 import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
23 import java.util.Comparator;
25 public class SourceComparator implements Comparator<NodeDescriptor>{
26 public static final SourceComparator INSTANCE = new SourceComparator();
28 private SourceComparator() {
31 public int compare(NodeDescriptor nodeDescriptor1, NodeDescriptor nodeDescriptor2) {
32 int weight1 = getWeight(nodeDescriptor1);
33 int weight2 = getWeight(nodeDescriptor2);
34 if (weight1 != weight2) {
35 return weight1 - weight2;
37 if (!(nodeDescriptor1.getParentDescriptor() instanceof ProjectViewProjectNode)){
38 if (nodeDescriptor1 instanceof PsiDirectoryNode || nodeDescriptor1 instanceof PsiFileNode){
39 return nodeDescriptor1.toString().compareToIgnoreCase(nodeDescriptor2.toString());
41 if (nodeDescriptor1 instanceof ClassTreeNode && nodeDescriptor2 instanceof ClassTreeNode){
42 if (((ClassTreeNode)nodeDescriptor1).isTopLevel()){
43 return nodeDescriptor1.toString().compareToIgnoreCase(nodeDescriptor2.toString());
47 int index1 = nodeDescriptor1.getIndex();
48 int index2 = nodeDescriptor2.getIndex();
49 if (index1 == index2) return 0;
50 return index1 < index2 ? -1 : +1;
53 private static int getWeight(NodeDescriptor descriptor) {
54 if (descriptor instanceof PsiDirectoryNode) {
55 return ((PsiDirectoryNode)descriptor).isFQNameShown() ? 7 : 0;
57 else {
58 return 2;