collatedemo.java

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

JAVA
1,001
字号
/*
 * @(#)CollateDemo.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.Locale;
import java.util.Vector;
import java.text.NumberFormat;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.text.ParseException;

/**
 * Concrete class for demonstrating language sensitive collation.
 * The following is the instruction on how to run the collation demo.
 * <p>
 * ===================
 * <H3>Customization</H3>
 * You can produce a new collation by adding to or changing an existing
 * one.
 * <H4>To show...</H4>
 * <BLOCKQUOTE>You can modify an existing collation to show how this works.
 * By adding items at the end of a collation you override earlier
 * information.
 * Watch how you can make the letter P sort at the end of the
 * alphabet.</BLOCKQUOTE>
 * <H4>Do...</H4>
 * <BLOCKQUOTE>1. Scroll to the end of the Sequence field. After the Z,
 * type
 * &quot;&lt; p , P&quot;. This will put the letter P (with both of its
 * case
 * variants) at the end of the alphabet. Hit the Set Rule button. This creates
 * a new collation with the name &quot;Custom-1&quot; (you could give it a
 * different name by typing in the Collator Name field). When you now look
 * at the Text field, you will see that you have changed the sequence to put
 * <I>Pat</I>
 * at the end. (If you did not have Sort Ascending on, click it
 * now.)</BLOCKQUOTE>
 * <BR>
 * Making P sort at the end may not seem terribly useful, but it is used to
 * make modifications in the sorting sequence for different languages.
 * <H4>To show...</H4>
 * <BLOCKQUOTE>For example, you can add CH as a single letter after C, as
 * in
 * traditional Spanish sorting.</BLOCKQUOTE>
 * <H4>Do...</H4>
 * <BLOCKQUOTE>Enter in the following after Z; &quot;&amp; C < ch , cH, Ch,
 * CH&quot;.
 * Hit the Set Rule button, type in test words in the Text field (such as
 * &quot;czar&quot;,
 * &quot;churo&quot; and &quot;darn&quot;), and select Sort Ascending to
 * see
 * the resulting sort order.</BLOCKQUOTE>
 * <H4>To show...</H4>
 * <BLOCKQUOTE>You can also add other sequences to the collation rules,
 * such as sorting symbols with their alphabetic equivalents.</BLOCKQUOTE>
 * <H4>Do...</H4>
 * <BLOCKQUOTE>1. Scroll to the end of the Sequence field. After the end,
 * type the following list (you can just select this text in your browser and
 * paste it in, to avoid typing). Now type lines in the Text field with these
 * symbols on them, and select Sort Ascending to see the resulting sort
 * order.</BLOCKQUOTE>
 * <UL>
 *   <UL>
 *     <LI>&amp; Asterisk ; *
 *     <LI>&amp; Question-mark ; ?
 *     <LI>&amp; Hash-mark ; #
 *     <LI>&amp; Exclamation-mark ; !
 *     <LI>&amp; Dollar-sign ; $
 *     <LI>&amp; Ampersand ; '&amp';
 *   </UL>
 * </UL>
 * <H4>Details</H4>
 * If you are an advanced user and interested in trying out more rules,
 * here is a brief explanation of how they work. The sequence is a list of
 * rules. Each rule is of two forms:
 * <UL>
 *   <LI>&lt;modifier&gt;
 *   <LI>&lt;relation&gt; &lt;text-argument&gt;
 *   <LI>&lt;reset&gt; &lt;text-argument&gt;
 * </UL>
 * <H5>Modifier</H5>
 * <BLOCKQUOTE>@ Indicates that accents are sorted backwards, as in
 * French</BLOCKQUOTE>
 * <H5>Text-argument</H5>
 * The text can be any number of characters (if you want to include special
 * characters, such as space, use single-quotes around them).
 * <H5>Relation</H5>
 * The relations are the following:
 * <DL>
 *   <DD>&lt;	Greater, as a letter difference (primary)
 *   <DD>;	Greater, as an accent difference (secondary)
 *   <DD>,	Greater, as a case difference (tertiary)
 *   <DD>=	Equal
 *   <DD>&amp;	Reset previous comparison.
 * </DL>
 * <H5>Reset</H5>
 * The &quot;&amp;&quot; is special in that does not put the text-argument
 * into the sorting sequence; instead, it indicates that the <I>next</I>
 * rule is with respect to where the text-argument <I>would be</I> sorted.
 * This sounds more complicated than it is in practice. For example, the
 * following are equivalent ways of expressing the same thing:
 * <UL>
 *   <LI>a &lt; b &lt; c
 *   <LI>a &lt; b &amp; b &lt; c
 *   <LI>a &lt; c &amp; a &lt; b
 * </UL>
 * Notice that the order is important, since the subsequent item goes
 * <I>immediately</I>
 * after the text-argument. The following are <I>not</I> equivalent:
 * <UL>
 *   <LI>a &lt; b &amp; a &lt; c
 *   <LI>a &lt; c &amp; a &lt; b
 * </UL>
 * The text-argument must already be present in the sequence, or some
 * initial substring of the text-argument must be present. (e.g. &quot;a &lt;
 * b&amp; ae &lt; e&quot; is valid since &quot;a&quot; is present in the
 * sequence<I>before</I> &quot;ae&quot; is reset). In this latter case,
 * &quot;ae&quot;
 * is <B><I>not</I></B> entered and treated as a single character; instead,
 * &quot;e&quot; is sorted as if it were expanded to two characters:
 * &quot;a&quot;
 * followed by an &quot;e&quot;.<BR>
 * This difference appears in natural languages: in traditional Spanish
 * &quot;ch&quot;
 * is treated as though it <I>contracts</I> to a single character
 * (expressed
 * as &quot;c &lt; ch &lt; d&quot;), while in traditional German
 * &quot;&auml;&quot;
 * (a-umlaut) is treated as though it <I>expands</I> to two characters
 * (expressed
 * as &quot;a &amp; ae ; &auml; &lt; b&quot;).<BR>
 * <H5>Ignorable Characters</H5>
 * The first rule must start with a relation (the examples we have used are
 * fragments; &quot;a &lt; b&quot; really should be &quot;&lt; a &lt;
 * b&quot;).
 * If, however, the first relation is not &quot;&lt;&quot;, then all the
 * all
 * text-arguments up to the first &quot;&lt;&quot; are ignorable. For
 * example,
 * &quot;, - &lt; a &lt; b&quot; makes &quot;-&quot; an ignorable
 * character,
 * as we saw earlier in the word &quot;black-birds&quot;.<BR>
 * <H5>Accents</H5>
 * The Collator automatically normalizes text internally to separate
 * accents
 * from base characters where possible. So, if you type in an
 * &quot;&auml;&quot;
 * (a-umlaut), after you reset the collation you will see
 * &quot;a\u0308&quot;
 * in the sequence, where \u0308 is the Java syntax for umlaut. The
 * demonstration
 * program uses this syntax instead of just showing the umlaut since many
 * browsers are unable to display the umlaut yet.<BR>
 * <H4>Errors</H4>
 * The following are errors:
 * <UL>
 *   <LI>Two relations in a row (e.g. &quot;a &lt; , b&quot;
 *   <LI>Two text arguments in a row (e.g. &quot;a &lt; b c &lt; d&quot;)
 *   <LI>A reset where the text-argument is not already in the sequence
 * (e.g.&quot;a &lt; b &amp; e &lt; f&quot;)
 * </UL>
 * If you produce one of these errors, then the demonstration will beep at
 * you, and select the offending text (note: on some browsers, the
 * selection will not appear correctly).
 * @version    1.1 11/23/96
 * @author     Kathleen Wilson, Helena Shih
 * @see        java.util.Collator
 * @see        java.util.RuleBasedCollator
 * @see        java.demos.utilities.DemoApplet
 */
