home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Label.java < prev    next >
Text File  |  1998-01-23  |  6KB  |  213 lines

  1. /*
  2.  * @(#)Label.java    1.27 97/10/07
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.peer.LabelPeer;
  25.  
  26. /**
  27.  * A <code>Label</code> object is a component for placing text in a 
  28.  * container. A label displays a single line of read-only text.
  29.  * The text can be changed by the application, but a user cannot edit it 
  30.  * directly.  
  31.  * <p>
  32.  * For example, the code . . .
  33.  * <p>
  34.  * <hr><blockquote><pre>
  35.  * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); 
  36.  * add(new Label("Hi There!")); 
  37.  * add(new Label("Another Label"));
  38.  * </pre></blockquote><hr>
  39.  * <p>
  40.  * produces the following label:
  41.  * <p>
  42.  * <img src="images-awt/Label-1.gif" 
  43.  * ALIGN=center HSPACE=10 VSPACE=7>
  44.  *
  45.  * @version    1.27, 10/07/97
  46.  * @author     Sami Shaio
  47.  * @since       JDK1.0
  48.  */
  49. public class Label extends Component {
  50.  
  51.     /**
  52.      * Indicates that the label should be left justified. 
  53.      * @since   JDK1.0
  54.      */
  55.     public static final int LEFT     = 0;
  56.  
  57.     /** 
  58.      * Indicates that the label should be centered. 
  59.      * @since   JDK1.0
  60.      */
  61.     public static final int CENTER     = 1;
  62.  
  63.     /**
  64.      * Indicates that the label should be right justified. 
  65.      * @since   JDK1.0t.
  66.      */
  67.     public static final int RIGHT     = 2;
  68.  
  69.     /**
  70.      * The text of this label.
  71.      */
  72.     String text;
  73.     
  74.     /**
  75.      * The label's alignment.  The default alignment is set
  76.      * to be left justified.
  77.      */
  78.     int       alignment = LEFT;
  79.  
  80.     private static final String base = "label";
  81.     private static int nameCounter = 0;
  82.  
  83.     /*
  84.      * JDK 1.1 serialVersionUID 
  85.      */
  86.      private static final long serialVersionUID = 3094126758329070636L;
  87.  
  88.     /**
  89.      * Constructs an empty label.
  90.      * @since JDK1.0
  91.      */
  92.     public Label() {
  93.     this("", LEFT);
  94.     }
  95.  
  96.     /**
  97.      * Constructs a new label with the specified string of text, 
  98.      * left justified.
  99.      * @param text the string that the label presents.
  100.      * @since JDK1.0
  101.      */
  102.     public Label(String text) {
  103.         this(text, LEFT);
  104.     }
  105.  
  106.     /**
  107.      * Constructs a new label that presents the specified string of 
  108.      * text with the specified alignment.
  109.      * <p>
  110.      * Possible values for <code>alignment</code> are <code>Label.LEFT</code>, 
  111.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  112.      * @param     text        the string that the label presents.
  113.      * @param     alignment   the alignment value.
  114.      * @since     JDK1.0
  115.      */
  116.     public Label(String text, int alignment) {
  117.     this.name = base + nameCounter++;
  118.     this.text = text;
  119.     setAlignment(alignment);
  120.     }
  121.  
  122.     /**
  123.      * Creates the peer for this label.  The peer allows us to
  124.      * modify the appearance of the label without changing its 
  125.      * functionality.
  126.      */
  127.     public void addNotify() {
  128.       synchronized (Component.LOCK) {
  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.