KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.visual.sort;
2
3 import prefuse.Visualization;
4 import prefuse.visual.AggregateItem;
5 import prefuse.visual.DecoratorItem;
6 import prefuse.visual.EdgeItem;
7 import prefuse.visual.NodeItem;
8 import prefuse.visual.VisualItem;
9
10 /**
11  * ItemSorter that sorts items by tree depths. By default items deeper
12  * in the tree are given lower scores, so that parent nodes are drawn
13  * on top of child nodes. This ordering can be reversed using the
14  * appropriate constructor arguments.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class TreeDepthItemSorter extends ItemSorter {
19
20     protected static final int AGGREGATE = 0;
21     protected static final int EDGE = 1;
22     protected static final int ITEM = 2;
23     protected static final int NODE = 3;
24     protected static final int DECORATOR = 4;
25     
26     private int m_childrenAbove;
27     private int m_hover;
28     private int m_highlight;
29     private int m_depth;
30     
31     /**
32      * Create a new TreeDepthItemSorter that orders nodes such that parents
33      * are placed above their children.
34      */

35     public TreeDepthItemSorter() {
36         this(false);
37     }
38     
39     /**
40      * Create a new TreeDepthItemSorter with the given sort ordering by depth.
41      * @param childrenAbove true if children should be ordered above their
42      * parents, false if parents should be ordered above their children.
43      */

44     public TreeDepthItemSorter(boolean childrenAbove) {
45         if ( childrenAbove ) {
46             m_childrenAbove = 1;
47             m_hover = 13;
48             m_highlight = 12;
49             m_depth = 14;
50         } else {
51             m_childrenAbove = -1;
52             m_hover = 24;
53             m_highlight = 23;
54             m_depth = 12;
55         }
56     }
57     
58     /**
59      * Score items similarly to {@link ItemSorter}, but additionally
60      * ranks items based on their tree depth.
61      * @see prefuse.visual.sort.ItemSorter#score(prefuse.visual.VisualItem)
62      */

63     public int score(VisualItem item) {
64         int type = ITEM;
65         if ( item instanceof EdgeItem ) {
66             type = EDGE;
67         } else if ( item instanceof AggregateItem ) {
68             type = AGGREGATE;
69         } else if ( item instanceof DecoratorItem ) {
70             type = DECORATOR;
71         }
72         
73         int score = (1<<(25+type));
74         if ( item instanceof NodeItem ) {
75             int depth = ((NodeItem)item).getDepth();
76             score += m_childrenAbove*(depth<<m_depth);
77         }
78         if ( item.isHover() ) {
79             score += (1<<m_hover);
80         }
81         if ( item.isHighlighted() ) {
82             score += (1<<m_highlight);
83         }
84         if ( item.isInGroup(Visualization.FOCUS_ITEMS) ) {
85             score += (1<<11);
86         }
87         if ( item.isInGroup(Visualization.SEARCH_ITEMS) ) {
88             score += (1<<10);
89         }
90
91         return score;
92 // int score = 0;
93
// if ( item.isHover() ) {
94
// score += (1<<m_hover);
95
// }
96
// if ( item.isHighlighted() ) {
97
// score += (1<<m_highlight);
98
// }
99
// if ( item instanceof NodeItem ) {
100
// score += (1<<27); // nodes before edges
101
// score += m_childrenAbove*(((NodeItem)item).getDepth()<<m_depth);
102
// }
103
// if ( item.isInGroup(Visualization.FOCUS_ITEMS) ) {
104
// score += (1<<11);
105
// }
106
// if ( item.isInGroup(Visualization.SEARCH_ITEMS) ) {
107
// score += (1<<10);
108
// }
109
// if ( item instanceof DecoratorItem ) {
110
// score += (1<<9);
111
// }
112
// return score;
113
}
114
115 } // end of class TreeDepthItemSorter
116
Popular Tags