KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.action.layout;
2
3 import java.awt.geom.Rectangle2D JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import prefuse.data.Node;
7 import prefuse.data.tuple.TupleSet;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Implements a uniform grid-based layout. This component can either use
13  * preset grid dimensions or analyze a grid-shaped graph to determine them
14  * automatically.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class GridLayout extends Layout {
19
20     protected int rows;
21     protected int cols;
22     protected boolean analyze = false;
23     
24     /**
25      * Create a new GridLayout without preset dimensions. The layout will
26      * attempt to analyze an input graph to determine grid parameters.
27      * @param group the data group to layout. In this automatic grid
28      * analysis configuration, the group <b>must</b> resolve to a set of
29      * graph nodes.
30      */

31     public GridLayout(String JavaDoc group) {
32         super(group);
33         analyze = true;
34     }
35     
36     /**
37      * Create a new GridLayout using the specified grid dimensions. If the
38      * input data has more elements than the grid dimensions can hold, the
39      * left over elements will not be visible.
40      * @param group the data group to layout
41      * @param nrows the number of rows of the grid
42      * @param ncols the number of columns of the grid
43      */

44     public GridLayout(String JavaDoc group, int nrows, int ncols) {
45         super(group);
46         rows = nrows;
47         cols = ncols;
48         analyze = false;
49     }
50     
51     /**
52      * @see prefuse.action.Action#run(double)
53      */

54     public void run(double frac) {
55         Rectangle2D JavaDoc b = getLayoutBounds();
56         double bx = b.getMinX(), by = b.getMinY();
57         double w = b.getWidth(), h = b.getHeight();
58         
59         TupleSet ts = m_vis.getGroup(m_group);
60         int m = rows, n = cols;
61         if ( analyze ) {
62             int[] d = analyzeGraphGrid(ts);
63             m = d[0]; n = d[1];
64         }
65         
66         Iterator JavaDoc iter = ts.tuples();
67         // layout grid contents
68
for ( int i=0; iter.hasNext() && i < m*n; ++i ) {
69             VisualItem item = (VisualItem)iter.next();
70             item.setVisible(true);
71             double x = bx + w*((i%n)/(double)(n-1));
72             double y = by + h*((i/n)/(double)(m-1));
73             setX(item,null,x);
74             setY(item,null,y);
75         }
76         // set left-overs invisible
77
while ( iter.hasNext() ) {
78             VisualItem item = (VisualItem)iter.next();
79             item.setVisible(false);
80         }
81     }
82     
83     /**
84      * Analyzes a set of nodes to try and determine grid dimensions. Currently
85      * looks for the edge count on a node to drop to 2 to determine the end of
86      * a row.
87      * @param ts TupleSet ts a set of nodes to analyze. Contained tuples
88      * <b>must</b> implement be Node instances.
89      * @return a two-element int array with the row and column lengths
90      */

91     public static int[] analyzeGraphGrid(TupleSet ts) {
92         // TODO: more robust grid analysis?
93
int m, n;
94         Iterator JavaDoc iter = ts.tuples(); iter.next();
95         for ( n=2; iter.hasNext(); n++ ) {
96             Node nd = (Node)iter.next();
97             if ( nd.getDegree() == 2 )
98                 break;
99         }
100         m = ts.getTupleCount() / n;
101         return new int[] {m,n};
102     }
103     
104     /**
105      * Get the number of grid columns.
106      * @return the number of grid columns
107      */

108     public int getNumCols() {
109         return cols;
110     }
111     
112     /**
113      * Set the number of grid columns.
114      * @param cols the number of grid columns to use
115      */

116     public void setNumCols(int cols) {
117         this.cols = cols;
118     }
119     
120     /**
121      * Get the number of grid rows.
122      * @return the number of grid rows
123      */

124     public int getNumRows() {
125         return rows;
126     }
127     
128     /**
129      * Set the number of grid rows.
130      * @param rows the number of grid rows to use
131      */

132     public void setNumRows(int rows) {
133         this.rows = rows;
134     }
135     
136 } // end of class GridLayout
137
Popular Tags