KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.action.layout;
2
3 import java.awt.geom.Rectangle2D JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import prefuse.Constants;
7 import prefuse.data.Table;
8 import prefuse.render.PolygonRenderer;
9 import prefuse.visual.VisualItem;
10
11 /**
12  * Layout Action that updates the outlines of polygons in a stacked line chart,
13  * properly setting the coordinates of "collapsed" stacks.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class CollapsedStackLayout extends Layout {
18
19     private String JavaDoc m_polyField;
20     private int m_orientation = Constants.ORIENT_BOTTOM_TOP;
21     private boolean m_horiz = false;
22     private boolean m_top = false;
23     
24     /**
25      * Create a new CollapsedStackLayout. The polygon field is assumed to be
26      * {@link prefuse.render.PolygonRenderer#POLYGON}.
27      * @param group the data group to layout
28      */

29     public CollapsedStackLayout(String JavaDoc group) {
30         this(group, PolygonRenderer.POLYGON);
31     }
32     
33     /**
34      * Create a new CollapsedStackLayout.
35      * @param group the data group to layout
36      * @param field the data field from which to lookup the polygons
37      */

38     public CollapsedStackLayout(String JavaDoc group, String JavaDoc field) {
39         super(group);
40         m_polyField = field;
41     }
42     
43     /**
44      * Returns the orientation of this layout. One of
45      * {@link Constants#ORIENT_BOTTOM_TOP} (to grow bottom-up),
46      * {@link Constants#ORIENT_TOP_BOTTOM} (to grow top-down),
47      * {@link Constants#ORIENT_LEFT_RIGHT} (to grow left-right), or
48      * {@link Constants#ORIENT_RIGHT_LEFT} (to grow right-left).
49      * @return the orientation of this layout
50      */

51     public int getOrientation() {
52         return m_orientation;
53     }
54     
55     /**
56      * Sets the orientation of this layout. Must be one of
57      * {@link Constants#ORIENT_BOTTOM_TOP} (to grow bottom-up),
58      * {@link Constants#ORIENT_TOP_BOTTOM} (to grow top-down),
59      * {@link Constants#ORIENT_LEFT_RIGHT} (to grow left-right), or
60      * {@link Constants#ORIENT_RIGHT_LEFT} (to grow right-left).
61      * @param orient the desired orientation of this layout
62      * @throws IllegalArgumentException if the orientation value
63      * is not a valid value
64      */

65     public void setOrientation(int orient) {
66         if ( orient != Constants.ORIENT_TOP_BOTTOM &&
67              orient != Constants.ORIENT_BOTTOM_TOP &&
68              orient != Constants.ORIENT_LEFT_RIGHT &&
69              orient != Constants.ORIENT_RIGHT_LEFT) {
70             throw new IllegalArgumentException JavaDoc(
71                     "Invalid orientation value: "+orient);
72         }
73         m_orientation = orient;
74         m_horiz = (m_orientation == Constants.ORIENT_LEFT_RIGHT ||
75                    m_orientation == Constants.ORIENT_RIGHT_LEFT);
76         m_top = (m_orientation == Constants.ORIENT_TOP_BOTTOM ||
77                    m_orientation == Constants.ORIENT_LEFT_RIGHT);
78     }
79     
80     /**
81      * @see prefuse.action.Action#run(double)
82      */

83     public void run(double frac) {
84         VisualItem lastItem = null;
85         
86         Rectangle2D JavaDoc bounds = getLayoutBounds();
87         float floor = (float)
88             (m_horiz ? (m_top?bounds.getMaxX():bounds.getMinX())
89                      : (m_top?bounds.getMinY():bounds.getMaxY()));
90         int bias = (m_horiz ? 0 : 1);
91         
92         // TODO: generalize this -- we want tuplesReversed available for general sets
93
Iterator JavaDoc iter = ((Table)m_vis.getGroup(m_group)).tuplesReversed();
94         while ( iter.hasNext() ) {
95             VisualItem item = (VisualItem)iter.next();
96             boolean prev = item.isStartVisible();
97             boolean cur = item.isVisible();
98             
99             if ( !prev && cur ) {
100                 // newly visible, update contour
101
float[] f = (float[])item.get(m_polyField);
102                 if ( f == null ) continue;
103                 
104                 if ( lastItem == null ) {
105                     // no previous items, smash values to the floor
106
for ( int i=0; i<f.length; i+=2 )
107                         f[i+bias] = floor;
108                 } else {
109                     // previous visible item, smash values to the
110
// visible item's contour
111
float[] l = (float[])lastItem.get(m_polyField);
112                     for ( int i=0; i<f.length/2; i+=2 )
113                         f[i+bias] = f[f.length-2-i+bias]
114                                   = l[i+bias];
115                 }
116             } else if ( prev && cur ) {
117                 // this item was previously visible, remember it
118
lastItem = item;
119             }
120         }
121     }
122     
123 } // end of class CollapsedStackAction
124
Popular Tags