Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/eclipse/jdt/annotation/AnnotationSorter.java


1   package com.port80.eclipse.jdt.annotation;
2   
3   import java.sql.Timestamp;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.eclipse.jface.viewers.Viewer;
8   import org.eclipse.jface.viewers.ViewerSorter;
9   
10  import com.port80.eclipse.jdt.util.PersistentItem;
11  
12  /** 
13   * Sort annotation entries according to prefered keys. Current valid keys are "Name", "Path", "Annotation",
14   * "AccessTime", "CreateTime", "Count". AccessTime, CreateTime and Count is sorted numerically larger first.
15   * @ author chrisl 
16   */
17  public class AnnotationSorter extends ViewerSorter {
18  
19    // Fields //////////////////////////////////////////////////////////////
20  
21    public static final String DEF_SORTKEY = "Name";
22    public static final Map SORTKEYMAP = new HashMap();
23    public static final String[] SORTKEYS =
24      new String[] { "Name", "Path", "Annotation", "CreateTime", "AccessTime", "Count" };
25    static {
26      for (int i = 0; i < SORTKEYS.length; ++i)
27        SORTKEYMAP.put(SORTKEYS[i], new Integer(i));
28    }
29    public static final int NAME = 0;
30    public static final int PATH = 1;
31    public static final int ANNOTATION = 2;
32    public static final int CREATETIME = 3;
33    public static final int ACCESSTIME = 4;
34    public static final int COUNT = 5;
35  
36    private int fSortBy = NAME;
37  
38    // Methods /////////////////////////////////////////////////////////////
39  
40    public AnnotationSorter(String sortkey) {
41      setSortKey(sortkey);
42    }
43  
44    public int category(Object element) {
45      return 0;
46    }
47  
48    public int compare(Viewer viewer, Object e1, Object e2) {
49      PersistentItem item1 = (PersistentItem) e1;
50      PersistentItem item2 = (PersistentItem) e2;
51      String name1, name2, ret;
52      Timestamp timestamp1, timestamp2;
53      long time1, time2;
54      int kind1, kind2;
55      switch (fSortBy) {
56        case NAME :
57          kind1 = item1.getKind();
58          kind2 = item2.getKind();
59          if(kind1==PersistentItem.INTERFACE) kind1=PersistentItem.CLASS;
60          if(kind2==PersistentItem.INTERFACE) kind2=PersistentItem.CLASS;
61          if (kind1 > kind2)
62            return 1;
63          if (kind1 < kind2)
64            return -1;
65          return compare(item1.getName(), item2.getName());
66        case PATH :
67          name1 = item1.getFullName();
68          ret = item1.getFullPath();
69          if (ret != null)
70            name1 = ret + "." + name1;
71          name2 = item2.getFullName();
72          ret = item2.getFullPath();
73          if (ret != null)
74            name2 = ret + "." + name2;
75          return collator.compare(name1, name2);
76        case ANNOTATION :
77          return compare(item1.getAnnotation(), item2.getAnnotation());
78        case ACCESSTIME :
79          //NOTE: Sorted in reverse order, numerical larger first.
80          timestamp1 = item1.getATime();
81          if (timestamp1 != null)
82            time1 = timestamp1.getTime();
83          else
84            time1 = 0;
85          timestamp2 = item2.getATime();
86          if (timestamp2 != null)
87            time2 = timestamp2.getTime();
88          else
89            time2 = 0;
90          return (time1 > time2) ? -1 : (time1 < time2) ? 1 : 0;
91        case CREATETIME :
92          //NOTE: Sorted in reverse order, numerical larger first.
93          timestamp1 = item1.getCTime();
94          if (timestamp1 != null)
95            time1 = timestamp1.getTime();
96          else
97            time1 = 0;
98          timestamp2 = item2.getCTime();
99          if (timestamp2 != null)
100           time2 = timestamp2.getTime();
101         else
102           time2 = 0;
103         return (time1 > time2) ? -1 : (time1 < time2) ? 1 : 0;
104       case COUNT:
105         //NOTE: Sorted in reverse order, numerical larger first.
106         kind1=item1.getCount();
107         kind2=item2.getCount();
108         if(kind1==PersistentItem.INTERFACE) kind1=PersistentItem.CLASS;
109         if(kind2==PersistentItem.INTERFACE) kind2=PersistentItem.CLASS;
110         if(kind1>kind2) return -1;
111         if(kind1<kind2) return 1;
112         return 0;
113     }
114     return 0;
115   }
116 
117   // Additional propriatory interface to Viewer ///////////////////
118   //
119   void setSortKey(String sortkey) {
120     Integer ret = (Integer) SORTKEYMAP.get(sortkey);
121     if (ret != null) {
122       fSortBy = ret.intValue();
123     } else
124       fSortBy = NAME;
125   }
126 
127   ////////////////////////////////////////////////////////////////////////
128 
129   private int compare(String name1, String name2) {
130     if (name1 == null) {
131       if (name2 == null)
132         return 0;
133       else
134         return -1;
135     } else if (name2 == null) {
136       return 1;
137     } else
138       return name1.compareTo(name2);
139   }
140 
141   ////////////////////////////////////////////////////////////////////////
142 }