KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > PrefuseLib


1 package prefuse.util;
2
3 import java.awt.BasicStroke JavaDoc;
4 import java.awt.Font JavaDoc;
5 import java.awt.Stroke JavaDoc;
6 import java.awt.geom.Rectangle2D JavaDoc;
7
8 import prefuse.Constants;
9 import prefuse.Display;
10 import prefuse.data.Schema;
11 import prefuse.visual.VisualItem;
12
13 /**
14  * General library routines used by the prefuse toolkit.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class PrefuseLib {
19
20     /**
21      * Group delimiter string used to demarcate the hierarchical structure
22      * of nested data groups; the default value is a dot (.).
23      */

24     private static final String JavaDoc GROUP_DELIMITER
25         = PrefuseConfig.get("data.delimiter");
26     /**
27      * Scale factor for psychophysical scaling of area values, defaults to
28      * 0.5, resulting in linear scaling.
29      */

30     private static final double SIZE_SCALE_FACTOR
31         = PrefuseConfig.getDouble("size.scale2D");
32     /**
33      * Prefix for visualization-specific data fields, such as those used by
34      * VisualItem; the default is a single underscore (_).
35      */

36     public static final String JavaDoc FIELD_PREFIX
37         = PrefuseConfig.get("data.visual.fieldPrefix");
38     
39     // ------------------------------------------------------------------------
40

41     private PrefuseLib() {
42         // prevent instantiation
43
}
44     
45     // ------------------------------------------------------------------------
46
// Memory Usage / Debugging Output
47

48     /**
49      * Get a String showing current JVM memory usage in kilobytes.
50      * @return the memory usage String in KB
51      */

52     public static String JavaDoc getMemoryUsageInKB() {
53         long total = Runtime.getRuntime().totalMemory() / (2<<10);
54         long free = Runtime.getRuntime().freeMemory() / (2<<10);
55         long max = Runtime.getRuntime().maxMemory() / (2<<10);
56         return "Memory: "+(total-free)+"k / "+total+"k / "+max+"k";
57     }
58
59     /**
60      * Get a String showing current JVM memory usage in megabytes.
61      * @return the memory usage String in MB
62      */

63     public static String JavaDoc getMemoryUsageInMB() {
64         long total = Runtime.getRuntime().totalMemory() / (2<<20);
65         long free = Runtime.getRuntime().freeMemory() / (2<<20);
66         long max = Runtime.getRuntime().maxMemory() / (2<<20);
67         return "Memory: "+(total-free)+"M / "+total+"M / "+max+"M";
68     }
69     
70     /**
71      * Returns a string showing debugging info such as number of visualized
72      * items and the current frame rate.
73      * @return the debug string
74      */

75     public static String JavaDoc getDisplayStats(Display d) {
76         float fr = Math.round(d.getFrameRate()*100f)/100f;
77         
78         Runtime JavaDoc rt = Runtime.getRuntime();
79         long tm = rt.totalMemory() / (2<<20);
80         long fm = rt.freeMemory() / (2<<20);
81         long mm = rt.maxMemory() / (2<<20);
82         
83         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
84         sb.append("frame rate: ").append(fr).append("fps - ");
85         sb.append(d.getVisibleItemCount()).append(" items - ");
86         sb.append("fonts(").append(FontLib.getCacheMissCount());
87         sb.append(") colors(");
88         sb.append(ColorLib.getCacheMissCount()).append(')');
89         sb.append(" mem(");
90         sb.append(tm-fm).append("M / ");
91         sb.append(mm).append("M)");
92         sb.append(" (x:");
93         sb.append(StringLib.formatNumber(d.getDisplayX(),2));
94         sb.append(", y:");
95         sb.append(StringLib.formatNumber(d.getDisplayY(),2));
96         sb.append(", z:");
97         sb.append(StringLib.formatNumber(d.getScale(),5)).append(")");
98         return sb.toString();
99     }
100     
101     // ------------------------------------------------------------------------
102
// VisualItem Methods
103

104     /**
105      * Returns a scale factor by which to scale a 2D shape to grow
106      * the area by the desired input size value. This is used to scale shapes
107      * by total pixel area, rather than scaling each dimension by the
108      * size value itself, which grows the pixel area quadratically rather
109      * than linearly.
110      */

111     public static double getSize2D(double size) {
112         return Math.pow(size, SIZE_SCALE_FACTOR);
113     }
114     
115     /**
116      * Get the distance between the x,y points of two VisualItems.
117      * @param vi1 the first VisualItem
118      * @param vi2 the second VisualItem
119      * @return the distance between the items' x,y coordinates
120      */

