picsou.util
Class WordWrap

java.lang.Object
  extended by picsou.util.WordWrap
All Implemented Interfaces:
java.io.Serializable

public class WordWrap
extends java.lang.Object
implements java.io.Serializable

WordWrap is a simple word-wrapping class which provides word-wrap either to columns of raw text; or to some number of pixels (given a font). It's not terribly efficient but works reasonably well. Tabs are considered to be the same length as spaces. The input is the string to wrap, and the output is the same string with returns inserted in the appropriate location.

See Also:
Serialized Form

Constructor Summary
WordWrap()
           
 
Method Summary
static java.lang.String[] split(java.lang.String str)
          A useful auxillary method: once you're word-wrapped your text, you can use this to break it into multiple strings at the \n position.
static java.lang.String toHTML(java.lang.String text)
          A useful auxillary method: once you've word-wrapped your text, you can use this to convert it into 'HTML' style, where < is converted into &lt;, & is converted into &amp;, and \n or \r are converted into <br>.
static java.lang.String wrap(java.lang.String string, int numColumns)
          Wraps a string to a given number of columns.
static java.lang.String wrap(java.lang.String string, int numPixels, java.awt.FontMetrics metrics)
          Wraps a string to a given number of pixels in width, given a font whose metrics are provided as well.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WordWrap

public WordWrap()
Method Detail

wrap

public static java.lang.String wrap(java.lang.String string,
                                    int numColumns)
Wraps a string to a given number of columns.


wrap

public static java.lang.String wrap(java.lang.String string,
                                    int numPixels,
                                    java.awt.FontMetrics metrics)
Wraps a string to a given number of pixels in width, given a font whose metrics are provided as well.


split

public static java.lang.String[] split(java.lang.String str)
A useful auxillary method: once you're word-wrapped your text, you can use this to break it into multiple strings at the \n position.


toHTML

public static java.lang.String toHTML(java.lang.String text)
A useful auxillary method: once you've word-wrapped your text, you can use this to convert it into 'HTML' style, where < is converted into &lt;, & is converted into &amp;, and \n or \r are converted into <br>.

You can use this to make multi-line buttons and multi-line menus like this:


      String myText = "Here is the big text that we want to have word-wrapped";
      int myNumberOfPixels = 50; // our word-wrap pixel length
      JButton button = new JButton();
      button.setText("<html>" + sim.util.WordWrap.toHTML(sim.util.WordWrap.wrap(
      myText, myNumberOfPixels, button.getFontMetrics(button.getFont()))) +
      "</html>");
      

On MacOS X Java 1.3 (which will go away soon), this isn't sufficient -- the default font is incorrectly Times Roman, and the default point size is slightly off. You can get the font fixed, but not the size, with:


      String myText = "Here is the big text that we want to have word-wrapped";
      int myNumberOfPixels = 50; // our word-wrap pixel length
      JButton button = new JButton();
      button.setText("<html><font face=\"" + 
      sim.util.WordWrap.toHTML(button.getFont().getFamily()) + "\">" + 
      sim.util.WordWrap.toHTML(sim.util.WordWrap.wrap(
      myText, myNumberOfPixels, button.getFontMetrics(button.getFont()))) +
      "</font></html>");