home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / Label.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  213 lines

  1. /*
  2.  * @(#)Label.java    1.31 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.LabelPeer;
  17.  
  18. /**
  19.  * A <code>Label</code> object is a component for placing text in a 
  20.  * container. A label displays a single line of read-only text.
  21.  * The text can be changed by the application, but a user cannot edit it 
  22.  * directly.  
  23.  * <p>
  24.  * For example, the code . . .
  25.  * <p>
  26.  * <hr><blockquote><pre>
  27.  * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); 
  28.  * add(new Label("Hi There!")); 
  29.  * add(new Label("Another Label"));
  30.  * </pre></blockquote><hr>
  31.  * <p>
  32.  * produces the following label:
  33.  * <p>
  34.  * <img src="images-awt/Label-1.gif" 
  35.  * ALIGN=center HSPACE=10 VSPACE=7>
  36.  *
  37.  * @version    1.31, 07/01/98
  38.  * @author     Sami Shaio
  39.  * @since       JDK1.0
  40.  */
  41. public class Label extends Component {
  42.  
  43.     /**
  44.      * Indicates that the label should be left justified. 
  45.      * @since   JDK1.0
  46.      */
  47.     public static final int LEFT     = 0;
  48.  
  49.     /** 
  50.      * Indicates that the label should be centered. 
  51.      * @since   JDK1.0
  52.      */
  53.     public static final int CENTER     = 1;
  54.  
  55.     /**
  56.      * Indicates that the label should be right justified. 
  57.      * @since   JDK1.0t.
  58.      */
  59.     public static final int RIGHT     = 2;
  60.  
  61.     /**
  62.      * The text of this label.
  63.      */
  64.     String text;
  65.     
  66.     /**
  67.      * The label's alignment.  The default alignment is set
  68.      * to be left justified.
  69.      */
  70.     int       alignment = LEFT;
  71.  
  72.     private static final String base = "label";
  73.     private static int nameCounter = 0;
  74.  
  75.     /*
  76.      * JDK 1.1 serialVersionUID 
  77.      */
  78.      private static final long serialVersionUID = 3094126758329070636L;
  79.  
  80.     /**
  81.      * Constructs an empty label.
  82.      * @since JDK1.0
  83.      */
  84.     public Label() {
  85.     this("", LEFT);
  86.     }
  87.  
  88.     /**
  89.      * Constructs a new label with the specified string of text, 
  90.      * left justified.
  91.      * @param text the string that the label presents.
  92.      * @since JDK1.0
  93.      */
  94.     public Label(String text) {
  95.         this(text, LEFT);
  96.     }
  97.  
  98.     /**
  99.      * Constructs a new label that presents the specified string of 
  100.      * text with the specified alignment.
  101.      * <p>
  102.      * Possible values for <code>alignment</code> are <code>Label.LEFT</code>, 
  103.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  104.      * @param     text        the string that the label presents.
  105.      * @param     alignment   the alignment value.
  106.      * @since     JDK1.0
  107.      */
  108.     public Label(String text, int alignment) {
  109.     this.text = text;
  110.     setAlignment(alignment);
  111.     }
  112.  
  113.     /**
  114.      * Construct a name for this component.  Called by getName() when the
  115.      * name is null.
  116.      */
  117.     String constructComponentName() {
  118.         return base + nameCounter++;
  119.     }
  120.  
  121.     /**
  122.      * Creates the peer for this label.  The peer allows us to
  123.      * modify the appearance of the label without changing its 
  124.      * functionality.
  125.      */
  126.     public void addNotify() {
  127.       synchronized (getTreeLock()) {
  128.       if (peer == null)
  129.         peer = getToolkit().createLabel(this);
  130.     super.addNotify();
  131.       }
  132.     }
  133.  
  134.     /** 
  135.      * Gets the current alignment of this label. Possible values are
  136.      * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and 
  137.      * <code>Label.CENTER</code>.
  138.      * @see        java.awt.Label#setAlignment
  139.      * @since      JDK1.0
  140.      */
  141.     public int getAlignment() {
  142.     return alignment;
  143.     }
  144.  
  145.     /** 
  146.      * Sets the alignment for this label to the specified alignment.
  147.      * Possible values are <code>Label.LEFT</code>, 
  148.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  149.      * @param      alignment    the alignment to be set.
  150.      * @exception  IllegalArgumentException if an improper value for 
  151.      *                          <code>alignment</code> is given. 
  152.      * @see        java.awt.Label#getAlignment
  153.      * @since      JDK1.0
  154.      */
  155.     public synchronized void setAlignment(int alignment) {
  156.     switch (alignment) {
  157.       case LEFT:
  158.       case CENTER:
  159.       case RIGHT:
  160.         this.alignment = alignment;
  161.             LabelPeer peer = (LabelPeer)this.peer;
  162.         if (peer != null) {
  163.         peer.setAlignment(alignment);
  164.         }
  165.         return;
  166.     }
  167.     throw new IllegalArgumentException("improper alignment: " + alignment);
  168.     }
  169.  
  170.     /** 
  171.      * Gets the text of this label. 
  172.      * @return     the text of this label.
  173.      * @see        java.awt.Label#setText
  174.      * @since      JDK1.0
  175.      */
  176.     public String getText() {
  177.     return text;
  178.     }
  179.  
  180.     /** 
  181.      * Sets the text for this label to the specified text.
  182.      * @param      text the text that this label presents. 
  183.      * @see        java.awt.Label#getText
  184.      * @since      JDK1.0
  185.      */
  186.     public synchronized void setText(String text) {
  187.     if (text != this.text && (this.text == null
  188.                     || !this.text.equals(text))) {
  189.         this.text = text;
  190.             LabelPeer peer = (LabelPeer)this.peer;
  191.         if (peer != null) {
  192.         peer.setText(text);
  193.         }
  194.     }
  195.     }
  196.  
  197.     /**
  198.      * Returns the parameter string representing the state of this 
  199.      * label. This string is useful for debugging. 
  200.      * @return     the parameter string of this label.
  201.      * @since      JDK1.0
  202.      */
  203.     protected String paramString() {
  204.     String str = ",align=";
  205.     switch (alignment) {
  206.       case LEFT:   str += "left"; break;
  207.       case CENTER: str += "center"; break;
  208.       case RIGHT:  str += "right"; break;
  209.     }
  210.     return super.paramString() + str + ",text=" + text;
  211.     }
  212. }
  213.