home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TEXT_FLO.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  6.6 KB  |  226 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.input.*;
  4. import sub_arctic.output.*;
  5. import sub_arctic.constraints.std_function;
  6.  
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.util.StringTokenizer;
  10. import java.util.Vector;
  11. import java.awt.Color;
  12.  
  13. /**
  14.  * This class takes a string and breaks it into lines which "fit" into
  15.  * the text column's width based on embedded HTML (subset) formatting. This 
  16.  * object provides no input behavior.
  17.  *
  18.  * @author Ian Smith
  19.  */
  20. public class text_flow extends column {
  21.  
  22.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  23.  
  24.   /**
  25.    * The text of this object
  26.    */
  27.   protected String _text;
  28.  
  29.   /**
  30.    * Get the text of this object. 
  31.    * @return String the text contained in this flow
  32.    */
  33.   public String text() { return _text;}
  34.  
  35.   /**
  36.    * Set the text in this object. 
  37.    * @param String t the new text of this object
  38.    */
  39.   public void set_text(String t) {
  40.     _text=t;
  41.     do_layout();
  42.   }
  43.  
  44.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  45.  
  46.   /**
  47.    * This is the minimum size that this object can be before we bother
  48.    * to try to do a layout. This is in pixels.
  49.    */
  50.   protected int minimum_layout_size=20;
  51.  
  52.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  53.  
  54.   /**
  55.    * Create a new text flow with a given string. This constructor assumes
  56.    * that you are going to set the position of this object with
  57.    * constraints. 
  58.    * 
  59.    * @param String v the string to put in this text flow.
  60.    * @param int    w width of the interactor.
  61.    * @param int    h height of the interactor.
  62.    */
  63.   public text_flow(String v, int w, int h) 
  64. {
  65.     /* we DO NOT want size by children */
  66.     super(0 /* ignored */,0 /*ignored */,
  67.       w, h, 2,0,true,true,false /* crucial */,column.LEFT_JUSTIFIED,null);
  68.     set_text(v);
  69.   }
  70.  
  71.    //had:
  72.    //* @exception general PROPAGATED
  73.    //* @exception bad_value PROPAGATED
  74.  
  75.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  76.  
  77.   /**
  78.    * Create a new text flow with a given string. This constructor assumes
  79.    * that you are going to set the position of this object with
  80.    * constraints and that it is ok for this object to get as tall 
  81.    * as necessary to fit the string. If you are using this version,
  82.    * you should probably be using this in conjunction with a 
  83.    * panner.
  84.    * 
  85.    * @param String v the string to put in this text flow.
  86.    * @param int    w width of the interactor.
  87.    */
  88.   public text_flow(String v, int w) 
  89. {
  90.     /* we DO NOT want size by children */
  91.     super(0 /* ignored */,0 /*ignored */,
  92.       w, 10 /*ignored*/, 2,0,true,true,false /* crucial */,
  93.       column.LEFT_JUSTIFIED,null);
  94.  
  95.     /* put a constraint on our height to grow to be the right size */
  96.     set_h_constraint(std_function.offset(LAST_CHILD.Y2(),0));
  97.  
  98.     set_text(v);
  99.   }
  100.  
  101.    //had:
  102.    //* @exception general PROPAGATED
  103.    //* @exception bad_value PROPAGATED
  104.  
  105.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  106.  
  107.   /**
  108.    * Do the layout procedure. This ends up removing all the children
  109.    * and re-parsing the text. Notes about this parser:<P>
  110.    * <OL>
  111.    * <LI> It is not robust in the face of badly formed HTML. 
  112.    * <LI> It expects the <I>de facto</I> standard of using the P tag to 
  113.    *      separate paragraphs, rather than the more correct P matched with a 
  114.    *      slash P.
  115.    * <LI> We don't check to see that the tags are matched with the correct 
  116.    *      slash tag. We just blindly assume you matched them right.
  117.    * </OL>
  118.    */
  119.   protected void do_layout() 
  120. {
  121.  
  122.     /* if we don't have any text, don't bother to do a layout */
  123.     if ((text()==null) ||
  124.     (text()=="")) return;
  125.     
  126.     /* if the object is too small, don't bother because the display
  127.      * will be so ugly as to be useless */
  128.     if (w()<minimum_layout_size) {
  129.       return;
  130.     }
  131.  
  132.     /* remove all the children */
  133.     while (num_children()>0) {
  134.       remove_child(0);
  135.     }
  136.  
  137.     /* initialization of the parser */
  138.     html_element.init(this,text());
  139.     /* do the parse and layout */
  140.     html_element.parse();
  141.   }
  142.  
  143.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  144.  
  145.   /**
  146.    * This variable holds the string for the base font name. We 
  147.    * default to Helvetica.
  148.    */
  149.   protected String _font_name="Helvetica";
  150.  
  151.   /**
  152.    * Retrieve the current base font name
  153.    * @return String the string with with font name we are using
  154.    */
  155.   public String font_name() { return _font_name;}
  156.  
  157.   /**
  158.    * Set the current base font name. Be aware that this better be a
  159.    * font name that exists on your system and it will only take effect
  160.    * the next time this variable is consulted by the parser. Thus
  161.    * if you want to force this change you'll need to call do_layout
  162.    * yourself.
  163.    */
  164.   public void set_font_name(String fn) {
  165.     _font_name=fn;
  166.   }
  167.  
  168.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  169.  
  170.   /**
  171.    * We override the set_raw_w so we can catch changes to the 
  172.    * width and do a re-layout.
  173.    * @param int n the new width
  174.    */
  175.   public void set_raw_w(int n) 
  176. {
  177.     super.set_raw_w(n);
  178.     do_layout();
  179.   }
  180.  
  181.    //had:
  182.    //* @exception general PROPAGATED
  183.  
  184.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  185.  
  186.   /**
  187.    * This is where we hold the basic font size for this text flow.
  188.    */
  189.   protected int _font_size=12;
  190.  
  191.   /**
  192.    * Retrieve the basic font size for this text flow.
  193.    * @return int the font size (in points) for basic text in this flow
  194.    */
  195.   public int font_size() { return _font_size;}
  196.  
  197.   /**
  198.    * Set the font size of this text flow. This does <B>not</B> take
  199.    * effect immediately, but rather on the next layout. Thus, you
  200.    * have to force a re-layout with "do_layout()" if you want this
  201.    * happen now. <H6> When you say "now", what exactly do you mean?</H6>
  202.    * @param int n new basic font size for this flow (in points)
  203.    */
  204.   public void set_font_size(int n) { _font_size=n;}
  205.  
  206.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  207. }
  208.  
  209.  
  210. /*=========================== COPYRIGHT NOTICE ===========================
  211.  
  212. This file is part of the subArctic user interface toolkit.
  213.  
  214. Copyright (c) 1996 Scott Hudson and Ian Smith
  215. All rights reserved.
  216.  
  217. The subArctic system is freely available for most uses under the terms
  218. and conditions described in 
  219.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  220. and appearing in full in the lib/interactor.java source file.
  221.  
  222. The current release and additional information about this software can be 
  223. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  224.  
  225. ========================================================================*/
  226.