KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > action > layout > CollapsedSubtreeLayout


1 package prefuse.action.layout;
2
3 import java.awt.geom.Point2D JavaDoc;
4 import java.awt.geom.Rectangle2D JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import prefuse.Constants;
8 import prefuse.visual.NodeItem;
9 import prefuse.visual.VisualItem;
10 import prefuse.visual.expression.StartVisiblePredicate;
11
12 /**
13  * Layout Action that sets the positions for newly collapsed or newly
14  * expanded nodes of a tree. This action updates positions such that
15  * nodes flow out from their parents or collapse back into their parents
16  * upon animated transitions.
17  *
18  * @author <a HREF="http://jheer.org">jeffrey heer</a>
19  */

20 public class CollapsedSubtreeLayout extends Layout {
21
22     private int m_orientation;
23     private Point2D JavaDoc m_point = new Point2D.Double JavaDoc();
24     
25     /**
26      * Create a new CollapsedSubtreeLayout. By default, nodes will collapse
27      * to the center point of their parents.
28      * @param group the data group to layout (only newly collapsed or newly
29      * expanded items will be considered, as determined by their current
30      * visibility settings).
31      */

32     public CollapsedSubtreeLayout(String JavaDoc group) {
33         this(group, Constants.ORIENT_CENTER);
34     }
35     
36     /**
37      * Create a new CollapsedSubtreeLayout.
38      * @param group the data group to layout (only newly collapsed or newly
39      * expanded items will be considered, as determined by their current
40      * visibility settings).
41      * @param orientation the layout orientation, determining which point
42      * nodes will collapse/expand from. Valid values are
43      * {@link prefuse.Constants#ORIENT_CENTER},
44      * {@link prefuse.Constants#ORIENT_LEFT_RIGHT},
45      * {@link prefuse.Constants#ORIENT_RIGHT_LEFT},
46      * {@link prefuse.Constants#ORIENT_TOP_BOTTOM}, and
47      * {@link prefuse.Constants#ORIENT_BOTTOM_TOP}.
48      */

49     public CollapsedSubtreeLayout(String JavaDoc group, int orientation) {
50         super(group);
51         m_orientation = orientation;
52     }
53     
54     // ------------------------------------------------------------------------
55

56     /**
57      * Get the layout orientation, determining which point nodes will collapse
58      * or exapnd from. Valid values are
59      * {@link prefuse.Constants#ORIENT_CENTER},
60      * {@link prefuse.Constants#ORIENT_LEFT_RIGHT},
61      * {@link prefuse.Constants#ORIENT_RIGHT_LEFT},
62      * {@link prefuse.Constants#ORIENT_TOP_BOTTOM}, and
63      * {@link prefuse.Constants#ORIENT_BOTTOM_TOP}.
64      * @return the layout orientation
65      */

66     public int getOrientation() {
67         return m_orientation;
68     }
69     
70     /**
71      * Set the layout orientation, determining which point nodes will collapse
72      * or exapnd from. Valid values are
73      * {@link prefuse.Constants#ORIENT_CENTER},
74      * {@link prefuse.Constants#ORIENT_LEFT_RIGHT},
75      * {@link prefuse.Constants#ORIENT_RIGHT_LEFT},
76      * {@link prefuse.Constants#ORIENT_TOP_BOTTOM}, and
77      * {@link prefuse.Constants#ORIENT_BOTTOM_TOP}.
78      * @return the layout orientation to use
79      */

80     public void setOrientation(int orientation) {
81         if ( orientation < 0 || orientation >= Constants.ORIENTATION_COUNT )
82             throw new IllegalArgumentException JavaDoc(
83                 "Unrecognized orientation value: "+orientation);
84         m_orientation = orientation;
85     }
86     
87     // ------------------------------------------------------------------------
88

89     /**
90      * @see prefuse.action.Action#run(double)
91      */

92     public void run(double frac) {
93         // handle newly expanded subtrees - ensure they emerge from
94
// a visible ancestor node
95
Iterator JavaDoc items = m_vis.visibleItems(m_group);
96         while ( items.hasNext() ) {
97             VisualItem item = (VisualItem) items.next();
98             if ( item instanceof NodeItem && !item.isStartVisible() ) {
99                 NodeItem n = (NodeItem)item;
100                 Point2D JavaDoc p = getPoint(n, true);
101                 n.setStartX(p.getX());
102                 n.setStartY(p.getY());
103             }
104         }
105         
106         // handle newly collapsed nodes - ensure they collapse to
107
// the greatest visible ancestor node
108
items = m_vis.items(m_group, StartVisiblePredicate.TRUE);
109         while ( items.hasNext() ) {
110             VisualItem item = (VisualItem) items.next();
111             if ( item instanceof NodeItem && !item.isEndVisible() ) {
112                 NodeItem n = (NodeItem)item;
113                 Point2D JavaDoc p = getPoint(n, false);
114                 n.setStartX(n.getEndX());
115                 n.setStartY(n.getEndY());
116                 n.setEndX(p.getX());
117                 n.setEndY(p.getY());
118             }
119         }
120
121     }
122
123     private Point2D JavaDoc getPoint(NodeItem n, boolean start) {
124         // find the visible ancestor
125
NodeItem p = (NodeItem)n.getParent();
126         if ( start )
127             for (; p!=null && !p.isStartVisible(); p=(NodeItem)p.getParent());
128         else
129             for (; p!=null && !p.isEndVisible(); p=(NodeItem)p.getParent());
130         if ( p == null ) {
131             m_point.setLocation(n.getX(), n.getY());
132             return m_point;
133         }
134         
135         // get the vanishing/appearing point
136
double x = start ? p.getStartX() : p.getEndX();
137         double y = start ? p.getStartY() : p.getEndY();
138         Rectangle2D JavaDoc b = p.getBounds();
139         switch ( m_orientation ) {
140         case Constants.ORIENT_LEFT_RIGHT:
141             m_point.setLocation(x+b.getWidth(), y);
142             break;
143         case Constants.ORIENT_RIGHT_LEFT:
144             m_point.setLocation(x-b.getWidth(), y);
145             break;
146         case Constants.ORIENT_TOP_BOTTOM:
147             m_point.setLocation(x, y+b.getHeight());
148             break;
149         case Constants.ORIENT_BOTTOM_TOP:
150             m_point.setLocation(x, y-b.getHeight());
151             break;
152         case Constants.ORIENT_CENTER:
153             m_point.setLocation(x, y);
154             break;
155         }
156         return m_point;
157     }
158
159 } // end of class CollapsedSubtreeLayout
160
Popular Tags