1 package prefuse.action.layout; 2 3 import java.awt.geom.Point2D ; 4 import java.awt.geom.Rectangle2D ; 5 import java.util.Iterator ; 6 7 import prefuse.Constants; 8 import prefuse.visual.NodeItem; 9 import prefuse.visual.VisualItem; 10 import prefuse.visual.expression.StartVisiblePredicate; 11 12 20 public class CollapsedSubtreeLayout extends Layout { 21 22 private int m_orientation; 23 private Point2D m_point = new Point2D.Double (); 24 25 32 public CollapsedSubtreeLayout(String group) { 33 this(group, Constants.ORIENT_CENTER); 34 } 35 36 49 public CollapsedSubtreeLayout(String group, int orientation) { 50 super(group); 51 m_orientation = orientation; 52 } 53 54 56 66 public int getOrientation() { 67 return m_orientation; 68 } 69 70 80 public void setOrientation(int orientation) { 81 if ( orientation < 0 || orientation >= Constants.ORIENTATION_COUNT ) 82 throw new IllegalArgumentException ( 83 "Unrecognized orientation value: "+orientation); 84 m_orientation = orientation; 85 } 86 87 89 92 public void run(double frac) { 93 Iterator 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 p = getPoint(n, true); 101 n.setStartX(p.getX()); 102 n.setStartY(p.getY()); 103 } 104 } 105 106 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 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 getPoint(NodeItem n, boolean start) { 124 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 double x = start ? p.getStartX() : p.getEndX(); 137 double y = start ? p.getStartY() : p.getEndY(); 138 Rectangle2D 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 } | Popular Tags |