121     public static double distance(VisualItem vi1, VisualItem vi2) {
122         double dx = vi1.getX() - vi2.getX();
123         double dy = vi1.getY() - vi2.getY();
124         return Math.sqrt(dx*dx + dy*dy);
125     }
126     
127     /**
128      * Update the values in an interpolated column (a set of three columns
129      * representing a current value along with starting and ending values).
130      * The current value will become the new starting value, while the given
131      * value will become the new current and ending values.
132      * @param item the VisualItem to update
133      * @param field the base field to update, start and ending fields will
134      * also be updated, too.
135      * @param val the value to set
136      */

137     public static void update(VisualItem item, String JavaDoc field, Object JavaDoc val) {
138         item.set(getStartField(field), item.get(field));
139         item.set(field, val);
140         item.set(getEndField(field), val);
141     }
142     
143     /**
144      * Update the values in an interpolated column (a set of three columns
145      * representing a current value along with starting and ending values).
146      * The current value will become the new starting value, while the given
147      * value will become the new current and ending values.
148      * @param item the VisualItem to update
149      * @param field the base field to update, start and ending fields will
150      * also be updated, too.
151      * @param val the value to set
152      */

153     public static void updateInt(VisualItem item, String JavaDoc field, int val) {
154         item.setInt(getStartField(field), item.getInt(field));
155         item.setInt(field, val);
156         item.setInt(getEndField(field), val);
157     }
158     
159     /**
160      * Update the values in an interpolated column (a set of three columns
161      * representing a current value along with starting and ending values).
162      * The current value will become the new starting value, while the given
163      * value will become the new current and ending values.
164      * @param item the VisualItem to update
165      * @param field the base field to update, start and ending fields will
166      * also be updated, too.
167      * @param val the value to set
168      */

169     public static void updateLong(VisualItem item, String JavaDoc field, long val) {
170         item.setLong(getStartField(field), item.getLong(field));
171         item.setLong(field, val);
172         item.setLong(getEndField(field), val);
173     }
174     
175     /**
176      * Update the values in an interpolated column (a set of three columns
177      * representing a current value along with starting and ending values).
178      * The current value will become the new starting value, while the given
179      * value will become the new current and ending values.
180      * @param item the VisualItem to update
181      * @param field the base field to update, start and ending fields will
182      * also be updated, too.
183      * @param val the value to set
184      */

185     public static void updateFloat(VisualItem item, String JavaDoc field, float val)
186     {
187         item.setFloat(getStartField(field), item.getFloat(field));
188         item.setFloat(field, val);
189         item.setFloat(getEndField(field), val);
190     }
191     
192     /**
193      * Update the values in an interpolated column (a set of three columns
194      * representing a current value along with starting and ending values).
195      * The current value will become the new starting value, while the given
196      * value will become the new current and ending values.
197      * @param item the VisualItem to update
198      * @param field the base field to update, start and ending fields will
199      * also be updated, too.
200      * @param val the value to set
201      */

202     public static void updateDouble(VisualItem item, String JavaDoc field, double val)
203     {
204         item.setDouble(getStartField(field), item.getDouble(field));
205         item.setDouble(field, val);
206         item.setDouble(getEndField(field), val);
207     }
208     
209     /**
210      * Update the values in an interpolated column (a set of three columns
211      * representing a current value along with starting and ending values).
212      * The current value will become the new starting value, while the given
213      * value will become the new current and ending values.
214      * @param item the VisualItem to update
215      * @param field the base field to update, start and ending fields will
216      * also be updated, too.
217      * @param b the value to set
218      */

219     public static void updateBoolean(VisualItem item, String JavaDoc field, boolean b)
220     {
221         item.setBoolean(getStartField(field), item.getBoolean(field));
222         item.setBoolean(field, b);
223         item.setBoolean(getEndField(field), b);
224     }
225     
226     /**
227      * Update the visibility of an item. The current visibility will become the
228      * new starting visibility, while the given visibility value will become
229      * the new current and ending visibility.
230      * @param item the VisualItem to update
231      * @param val the visibility value to set
232      */

233     public static void updateVisible(VisualItem item, boolean val) {
234         item.setStartVisible(item.isVisible());
235         item.setVisible(val);
236         item.setEndVisible(val);
237     }
238     
239     /**
240      * Update the x-coordinate of an item. The current x value will become the
241      * new starting x value, while the given value will become the new current
242      * x and ending x values. This method also supports an optional referrer
243      * item, whose x coordinate will become the new starting x coordinate
244      * of item if item's current x value is NaN.
245      * @param item the VisualItem to update
246      * @param referrer an optional referrer VisualItem
247      * @param x the x value to set
248      */

