1 package prefuse.util; 2 3 import java.awt.BasicStroke ; 4 import java.awt.Font ; 5 import java.awt.Stroke ; 6 import java.awt.geom.Rectangle2D ; 7 8 import prefuse.Constants; 9 import prefuse.Display; 10 import prefuse.data.Schema; 11 import prefuse.visual.VisualItem; 12 13 18 public class PrefuseLib { 19 20 24 private static final String GROUP_DELIMITER 25 = PrefuseConfig.get("data.delimiter"); 26 30 private static final double SIZE_SCALE_FACTOR 31 = PrefuseConfig.getDouble("size.scale2D"); 32 36 public static final String FIELD_PREFIX 37 = PrefuseConfig.get("data.visual.fieldPrefix"); 38 39 41 private PrefuseLib() { 42 } 44 45 48 52 public static String 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 63 public static String 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 75 public static String getDisplayStats(Display d) { 76 float fr = Math.round(d.getFrameRate()*100f)/100f; 77 78 Runtime 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 sb = new StringBuffer (); 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 104 111 public static double getSize2D(double size) { 112 return Math.pow(size, SIZE_SCALE_FACTOR); 113 } 114 115 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 137 public static void update(VisualItem item, String field, Object val) { 138 item.set(getStartField(field), item.get(field)); 139 item.set(field, val); 140 item.set(getEndField(field), val); 141 } 142 143 153 public static void updateInt(VisualItem item, String 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 169 public static void updateLong(VisualItem item, String 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 185 public static void updateFloat(VisualItem item, String 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 202 public static void updateDouble(VisualItem item, String 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 219 public static void updateBoolean(VisualItem item, String 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 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 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 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 282 288 public static boolean isChildGroup(String group) { 289 return group.indexOf(GROUP_DELIMITER) != -1; 290 } 291 292 298 public static String getParentGroup(String group) { 299 int idx = group.lastIndexOf(GROUP_DELIMITER); 300 return (idx < 0 ? null : group.substring(0,idx)); 301 } 302 303 309 public static String getChildGroup(String group) { 310 int idx = group.lastIndexOf(GROUP_DELIMITER); 311 return (idx < 0 ? null : group.substring(idx+1)); 312 } 313 314 321 public static String getGroupName(String parent, String child) { 322 return parent + GROUP_DELIMITER + child; 323 } 324 325 331 public static String getStartField(String field) { 332 return field+":start"; 333 } 334 335 341 public static String getEndField(String field) { 342 return field+":end"; 343 } 344 345 348 356 public static Schema getVisualItemSchema() { 357 Schema s = new Schema(); 358 359 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 s.addColumn(VisualItem.BOUNDS, Rectangle2D .class, new Rectangle2D.Double ()); 375 376 Integer defStroke = new Integer (ColorLib.rgba(0,0,0,0)); 378 s.addInterpolatedColumn(VisualItem.STROKECOLOR, int.class, defStroke); 379 380 Integer defFill = new Integer (ColorLib.rgba(0,0,0,0)); 381 s.addInterpolatedColumn(VisualItem.FILLCOLOR, int.class, defFill); 382 383 Integer defTextColor = new Integer (ColorLib.rgba(0,0,0,0)); 384 s.addInterpolatedColumn(VisualItem.TEXTCOLOR, int.class, defTextColor); 385 386 s.addInterpolatedColumn(VisualItem.SIZE, double.class, new Double (1)); 388 389 s.addColumn(VisualItem.SHAPE, int.class, 391 new Integer (Constants.SHAPE_RECTANGLE)); 392 393 s.addColumn(VisualItem.STROKE, Stroke .class, new BasicStroke ()); 395 396 Font defFont = FontLib.getFont("SansSerif",Font.PLAIN,10); 398 s.addInterpolatedColumn(VisualItem.FONT, Font .class, defFont); 399 400 s.addColumn(VisualItem.DOI, double.class, new Double (Double.MIN_VALUE)); 402 403 return s; 404 } 405 406 412 public static Schema getMinimalVisualSchema() { 413 Schema s = new Schema(); 414 415 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 s.addColumn(VisualItem.BOUNDS, Rectangle2D .class, new Rectangle2D.Double ()); 424 425 return s; 426 } 427 428 435 public static Schema getAxisLabelSchema() { 436 Schema s = getVisualItemSchema(); 437 438 s.setDefault(VisualItem.STARTVISIBLE, Boolean.FALSE); 439 440 Integer defColor = new Integer (ColorLib.gray(230)); 441 s.setInterpolatedDefault(VisualItem.STROKECOLOR, defColor); 442 443 defColor = new Integer (ColorLib.gray(150)); 444 s.setInterpolatedDefault(VisualItem.TEXTCOLOR, defColor); 445 446 Double nan = new Double (Double.NaN); 447 s.addInterpolatedColumn(VisualItem.X2, double.class); 448 s.addInterpolatedColumn(VisualItem.Y2, double.class); 449 450 s.addColumn(VisualItem.LABEL, String .class); 451 s.addColumn(VisualItem.VALUE, double.class, nan); 452 453 return s; 454 } 455 456 } | Popular Tags |