messageformatdemo.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,034 行 · 第 1/3 页

JAVA
1,034
字号
/*
 * @(#)MessageFormatDemo.java	 1.1 98/06/16
 *
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
 *
 * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
 *
 *   The original version of this source code and documentation is copyrighted
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
 * materials are provided under terms of a License Agreement between Taligent
 * and Sun. This technology is protected by multiple US and International
 * patents. This notice and attribution to Taligent may not be removed.
 *   Taligent is a registered trademark of Taligent, Inc.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.lang.*;
import java.text.*;

/**
 * <P> Pattern formats are used to put together sequences of strings, numbers,
 * dates, and other formats to create messages.  The pattern formatters
 * facilitate localization because they prevent both hard-coding of message
 * strings, <I> and </I> hard-coding of the concatenation sequence for portions
 * of message strings.  This means localizers can change the content, format,
 * and order of any text as appropriate for any language.
 * </P>
 * <P><CENTER>
 * <A HREF="#format">About the Pattern Format</A>&nbsp;
 * <A HREF="#arguments">Formatting Arguments</A><BR>
 * <A HREF="#choices">Supporting Multiple Choices</A>
 * </CENTER></P>
 * <HR>
 * <H3><A NAME="format">About the Pattern Format </A></H3>
 * <P> The pattern is a sequence of text and arguments, all of which can be
 * edited.  The positions of the arguments within the sequence are indicated
 * by a &quot;&#37;&quot; followed by a digit <I> n </I> identifying the
 * argument.  The pattern in the demo applet has three arguments, numbered 0,
 * 1, and 2.  The arguments can appear in any order within the sequence, which
 * can be edited and modified.
 * </P>
 * <TABLE WIDTH=100% CELLPADDING=5>
 * <TR>
 * <TH ALIGN=left VALIGN=bottom>To See This...</TH>
 * <TH ALIGN=left VALIGN=bottom>Do This...</TH>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can move arguments freely within the sequence
 * or delete arguments.  You can also edit or translate any of the unformatted
 * text.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Move the string &quot;on &#37;2&quot; from the end of the pattern to
 * the front and correct the capitalization
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * <TD WIDTH=50% VALIGN=top> Translations are provided in the demo applet for
 * the G7 countries.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Pull down the Locale menu
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 2.
 * </TD>
 * <TD> Try several different locales with the up and down arrow keys (on
 * Windows) or the mouse button (on Macintosh)
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * </TABLE>
 * <P><STRONG><I>Note:</I></STRONG> To add a real percentage character to the
 * pattern, enter &quot;&#37;&#37;&quot;.
 * </P>
 * <HR WIDTH=50% ALIGN=left>
 * <H3><A NAME="arguments"> Formatting Arguments </A></H3>
 * <P> The arguments can be either simple text strings or formattable,
 * localizable objects.  The pattern in the demo applet, for example, includes
 * a date, an unformatted string, and a more complex format called a <I> choice
 * </I> format (described below).  You can edit these arguments at will.  When
 * localizing, you can also select any format to be associated with an
 * argument.
 * </P>
 * <TABLE WIDTH=100% CELLPADDING=5>
 * <TR>
 * <TH ALIGN=left VALIGN=bottom>To See This... </TH>
 * <TH ALIGN=left VALIGN=bottom>Do This...</TH>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can modify the value of any argument.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Select the &quot;3&quot; in argument 2 and change it to another number
 * &#151;the formatted date adjusts to the new value
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can change the format of any argument, and can
 * specify &quot;unformatted&quot; arguments that are not localized.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Change the format type for argument 2 to <I> None </I>
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 2.
 * </TD>
 * <TD> Try different locales and notice that the date does not reformat for
 * different locales
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 3.
 * </TD>
 * <TD> Return the format back to <I>Date </I> and try different locales again.
 * The date reformats.
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * </TABLE>
 * <HR WIDTH=50% ALIGN=left>
 * <H3> <A NAME="choices">Supporting Multiple Choices </A></H3>
 * <P> Choice formats, like that used for argument 0, let localizers create
 * more natural messages, avoiding phrases like &quot;3 file(s)&quot;.  As
 * shown here, the correct text can be chosen for different numbers.  This
 * works even in more complicated contexts, such as Slavic languages which
 * have more than one plural format based on the number involved.  A particular
 * choice is chosen based on the value of the argument, and each choice can be
 * edited individually.
 * </P>
 * <P> Look also at the format for the choice associated with values of 2 and
 * higher, &quot;&#37;0|3&quot;.  The vertical bar indicates that the choice
 * uses the value for argument 0, but the format for argument 3 (in this case,
 * a Number).  This allows for a degree of flexibility in using different
 * formats.
 * </P>
 * <TABLE WIDTH=100% CELLPADDING=5>
 * <TR>
 * <TH ALIGN=left VALIGN=bottom>To See This...</TH>
 * <TH ALIGN=left VALIGN=bottom>Do This... </TH>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can edit the value of any of the choice
 * options.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Select the Choice String &quot;no files&quot; and type in &quot;not a
 * single file&quot;
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 2.
 * </TD>
 * <TD> Select the value for argument 0 and type in &quot;0&quot;
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can establish different choices for parameters
 * based on the value of an argument, so that strings are substituted that
 * agree numerically.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Select the value for argument 0 and type in &quot;0&quot;, then replace
 * it with a &quot;1&quot;, and then with a &quot;2&quot;.  The string changes
 * correspondingly.
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 2.
 * </TD>
 * <TD> Select argument 0 and return the value to &quot;0&quot;.  Choose the
 * French or German locale and notice that it makes correct substitutions in
 * any language.
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=50% VALIGN=top> You can add as many alternatives as you need for
 * different value ranges.
 * </TD>
 * <TD VALIGN=top>
 * <TABLE CELLSPACING=0 CELLPADDING=0>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 1.
 * </TD>
 * <TD> Select the U.S. English locale
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 2.
 * </TD>
 * <TD> Select the empty choice value box (under the &quot;2&quot;) and type
 * in &quot;10&quot;
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 3.
 * </TD>
 * <TD> Type in &quot;many files&quot; in the corresponding <I> Choice Strings
 * </I> field
 * </TD>
 * </TR>
 * <TR>
 * <TD WIDTH=10 VALIGN=top> 4.
 * </TD>
 * <TD> Enter a number larger than10 for the value of argument 0&#151;&quot;
 * many files&quot; is substituted in the resulting message
 * </TD>
 * </TR>
 * </TABLE>
 * </TD>
 * </TR>
 * </TABLE>
 * <P>  You can type in other text in the pattern, arguments, or choices fields
 * to  see different formatting behaviors.  Try it out!
 * </P>
 * @see        java.util.Format
 * @see        java.util.MessageFormat
 * @version    1.1 11/23/96
 * @author     Laura Werner, Mark Davis
*/