249     public static void setX(VisualItem item, VisualItem referrer, double x) {
250         double sx = item.getX();
251         if ( Double.isNaN(sx) )
252             sx = (referrer != null ? referrer.getX() : x);
253         
254         item.setStartX(sx);
255         item.setEndX(x);
256         item.setX(x);
257     }
258
259     /**
260      * Update the y-coordinate of an item. The current y value will become the
261      * new starting y value, while the given value will become the new current
262      * x and ending y values. This method also supports an optional referrer
263      * item, whose y coordinate will become the new starting y coordinate
264      * of item if item's current x value is NaN.
265      * @param item the VisualItem to update
266      * @param referrer an optional referrer VisualItem
267      * @param y the y value to set
268      */

269     public static void setY(VisualItem item, VisualItem referrer, double y) {
270         double sy = item.getY();
271         if ( Double.isNaN(sy) )
272             sy = (referrer != null ? referrer.getY() : y);
273         
274         item.setStartY(sy);
275         item.setEndY(y);
276         item.setY(y);
277     }
278     
279     // ------------------------------------------------------------------------
280
// Group Name Methods
281

282     /**
283      * Indicates if a group is a child group, a non-top-level data group in
284      * a set of nested data groups (e.g., the node or edge table of a
285      * graph or tree).
286      * @return true if the group is a nested, or child, group
287      */

288     public static boolean isChildGroup(String JavaDoc group) {
289         return group.indexOf(GROUP_DELIMITER) != -1;
290     }
291     
292     /**
293      * Get the parent group string of a child group, stripping off the
294      * bottom-level group from the group name (e.g., graph.nodes --> graph).
295      * @param group the group name
296      * @return the stripped parent group name
297      */

298     public static String JavaDoc getParentGroup(String JavaDoc group) {
299         int idx = group.lastIndexOf(GROUP_DELIMITER);
300         return (idx < 0 ? null : group.substring(0,idx));
301     }
302
303     /**
304      * Get the tail group name of a child group, stripping all but the
305      * bottom-level group from the group name (e.g., graph.nodes --> nodes).
306      * @param group the group name
307      * @return the stripped child group name
308      */

309     public static String JavaDoc getChildGroup(String JavaDoc group) {
310         int idx = group.lastIndexOf(GROUP_DELIMITER);
311         return (idx < 0 ? null : group.substring(idx+1));
312     }
313     
314     /**
315      * Get the group name for the given parent and child group, simply
316      * concatenating them together with a group delimiter in between.
317      * @param parent the parent group name
318      * @param child the child group name
319      * @return the combined group name
320      */

321     public static String JavaDoc getGroupName(String JavaDoc parent, String JavaDoc child) {
322         return parent + GROUP_DELIMITER + child;
323     }
324     
325     /**
326      * For a given interpolated field name, get the name of the start
327      * field.
328      * @param field the data field
329      * @return the starting field for the interpolated column
330      */

331     public static String JavaDoc getStartField(String JavaDoc field) {
332         return field+":start";
333     }
334     
335     /**
336      * For a given interpolated field name, get the name of the end
337      * field.
338      * @param field the data field
339      * @return the ending field for the interpolated column
340      */

341     public static String JavaDoc getEndField(String JavaDoc field) {
342         return field+":end";
343     }
344     
345     // ------------------------------------------------------------------------
346
// Schema Routines
347

348     /**
349      * Get an instance of the default Schema used for VisualItem instances.
350      * Contains all the data members commonly used to model a visual element,
351      * such as x,y position, stroke, fill, and text, colors, size, font,
352      * and validated, visibility, interactive, fixed, highlight, and mouse
353      * hover fields.
354      * @return the VisualItem data Schema
355      */

