KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > demos > DataMountain


1 package prefuse.demos;
2
3 import java.awt.Cursor JavaDoc;
4 import java.awt.event.MouseEvent JavaDoc;
5 import java.awt.geom.Point2D JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import javax.swing.BorderFactory JavaDoc;
9 import javax.swing.JComponent JavaDoc;
10 import javax.swing.JFrame JavaDoc;
11 import javax.swing.SwingUtilities JavaDoc;
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 /**
44  * @author <a HREF="http://jheer.org">jeffrey heer</a>
45  */

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         // we run this to make sure the forces are stabilized
77
ActionList preforce = new ActionList(1000);
78         preforce.add(new DataMountainForceLayout(true));
79         m_vis.putAction("preforce", preforce);
80
81         // this will cause docs to move out of the way when dragging
82
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); // disable due to Java2D image clipping errors
96
setBorder(BorderFactory.createEmptyBorder(30,20,5,20));
97         setItemSorter(new DataMountainSorter());
98         addControlListener(new DataMountainControl());
99         
100         // pre-load images, otherwise they will be loaded asynchronously
101
nodeRenderer.getImageFactory().preloadImages(m_vis.items(),"image");
102         
103         // initialize and present the interface
104
m_vis.run("init");
105         m_vis.runAfter("preforce", "update");
106         m_vis.run("preforce");
107     }
108
109     // ------------------------------------------------------------------------
110

111     public static void main(String JavaDoc[] args) {
112         JFrame JavaDoc frame = new JFrame JavaDoc("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 JavaDoc demo() {
120         String JavaDoc 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 JavaDoc e) {
126             e.printStackTrace();
127             System.exit(1);
128         }
129         return new DataMountain(data);
130     }
131     
132     // ------------------------------------------------------------------------
133

134     public static class DataMountainSorter extends ItemSorter {
135         public int score(VisualItem item) {
136             return (int)(10000*item.getY());
137         }
138     } // end of inner class DataMountainComparator
139

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     } // end of inner class DataMountainSizeAction
146

147     private static final String JavaDoc 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 JavaDoc 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             // make sure we have force items to work with
186
TupleSet t = (TupleSet)m_vis.getGroup(m_group);
187             t.addColumns(ANCHORITEM_SCHEMA);
188             t.addColumns(FORCEITEM_SCHEMA);
189             
190             Iterator JavaDoc iter = m_vis.visibleItems(m_nodeGroup);
191             while ( iter.hasNext() ) {
192                 VisualItem item = (VisualItem)iter.next();
193                 // get force item
194
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                 // get spring anchor
204
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     } // end of inner class DataMountainForceLayout
217

218     public class DataMountainControl extends ControlAdapter {
219         public static final String JavaDoc URL = "http://www.amazon.com/exec/obidos/tg/detail/-/";
220         private VisualItem activeItem;
221         private Point2D JavaDoc down = new Point2D.Double JavaDoc();
222         private Point2D JavaDoc tmp = new Point2D.Double JavaDoc();
223         private boolean wasFixed, dragged;
224         private boolean repaint = false;
225         
226         public void itemEntered(VisualItem item, MouseEvent JavaDoc 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 JavaDoc 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 JavaDoc e) {
245             if (!SwingUtilities.isLeftMouseButton(e)) return;
246             
247             // set the focus to the current node
248
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 JavaDoc e) {
260             if (!SwingUtilities.isLeftMouseButton(e)) return;
261             if ( dragged ) {
262                 activeItem = null;
263                 item.setFixed(wasFixed);
264                 dragged = false;
265             }
266             // clear the focus
267
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 JavaDoc e) {
274             if (!SwingUtilities.isLeftMouseButton(e)) return;
275             if ( e.getClickCount() == 2 ) {
276                 String JavaDoc id = item.getString("id");
277                 BrowserLauncher.showDocument(URL+id);
278             }
279         }
280         
281         public void itemDragged(VisualItem item, MouseEvent JavaDoc 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     } // end of inner class DataMountainControl
296

297 } // end of class DataMountain
298

299
Popular Tags