1 package prefuse.action.layout; 2 3 import java.awt.geom.Rectangle2D ; 4 import java.util.Iterator ; 5 6 import prefuse.data.Node; 7 import prefuse.data.tuple.TupleSet; 8 import prefuse.visual.VisualItem; 9 10 11 18 public class GridLayout extends Layout { 19 20 protected int rows; 21 protected int cols; 22 protected boolean analyze = false; 23 24 31 public GridLayout(String group) { 32 super(group); 33 analyze = true; 34 } 35 36 44 public GridLayout(String group, int nrows, int ncols) { 45 super(group); 46 rows = nrows; 47 cols = ncols; 48 analyze = false; 49 } 50 51 54 public void run(double frac) { 55 Rectangle2D 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 iter = ts.tuples(); 67 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 while ( iter.hasNext() ) { 78 VisualItem item = (VisualItem)iter.next(); 79 item.setVisible(false); 80 } 81 } 82 83 91 public static int[] analyzeGraphGrid(TupleSet ts) { 92 int m, n; 94 Iterator 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 108 public int getNumCols() { 109 return cols; 110 } 111 112 116 public void setNumCols(int cols) { 117 this.cols = cols; 118 } 119 120 124 public int getNumRows() { 125 return rows; 126 } 127 128 132 public void setNumRows(int rows) { 133 this.rows = rows; 134 } 135 136 } | Popular Tags |