home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 June / INTERNET92.ISO / pc / software / windows / building / visual_dhtml / visualdhtmlwin9x_nt.exe / ocwordw.___ (.txt) < prev    next >
Encoding:
Java Class File  |  1999-02-28  |  2.0 KB  |  86 lines

  1. import java.awt.FontMetrics;
  2. import java.util.StringTokenizer;
  3. import java.util.Vector;
  4.  
  5. class ocwordw {
  6.    static String[] WrapText(String temp, int width, FontMetrics fm) {
  7.       Vector v_the = new Vector();
  8.       String s_build = "";
  9.       int s_length = 0;
  10.       int i = 0;
  11.       String s_temp = "";
  12.       boolean first_run = true;
  13.       boolean last_line = false;
  14.       boolean has_tokens = false;
  15.       temp.trim();
  16.       int num_paras = 0;
  17.       StringTokenizer p_st = new StringTokenizer(temp, "|");
  18.       num_paras = p_st.countTokens();
  19.       String[] paras;
  20.       if (num_paras == 0) {
  21.          num_paras = 1;
  22.          paras = new String[num_paras];
  23.          paras[0] = temp;
  24.       } else {
  25.          for(paras = new String[num_paras]; p_st.hasMoreTokens(); ++i) {
  26.             paras[i] = p_st.nextToken();
  27.          }
  28.       }
  29.  
  30.       for(int var17 = 0; var17 < num_paras; ++var17) {
  31.          for(StringTokenizer st = new StringTokenizer(paras[var17], " "); st.hasMoreTokens(); first_run = false) {
  32.             has_tokens = true;
  33.             s_temp = st.nextToken();
  34.             if (s_temp != null) {
  35.             }
  36.  
  37.             s_length += fm.stringWidth(s_temp + " ");
  38.             if (s_length < width) {
  39.                if (first_run) {
  40.                   s_build = s_temp;
  41.                } else {
  42.                   s_build = s_build + " " + s_temp;
  43.                }
  44.  
  45.                last_line = true;
  46.             } else {
  47.                v_the.addElement(s_build);
  48.                if (!st.hasMoreTokens()) {
  49.                   v_the.addElement(s_temp);
  50.                } else {
  51.                   s_length = fm.stringWidth(s_temp + " ");
  52.                   s_build = s_temp;
  53.                }
  54.  
  55.                last_line = false;
  56.             }
  57.          }
  58.  
  59.          if (last_line && has_tokens) {
  60.             last_line = false;
  61.             v_the.addElement(s_build);
  62.          }
  63.  
  64.          if (!has_tokens) {
  65.             v_the.addElement(paras[var17]);
  66.          } else {
  67.             has_tokens = false;
  68.          }
  69.  
  70.          if (var17 < num_paras - 1) {
  71.             v_the.addElement(" ");
  72.          }
  73.  
  74.          first_run = true;
  75.          has_tokens = false;
  76.          s_length = 0;
  77.          s_temp = "";
  78.          s_build = "";
  79.       }
  80.  
  81.       String[] the_string = new String[v_the.size()];
  82.       v_the.copyInto(the_string);
  83.       return the_string;
  84.    }
  85. }
  86.