public class CollateDemo extends DemoApplet
{
    /**
     * The main function which defines the behavior of the CollateDemo applet
     * when an applet is started.
     */
    public static void main(String argv[]) {
        DemoApplet.showDemo(new CollateFrame(null));
    }

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

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

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

    /**
     * Called if an action occurs in the CollateFrame object.
     */
    public boolean action(Event evt, Object obj)
    {
        return false;
    }

    /**
     * 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()
    {
        theLocale = Locale.US;
        theCollation = Collator.getInstance(theLocale);
        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()
    {
    }

  public void itemStateChanged(ItemEvent e) {
    errorText("");
    if (e.getSource() == sortAscending)
      handleSort(true, false);
    else if (e.getSource() == sortDescending)
      handleSort(false, true);
    else if (e.getSource() == localeChoice) {
      handleLocale();
      handleSort(sortAscending.getState(), sortDescending.getState());
    }
    else if (e.getSource() == decompChoice) {
      handleSort(sortAscending.getState(), sortDescending.getState());
    }
    else if (e.getSource() == strengthChoice) {
      handleSort(sortAscending.getState(), sortDescending.getState());
    }
  }

  public void keyPressed(KeyEvent e) {
    if (e.getSource() == textentry) {
      checkboxes.setSelectedCheckbox(noSort);
    }
  }

  public void keyReleased(KeyEvent e) {
  }

  public void keyTyped(KeyEvent e) {
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == collateRulesButton) {
      errorText("");
      collateRulesButton.setLabel("setting");
      handleSetRules();
      collateRulesButton.setLabel("Set Rules");
      handleSort(sortAscending.getState(),
                 sortDescending.getState());
    }
    
  }

  public void windowClosed(WindowEvent e) {
  }

  public void windowDeiconified(WindowEvent e) {
  }

  public void windowIconified(WindowEvent e) {
  }

  public void windowActivated(WindowEvent e) {
  }

  public void windowDeactivated(WindowEvent e) {
  }

  public void windowOpened(WindowEvent e) {
  }

  public void windowClosing(WindowEvent e) {
    setVisible(false);
    dispose();

    if (applet != null) {
      applet.demoClosed();
    } else System.exit(0);

  }
  

⌨️ 快捷键说明

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