home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / Label.java < prev    next >
Text File  |  1997-08-30  |  6KB  |  211 lines

  1. /*
  2.  * @(#)Label.java    1.26 97/06/16
  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.26, 06/16/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.     peer = getToolkit().createLabel(this);
  129.     super.addNotify();
  130.     }
  131.  
  132.     /** 
  133.      * Gets the current alignment of this label. Possible values are
  134.      * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and 
  135.      * <code>Label.CENTER</code>.
  136.      * @see        java.awt.Label#setAlignment
  137.      * @since      JDK1.0
  138.      */
  139.     public int getAlignment() {
  140.     return alignment;
  141.     }
  142.  
  143.     /** 
  144.      * Sets the alignment for this label to the specified alignment.
  145.      * Possible values are <code>Label.LEFT</code>, 
  146.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  147.      * @param      alignment    the alignment to be set.
  148.      * @exception  IllegalArgumentException if an improper value for 
  149.      *                          <code>alignment</code> is given. 
  150.      * @see        java.awt.Label#getAlignment
  151.      * @since      JDK1.0
  152.      */
  153.     public synchronized void setAlignment(int alignment) {
  154.     switch (alignment) {
  155.       case LEFT:
  156.       case CENTER:
  157.       case RIGHT:
  158.         this.alignment = alignment;
  159.             LabelPeer peer = (LabelPeer)this.peer;
  160.         if (peer != null) {
  161.         peer.setAlignment(alignment);
  162.         }
  163.         return;
  164.     }
  165.     throw new IllegalArgumentException("improper alignment: " + alignment);
  166.     }
  167.  
  168.     /** 
  169.      * Gets the text of this label. 
  170.      * @return     the text of this label.
  171.      * @see        java.awt.Label#setText
  172.      * @since      JDK1.0
  173.      */
  174.     public String getText() {
  175.     return text;
  176.     }
  177.  
  178.     /** 
  179.      * Sets the text for this label to the specified text.
  180.      * @param      text the text that this label presents. 
  181.      * @see        java.awt.Label#getText
  182.      * @since      JDK1.0
  183.      */
  184.     public synchronized void setText(String text) {
  185.     if (text != this.text && (this.text == null
  186.                     || !this.text.equals(text))) {
  187.         this.text = text;
  188.             LabelPeer peer = (LabelPeer)this.peer;
  189.         if (peer != null) {
  190.         peer.setText(text);
  191.         }
  192.     }
  193.     }
  194.  
  195.     /**
  196.      * Returns the parameter string representing the state of this 
  197.      * label. This string is useful for debugging. 
  198.      * @return     the parameter string of this label.
  199.      * @since      JDK1.0
  200.      */
  201.     protected String paramString() {
  202.     String str = ",align=";
  203.     switch (alignment) {
  204.       case LEFT:   str += "left"; break;
  205.       case CENTER: str += "center"; break;
  206.       case RIGHT:  str += "right"; break;
  207.     }
  208.     return super.paramString() + str + ",text=" + text;
  209.     }
  210. }
  211.