1 package prefuse.demos; 2 3 import java.awt.Cursor ; 4 import java.awt.event.MouseEvent ; 5 import java.awt.geom.Point2D ; 6 import java.util.Iterator ; 7 8 import javax.swing.BorderFactory ; 9 import javax.swing.JComponent ; 10 import javax.swing.JFrame ; 11 import javax.swing.SwingUtilities ; 12 13 import prefuse.Constants; 14 import prefuse.Display; 15 import prefuse.Visualization; 16 import prefuse.action.ActionList; 17 import prefuse.action.RepaintAction; 18 import prefuse.action.assignment.ColorAction; 19 import prefuse.action.assignment.SizeAction; 20 import prefuse.action.layout.RandomLayout; 21 import prefuse.action.layout.graph.ForceDirectedLayout; 22 import prefuse.activity.Activity; 23 import prefuse.activity.ActivityAdapter; 24 import prefuse.activity.ActivityListener; 25 import prefuse.controls.ControlAdapter; 26 import prefuse.data.Schema; 27 import prefuse.data.Table; 28 import prefuse.data.io.DelimitedTextTableReader; 29 import prefuse.data.tuple.TupleSet; 30 import prefuse.render.DefaultRendererFactory; 31 import prefuse.render.LabelRenderer; 32 import prefuse.util.ColorLib; 33 import prefuse.util.PrefuseLib; 34 import prefuse.util.force.DragForce; 35 import prefuse.util.force.ForceItem; 36 import prefuse.util.force.ForceSimulator; 37 import prefuse.util.force.NBodyForce; 38 import prefuse.util.force.SpringForce; 39 import prefuse.util.ui.BrowserLauncher; 40 import prefuse.visual.VisualItem; 41 import prefuse.visual.sort.ItemSorter; 42 43 46 public class DataMountain extends Display { 47 48 public DataMountain(Table t) { 49 super(new Visualization()); 50 m_vis.addTable("data", t); 51 52 LabelRenderer nodeRenderer = new LabelRenderer(null, "image"); 53 nodeRenderer.setTextField(null); 54 nodeRenderer.setVerticalAlignment(Constants.BOTTOM); 55 nodeRenderer.setHorizontalPadding(0); 56 nodeRenderer.setVerticalPadding(0); 57 nodeRenderer.setMaxImageDimensions(100,100); 58 59 m_vis.setRendererFactory(new DefaultRendererFactory(nodeRenderer)); 60 61 ActionList init = new ActionList(); 62 init.add(new RandomLayout()); 63 init.add(new DataMountainSizeAction()); 64 m_vis.putAction("init", init); 65 66 ActionList update = new ActionList(); 67 update.add(new DataMountainSizeAction()); 68 update.add(new ColorAction("data", VisualItem.STROKECOLOR) { 69 public int getColor(VisualItem item) { 70 return ColorLib.rgb((item.isHover() ? 255 : 0), 0, 0); 71 } 72 }); 73 update.add(new RepaintAction()); 74 m_vis.putAction("update", update); 75 76 ActionList preforce = new ActionList(1000); 78 preforce.add(new DataMountainForceLayout(true)); 79 m_vis.putAction("preforce", preforce); 80 81 final ForceDirectedLayout fl = new DataMountainForceLayout(false); 83 ActivityListener fReset = new ActivityAdapter() { 84 public void activityCancelled(Activity a) { 85 fl.reset(); 86 } 87 }; 88 ActionList forces = new ActionList(Activity.INFINITY); 89 forces.add(fl); 90 forces.add(update); 91 forces.addActivityListener(fReset); 92 m_vis.putAction("forces", forces); 93 94 setSize(640,450); 95 setDamageRedraw(false); setBorder(BorderFactory.createEmptyBorder(30,20,5,20)); 97 setItemSorter(new DataMountainSorter()); 98 addControlListener(new DataMountainControl()); 99 100 nodeRenderer.getImageFactory().preloadImages(m_vis.items(),"image"); 102 103 m_vis.run("init"); 105 m_vis.runAfter("preforce", "update"); 106 m_vis.run("preforce"); 107 } 108 109 111 public static void main(String [] args) { 112 JFrame frame = new JFrame ("p r e f u s e | d a t a m o u n t a i n"); 113 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 114 frame.getContentPane().add(demo()); 115 frame.pack(); 116 frame.setVisible(true); 117 } 118 119 public static JComponent demo() { 120 String datafile = "/amazon.txt"; 121 Table data = null; 122 try { 123 data = (new DelimitedTextTableReader()).readTable(datafile); 124 data.addColumn("image","CONCAT('/images/',id,'.01.MZZZZZZZ.jpg')"); 125 } catch (Exception e) { 126 e.printStackTrace(); 127 System.exit(1); 128 } 129 return new DataMountain(data); 130 } 131 132 134 public static class DataMountainSorter extends ItemSorter { 135 public int score(VisualItem item) { 136 return (int)(10000*item.getY()); 137 } 138 } 140 public class DataMountainSizeAction extends SizeAction { 141 public double getSize(VisualItem item) { 142 double y = item.getEndY(); 143 return 0.2 + y/1400; 144 } 145 } 147 private static final String ANCHORITEM = "_anchorItem"; 148 private static final Schema ANCHORITEM_SCHEMA = new Schema(); 149 static { 150 ANCHORITEM_SCHEMA.addColumn(ANCHORITEM, ForceItem.class); 151 } 152 153 public class DataMountainForceLayout extends ForceDirectedLayout { 154 155 public DataMountainForceLayout(boolean enforceBounds) { 156 super("data",enforceBounds,false); 157 158 ForceSimulator fsim = new ForceSimulator(); 159 fsim.addForce(new NBodyForce(-0.4f, 25f, NBodyForce.DEFAULT_THETA)); 160 fsim.addForce(new SpringForce(1e-5f,0f)); 161 fsim.addForce(new DragForce()); 162 setForceSimulator(fsim); 163 164 m_nodeGroup = "data"; 165 m_edgeGroup = null; 166 } 167 168 protected float getMassValue(VisualItem n) { 169 return n.isHover() ? 5f : 1f; 170 } 171 172 public void reset() { 173 Iterator iter = m_vis.visibleItems(m_nodeGroup); 174 while ( iter.hasNext() ) { 175 VisualItem item = (VisualItem)iter.next(); 176 ForceItem aitem = (ForceItem)item.get(ANCHORITEM); 177 if ( aitem != null ) { 178 aitem.location[0] = (float)item.getEndX(); 179 aitem.location[1] = (float)item.getEndY(); 180 } 181 } 182 super.reset(); 183 } 184 protected void initSimulator(ForceSimulator fsim) { 185 TupleSet t = (TupleSet)m_vis.getGroup(m_group); 187 t.addColumns(ANCHORITEM_SCHEMA); 188 t.addColumns(FORCEITEM_SCHEMA); 189 190 Iterator iter = m_vis.visibleItems(m_nodeGroup); 191 while ( iter.hasNext() ) { 192 VisualItem item = (VisualItem)iter.next(); 193 ForceItem fitem = (ForceItem)item.get(FORCEITEM); 195 if ( fitem == null ) { 196 fitem = new ForceItem(); 197 item.set(FORCEITEM, fitem); 198 } 199 fitem.location[0] = (float)item.getEndX(); 200 fitem.location[1] = (float)item.getEndY(); 201 fitem.mass = getMassValue(item); 202 203 ForceItem aitem = (ForceItem)item.get(ANCHORITEM); 205 if ( aitem == null ) { 206 aitem = new ForceItem(); 207 item.set(ANCHORITEM, aitem); 208 aitem.location[0] = fitem.location[0]; 209 aitem.location[1] = fitem.location[1]; 210 } 211 212 fsim.addItem(fitem); 213 fsim.addSpring(fitem, aitem, 0); 214 } 215 } 216 } 218 public class DataMountainControl extends ControlAdapter { 219 public static final String URL = "http://www.amazon.com/exec/obidos/tg/detail/-/"; 220 private VisualItem activeItem; 221 private Point2D down = new Point2D.Double (); 222 private Point2D tmp = new Point2D.Double (); 223 private boolean wasFixed, dragged; 224 private boolean repaint = false; 225 226 public void itemEntered(VisualItem item, MouseEvent e) { 227 Display d = (Display)e.getSource(); 228 d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 229 d.setToolTipText(item.getString("id")); 230 activeItem = item; 231 wasFixed = item.isFixed(); 232 } 233 234 public void itemExited(VisualItem item, MouseEvent e) { 235 if ( activeItem == item ) { 236 activeItem = null; 237 item.setFixed(wasFixed); 238 } 239 Display d = (Display)e.getSource(); 240 d.setToolTipText(null); 241 d.setCursor(Cursor.getDefaultCursor()); 242 } 243 244 public void itemPressed(VisualItem item, MouseEvent e) { 245 if (!SwingUtilities.isLeftMouseButton(e)) return; 246 247 Visualization vis = item.getVisualization(); 249 vis.getFocusGroup(Visualization.FOCUS_ITEMS).setTuple(item); 250 251 item.setFixed(true); 252 dragged = false; 253 Display d = (Display)e.getComponent(); 254 down = d.getAbsoluteCoordinate(e.getPoint(), down); 255 256 vis.run("forces"); 257 } 258 259 public void itemReleased(VisualItem item, MouseEvent e) { 260 if (!SwingUtilities.isLeftMouseButton(e)) return; 261 if ( dragged ) { 262 activeItem = null; 263 item.setFixed(wasFixed); 264 dragged = false; 265 } 266 Visualization vis = item.getVisualization(); 268 vis.getFocusGroup(Visualization.FOCUS_ITEMS).clear(); 269 270 vis.cancel("forces"); 271 } 272 273 public void itemClicked(VisualItem item, MouseEvent e) { 274 if (!SwingUtilities.isLeftMouseButton(e)) return; 275 if ( e.getClickCount() == 2 ) { 276 String id = item.getString("id"); 277 BrowserLauncher.showDocument(URL+id); 278 } 279 } 280 281 public void itemDragged(VisualItem item, MouseEvent e) { 282 if (!SwingUtilities.isLeftMouseButton(e)) return; 283 dragged = true; 284 Display d = (Display)e.getComponent(); 285 tmp = d.getAbsoluteCoordinate(e.getPoint(), tmp); 286 double dx = tmp.getX()-down.getX(); 287 double dy = tmp.getY()-down.getY(); 288 289 PrefuseLib.setX(item, null, item.getX()+dx); 290 PrefuseLib.setY(item, null, item.getY()+dy); 291 down.setLocation(tmp); 292 if ( repaint ) 293 item.getVisualization().repaint(); 294 } 295 } 297 } 299 | Popular Tags |