1 package prefuse.demos; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Component ; 6 import java.awt.Dimension ; 7 import java.awt.Font ; 8 import java.awt.Shape ; 9 import java.awt.event.MouseEvent ; 10 import java.awt.geom.Rectangle2D ; 11 import java.util.Iterator ; 12 13 import javax.swing.BorderFactory ; 14 import javax.swing.Box ; 15 import javax.swing.JComponent ; 16 import javax.swing.JFrame ; 17 import javax.swing.JPanel ; 18 import javax.swing.SwingConstants ; 19 20 import prefuse.Display; 21 import prefuse.Visualization; 22 import prefuse.action.ActionList; 23 import prefuse.action.RepaintAction; 24 import prefuse.action.animate.ColorAnimator; 25 import prefuse.action.assignment.ColorAction; 26 import prefuse.action.layout.Layout; 27 import prefuse.action.layout.graph.SquarifiedTreeMapLayout; 28 import prefuse.controls.ControlAdapter; 29 import prefuse.data.Schema; 30 import prefuse.data.Tree; 31 import prefuse.data.expression.Predicate; 32 import prefuse.data.expression.parser.ExpressionParser; 33 import prefuse.data.io.TreeMLReader; 34 import prefuse.data.query.SearchQueryBinding; 35 import prefuse.render.AbstractShapeRenderer; 36 import prefuse.render.DefaultRendererFactory; 37 import prefuse.render.LabelRenderer; 38 import prefuse.util.ColorLib; 39 import prefuse.util.ColorMap; 40 import prefuse.util.FontLib; 41 import prefuse.util.PrefuseLib; 42 import prefuse.util.UpdateListener; 43 import prefuse.util.ui.JFastLabel; 44 import prefuse.util.ui.JSearchPanel; 45 import prefuse.util.ui.UILib; 46 import prefuse.visual.DecoratorItem; 47 import prefuse.visual.NodeItem; 48 import prefuse.visual.VisualItem; 49 import prefuse.visual.VisualTree; 50 import prefuse.visual.expression.InGroupPredicate; 51 import prefuse.visual.sort.TreeDepthItemSorter; 52 53 54 61 public class TreeMap extends Display { 62 63 public static final String TREE_CHI = "/chi-ontology.xml.gz"; 64 65 private static final Schema LABEL_SCHEMA = PrefuseLib.getVisualItemSchema(); 67 static { 68 LABEL_SCHEMA.setDefault(VisualItem.INTERACTIVE, false); 69 LABEL_SCHEMA.setDefault(VisualItem.TEXTCOLOR, ColorLib.gray(200)); 70 LABEL_SCHEMA.setDefault(VisualItem.FONT, FontLib.getFont("Tahoma",16)); 71 } 72 73 private static final String tree = "tree"; 74 private static final String treeNodes = "tree.nodes"; 75 private static final String treeEdges = "tree.edges"; 76 private static final String labels = "labels"; 77 78 private SearchQueryBinding searchQ; 79 80 public TreeMap(Tree t, String label) { 81 super(new Visualization()); 82 83 VisualTree vt = m_vis.addTree(tree, t); 85 m_vis.setVisible(treeEdges, null, false); 86 87 Predicate noLeaf = (Predicate)ExpressionParser.parse("childcount()>0"); 89 m_vis.setInteractive(treeNodes, noLeaf, false); 90 91 Predicate labelP = (Predicate)ExpressionParser.parse("treedepth()=1"); 94 m_vis.addDecorators(labels, treeNodes, labelP, LABEL_SCHEMA); 96 97 DefaultRendererFactory rf = new DefaultRendererFactory(); 99 rf.add(new InGroupPredicate(treeNodes), new NodeRenderer()); 100 rf.add(new InGroupPredicate(labels), new LabelRenderer(label)); 101 m_vis.setRendererFactory(rf); 102 103 final ColorAction borderColor = new BorderColorAction(treeNodes); 105 final ColorAction fillColor = new FillColorAction(treeNodes); 106 107 ActionList colors = new ActionList(); 109 colors.add(fillColor); 110 colors.add(borderColor); 111 m_vis.putAction("colors", colors); 112 113 ActionList animatePaint = new ActionList(400); 115 animatePaint.add(new ColorAnimator(treeNodes)); 116 animatePaint.add(new RepaintAction()); 117 m_vis.putAction("animatePaint", animatePaint); 118 119 ActionList layout = new ActionList(); 121 layout.add(new SquarifiedTreeMapLayout(tree)); 122 layout.add(new LabelLayout(labels)); 123 layout.add(colors); 124 layout.add(new RepaintAction()); 125 m_vis.putAction("layout", layout); 126 127 setSize(700,600); 129 setItemSorter(new TreeDepthItemSorter()); 130 addControlListener(new ControlAdapter() { 131 public void itemEntered(VisualItem item, MouseEvent e) { 132 item.setStrokeColor(borderColor.getColor(item)); 133 item.getVisualization().repaint(); 134 } 135 public void itemExited(VisualItem item, MouseEvent e) { 136 item.setStrokeColor(item.getEndStrokeColor()); 137 item.getVisualization().repaint(); 138 } 139 }); 140 141 searchQ = new SearchQueryBinding(vt.getNodeTable(), label); 142 m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, searchQ.getSearchSet()); 143 searchQ.getPredicate().addExpressionListener(new UpdateListener() { 144 public void update(Object src) { 145 m_vis.cancel("animatePaint"); 146 m_vis.run("colors"); 147 m_vis.run("animatePaint"); 148 } 149 }); 150 151 m_vis.run("layout"); 153 } 154 155 public SearchQueryBinding getSearchQuery() { 156 return searchQ; 157 } 158 159 public static void main(String argv[]) { 160 UILib.setPlatformLookAndFeel(); 161 162 163 String infile = TREE_CHI; 164 String label = "name"; 165 if ( argv.length > 1 ) { 166 infile = argv[0]; 167 label = argv[1]; 168 } 169 JComponent treemap = demo(infile, label); 170 171 JFrame frame = new JFrame ("p r e f u s e | t r e e m a p"); 172 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 173 frame.setContentPane(treemap); 174 frame.pack(); 175 frame.setVisible(true); 176 } 177 178 public static JComponent demo() { 179 return demo(TREE_CHI, "name"); 180 } 181 182 public static JComponent demo(String datafile, final String label) { 183 Tree t = null; 184 try { 185 t = (Tree)new TreeMLReader().readGraph(datafile); 186 } catch ( Exception e ) { 187 e.printStackTrace(); 188 System.exit(1); 189 } 190 191 final TreeMap treemap = new TreeMap(t, label); 193 194 JSearchPanel search = treemap.getSearchQuery().createSearchPanel(); 196 search.setShowResultCount(true); 197 search.setBorder(BorderFactory.createEmptyBorder(5,5,4,0)); 198 search.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 11)); 199 200 final JFastLabel title = new JFastLabel(" "); 201 title.setPreferredSize(new Dimension (350, 20)); 202 title.setVerticalAlignment(SwingConstants.BOTTOM); 203 title.setBorder(BorderFactory.createEmptyBorder(3,0,0,0)); 204 title.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 16)); 205 206 treemap.addControlListener(new ControlAdapter() { 207 public void itemEntered(VisualItem item, MouseEvent e) { 208 title.setText(item.getString(label)); 209 } 210 public void itemExited(VisualItem item, MouseEvent e) { 211 title.setText(null); 212 } 213 }); 214 215 Box box = UILib.getBox(new Component []{title,search}, true, 10, 3, 0); 216 217 JPanel panel = new JPanel (new BorderLayout ()); 218 panel.add(treemap, BorderLayout.CENTER); 219 panel.add(box, BorderLayout.SOUTH); 220 UILib.setColor(panel, Color.BLACK, Color.GRAY); 221 return panel; 222 } 223 224 226 231 public static class BorderColorAction extends ColorAction { 232 233 public BorderColorAction(String group) { 234 super(group, VisualItem.STROKECOLOR); 235 } 236 237 public int getColor(VisualItem item) { 238 NodeItem nitem = (NodeItem)item; 239 if ( nitem.isHover() ) 240 return ColorLib.rgb(99,130,191); 241 242 int depth = nitem.getDepth(); 243 if ( depth < 2 ) { 244 return ColorLib.gray(100); 245 } else if ( depth < 4 ) { 246 return ColorLib.gray(75); 247 } else { 248 return ColorLib.gray(50); 249 } 250 } 251 } 252 253 258 public static class FillColorAction extends ColorAction { 259 private ColorMap cmap = new ColorMap( 260 ColorLib.getInterpolatedPalette(10, 261 ColorLib.rgb(85,85,85), ColorLib.rgb(0,0,0)), 0, 9); 262 263 public FillColorAction(String group) { 264 super(group, VisualItem.FILLCOLOR); 265 } 266 267 public int getColor(VisualItem item) { 268 if ( item instanceof NodeItem ) { 269 NodeItem nitem = (NodeItem)item; 270 if ( nitem.getChildCount() > 0 ) { 271 return 0; } else { 273 if ( m_vis.isInGroup(item, Visualization.SEARCH_ITEMS) ) 274 return ColorLib.rgb(191,99,130); 275 else 276 return cmap.getColor(nitem.getDepth()); 277 } 278 } else { 279 return cmap.getColor(0); 280 } 281 } 282 283 } 285 291 public static class LabelLayout extends Layout { 292 public LabelLayout(String group) { 293 super(group); 294 } 295 public void run(double frac) { 296 Iterator iter = m_vis.items(m_group); 297 while ( iter.hasNext() ) { 298 DecoratorItem item = (DecoratorItem)iter.next(); 299 VisualItem node = item.getDecoratedItem(); 300 Rectangle2D bounds = node.getBounds(); 301 setX(item, null, bounds.getCenterX()); 302 setY(item, null, bounds.getCenterY()); 303 } 304 } 305 } 307 311 public static class NodeRenderer extends AbstractShapeRenderer { 312 private Rectangle2D m_bounds = new Rectangle2D.Double (); 313 314 public NodeRenderer() { 315 m_manageBounds = false; 316 } 317 protected Shape getRawShape(VisualItem item) { 318 m_bounds.setRect(item.getBounds()); 319 return m_bounds; 320 } 321 } 323 } | Popular Tags |