public class MessageFormatDemo extends DemoApplet
{
    /**
     * The main function which defines the behavior of the MessageFormatDemo
     * applet when an applet is started.
     */
    public static void main(String argv[]) {
        DemoApplet.showDemo(new MessageFormatFrame(null));
    }

    /**
     * This creates a MessageFormatFrame for the demo applet.
     */
    public Frame createDemoFrame(DemoApplet applet) {
        return new MessageFormatFrame(applet);
    }
}

/**
 * A Frame is a top-level window with a title. The default layout for a frame
 * is BorderLayout.  The MessageFormatFrame class defines the window layout of
 * MessageFormatDemo.
 */
 class MessageFormatFrame extends Frame implements WindowListener, ItemListener, KeyListener {

    /**
     * Constructs a new MessageFormatFrame that is initially invisible.
     */
    public MessageFormatFrame(DemoApplet applet)
    {
        super("Message Formatting Demo");
        this.applet = applet;
	addWindowListener(this);
        init();
        start();
    }

    /**
     * Initializes the applet. You never need to call this directly, it
     * is called automatically by the system once the applet is created.
     */
    public void init() {

        //Get all locales for debugging, but only get G7 locales for demos.
        if (DEBUG == true)
             locales = NumberFormat.getAvailableLocales();
        else locales = Utility.getG7Locales();

        buildGUI();

    }

    /**
     * Called to start the applet. You never need to call this method
     * directly, it is called when the applet's document is visited.
     */
    public void start() {

        // Stick some initial data into the controls....
        arg1Text.setText("3");
        arg1Type.select(CHOICE);

        arg2Text.setText("MyDisk");
        arg2Type.select(NONE);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?