StringUtils.java

00001 package edu.stanford.hci.r3.util;
00002 
00003 import java.awt.Dimension;
00004 import java.awt.Font;
00005 import java.awt.font.FontRenderContext;
00006 import java.awt.geom.Rectangle2D;
00007 import java.util.ArrayList;
00008 import java.util.List;
00009 
00019 public class StringUtils {
00020 
00026         public static Dimension getStringSize(String textToMeasure, Font displayFont) {
00027                 final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
00028 
00029                 // break it up by lines (in case there are newlines "\n")
00030                 final String[] strings = textToMeasure.split("\n");
00031 
00032                 double totalHeight = 0;
00033                 double maxWidth = 0;
00034 
00035                 for (String s : strings) {
00036                         Rectangle2D stringBounds = displayFont.getStringBounds(s, fontRenderContext);
00037                         maxWidth = Math.max(maxWidth, stringBounds.getWidth());
00038                         totalHeight += stringBounds.getHeight();
00039                 }
00040 
00041                 final Dimension dimension = new Dimension();
00042                 dimension.setSize(maxWidth, totalHeight);
00043                 return dimension;
00044         }
00045 
00051         public static String repeat(String string, int numTimes) {
00052                 StringBuilder sb = new StringBuilder();
00053                 for (int i = 0; i < numTimes; i++) {
00054                         sb.append(string);
00055                 }
00056                 return sb.toString();
00057         }
00058 
00067         public static List<String> splitString(String string, int maxCharsPerLine) {
00068                 final List<String> lines = new ArrayList<String>();
00069                 final String[] items = string.split(" ");
00070                 final StringBuilder currString = new StringBuilder();
00071                 for (String item : items) {
00072                         if (currString.length() + item.length() > maxCharsPerLine) {
00073                                 lines.add(currString.toString());
00074                                 currString.setLength(0); // clear buffer
00075                         }
00076                         currString.append(item + " ");
00077                 }
00078                 lines.add(currString.toString());
00079                 return lines;
00080         }
00081 
00082 }

Generated on Sat Apr 14 18:21:38 2007 for R3 Paper Toolkit by  doxygen 1.4.7