356     public static Schema getVisualItemSchema() {
357         Schema s = new Schema();
358         
359         // booleans
360
s.addColumn(VisualItem.VALIDATED, boolean.class, Boolean.FALSE);
361         s.addColumn(VisualItem.VISIBLE, boolean.class, Boolean.TRUE);
362         s.addColumn(VisualItem.STARTVISIBLE, boolean.class, Boolean.FALSE);
363         s.addColumn(VisualItem.ENDVISIBLE, boolean.class, Boolean.TRUE);
364         s.addColumn(VisualItem.INTERACTIVE, boolean.class, Boolean.TRUE);
365         s.addColumn(VisualItem.EXPANDED, boolean.class, Boolean.TRUE);
366         s.addColumn(VisualItem.FIXED, boolean.class, Boolean.FALSE);
367         s.addColumn(VisualItem.HIGHLIGHT, boolean.class, Boolean.FALSE);
368         s.addColumn(VisualItem.HOVER, boolean.class, Boolean.FALSE);
369         
370         s.addInterpolatedColumn(VisualItem.X, double.class);
371         s.addInterpolatedColumn(VisualItem.Y, double.class);
372         
373         // bounding box
374
s.addColumn(VisualItem.BOUNDS, Rectangle2D JavaDoc.class, new Rectangle2D.Double JavaDoc());
375         
376         // color
377
Integer JavaDoc defStroke = new Integer JavaDoc(ColorLib.rgba(0,0,0,0));
378         s.addInterpolatedColumn(VisualItem.STROKECOLOR, int.class, defStroke);
379
380         Integer JavaDoc defFill = new Integer JavaDoc(ColorLib.rgba(0,0,0,0));
381         s.addInterpolatedColumn(VisualItem.FILLCOLOR, int.class, defFill);
382
383         Integer JavaDoc defTextColor = new Integer JavaDoc(ColorLib.rgba(0,0,0,0));
384         s.addInterpolatedColumn(VisualItem.TEXTCOLOR, int.class, defTextColor);
385
386         // size
387
s.addInterpolatedColumn(VisualItem.SIZE, double.class, new Double JavaDoc(1));
388         
389         // shape
390
s.addColumn(VisualItem.SHAPE, int.class,
391             new Integer JavaDoc(Constants.SHAPE_RECTANGLE));
392         
393         // stroke
394
s.addColumn(VisualItem.STROKE, Stroke JavaDoc.class, new BasicStroke JavaDoc());
395         
396         // font
397
Font JavaDoc defFont = FontLib.getFont("SansSerif",Font.PLAIN,10);
398         s.addInterpolatedColumn(VisualItem.FONT, Font JavaDoc.class, defFont);
399         
400         // degree-of-interest
401
s.addColumn(VisualItem.DOI, double.class, new Double JavaDoc(Double.MIN_VALUE));
402
403         return s;
404     }
405     
406     /**
407      * Get the minimal Schema needed for a unique VisualItem. Can be useful
408      * for derived groups that inherit other visual properties from a
409      * another visual data group.
410      * @return the minimal VisualItem data Schema
411      */

412     public static Schema getMinimalVisualSchema() {
413         Schema s = new Schema();
414         
415         // booleans
416
s.addColumn(VisualItem.VALIDATED, boolean.class, Boolean.FALSE);
417         s.addColumn(VisualItem.VISIBLE, boolean.class, Boolean.TRUE);
418         s.addColumn(VisualItem.STARTVISIBLE, boolean.class, Boolean.FALSE);
419         s.addColumn(VisualItem.ENDVISIBLE, boolean.class, Boolean.TRUE);
420         s.addColumn(VisualItem.INTERACTIVE, boolean.class, Boolean.TRUE);
421         
422         // bounding box
423
s.addColumn(VisualItem.BOUNDS, Rectangle2D JavaDoc.class, new Rectangle2D.Double JavaDoc());
424         
425         return s;
426     }
427     
428     /**
429      * Get the VisualItem Schema used for axis tick marks and labels. Extends
430      * the VisualItem Schema with an additional end-point coordinate, a
431      * String label field, and numeric value field for storing the value
432      * which the axis label corresponds to.
433      * @return the Schema for axis tick marks and labels.
434      */

435     public static Schema getAxisLabelSchema() {
436         Schema s = getVisualItemSchema();
437
438         s.setDefault(VisualItem.STARTVISIBLE, Boolean.FALSE);
439         
440         Integer JavaDoc defColor = new Integer JavaDoc(ColorLib.gray(230));
441         s.setInterpolatedDefault(VisualItem.STROKECOLOR, defColor);
442         
443         defColor = new Integer JavaDoc(ColorLib.gray(150));
444         s.setInterpolatedDefault(VisualItem.TEXTCOLOR, defColor);
445
446         Double JavaDoc nan = new Double JavaDoc(Double.NaN);
447         s.addInterpolatedColumn(VisualItem.X2, double.class);
448         s.addInterpolatedColumn(VisualItem.Y2, double.class);
449         
450         s.addColumn(VisualItem.LABEL, String JavaDoc.class);
451         s.addColumn(VisualItem.VALUE, double.class, nan);
452         
453         return s;
454     }
455     
456 } // end of class PrefuseLib
457
Popular Tags