KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > visual > sort > ItemSorter


1 package prefuse.visual.sort;
2
3 import java.util.Comparator JavaDoc;
4
5 import prefuse.Visualization;
6 import prefuse.visual.AggregateItem;
7 import prefuse.visual.DecoratorItem;
8 import prefuse.visual.EdgeItem;
9 import prefuse.visual.VisualItem;
10
11 /**
12  * ItemSorter instances provide an integer score for each VisualItem;
13  * these scores are then used to sort the items in ascending order of score.
14  * ItemSorters are used to determine the rendering order of items in a
15  * Display.
16  *
17  * @author <a HREF="http://jheer.org">jeffrey heer</a>
18  */

19 public class ItemSorter implements Comparator JavaDoc {
20
21     protected static final int AGGREGATE = 0;
22     protected static final int EDGE = 1;
23     protected static final int ITEM = 2;
24     protected static final int DECORATOR = 3;
25     
26     /**
27      * <p>Return an ordering score for an item. The default scoring imparts
28      * the following order:
29      * hover items > highlighted items > items in the
30      * {@link prefuse.Visualization#FOCUS_ITEMS} set >
31      * {@link prefuse.Visualization#SEARCH_ITEMS} set >
32      * DecoratorItem instances > normal VisualItem instances. A zero
33      * score is returned for normal items, with scores starting at
34      * 1&lt;&lt;27 for other items, leaving the number range beneath that
35      * value open for additional nuanced scoring.</p>
36      *
37      * <p>Subclasses can override this method to provide custom sorting
38      * criteria.</p>
39      * @param item the VisualItem to provide an ordering score
40      * @return the ordering score
41      */

42     public int score(VisualItem item) {
43         int type = ITEM;
44         if ( item instanceof EdgeItem ) {
45             type = EDGE;
46         } else if ( item instanceof AggregateItem ) {
47             type = AGGREGATE;
48         } else if ( item instanceof DecoratorItem ) {
49             type = DECORATOR;
50         }
51         
52         int score = (1<<(26+type));
53         if ( item.isHover() ) {
54             score += (1<<25);
55         }
56         if ( item.isHighlighted() ) {
57             score += (1<<24);
58         }
59         if ( item.isInGroup(Visualization.FOCUS_ITEMS) ) {
60             score += (1<<23);
61         }
62         if ( item.isInGroup(Visualization.SEARCH_ITEMS) ) {
63             score += (1<<22);
64         }
65
66         return score;
67     }
68     
69     /**
70      * Compare two items based on their ordering scores. Calls the
71      * {@link #score(VisualItem)} on each item and compares the result.
72      * @param v1 the first VisualItem to compare
73      * @param v2 the second VisualItem to compare
74      * @return -1 if score(v1) &lt; score(v2), 1 if score(v1) &gt; score(v2)
75      * and 0 if score(v1) == score(v2).
76      */

77     public int compare(VisualItem v1, VisualItem v2) {
78         int score1 = score(v1);
79         int score2 = score(v2);
80         return (score1<score2 ? -1 : (score1==score2 ? 0 : 1));
81     }
82     
83     /**
84      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
85      * @see #compare(VisualItem, VisualItem)
86      */

87     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
88         if ( !(o1 instanceof VisualItem && o2 instanceof VisualItem) ) {
89             throw new IllegalArgumentException JavaDoc();
90         }
91         return compare((VisualItem)o1, (VisualItem)o2);
92     }
93
94 } // end of class ItemSorter
95
Popular Tags