📄 collatedemo.java
字号:
/* * @(#)CollateDemo.java 1.1 96/11/23 * * (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 * "< p , P". 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 "Custom-1" (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; "& C < ch , cH, Ch, * CH". * Hit the Set Rule button, type in test words in the Text field (such as * "czar", * "churo" and "darn"), 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>& Asterisk ; * * <LI>& Question-mark ; ? * <LI>& Hash-mark ; # * <LI>& Exclamation-mark ; ! * <LI>& Dollar-sign ; $ * <LI>& Ampersand ; '&'; * </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><modifier> * <LI><relation> <text-argument> * <LI><reset> <text-argument> * </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>< Greater, as a letter difference (primary) * <DD>; Greater, as an accent difference (secondary) * <DD>, Greater, as a case difference (tertiary) * <DD>= Equal * <DD>& Reset previous comparison. * </DL> * <H5>Reset</H5> * The "&" 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 < b < c * <LI>a < b & b < c * <LI>a < c & a < 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 < b & a < c * <LI>a < c & a < 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. "a < * b& ae < e" is valid since "a" is present in the * sequence<I>before</I> "ae" is reset). In this latter case, * "ae" * is <B><I>not</I></B> entered and treated as a single character; instead, * "e" is sorted as if it were expanded to two characters: * "a" * followed by an "e".<BR> * This difference appears in natural languages: in traditional Spanish * "ch" * is treated as though it <I>contracts</I> to a single character * (expressed * as "c < ch < d"), while in traditional German * "ä" * (a-umlaut) is treated as though it <I>expands</I> to two characters * (expressed * as "a & ae ; ä < b").<BR> * <H5>Ignorable Characters</H5> * The first rule must start with a relation (the examples we have used are * fragments; "a < b" really should be "< a < * b"). * If, however, the first relation is not "<", then all the * all * text-arguments up to the first "<" are ignorable. For * example, * ", - < a < b" makes "-" an ignorable * character, * as we saw earlier in the word "black-birds".<BR> * <H5>Accents</H5> * The Collator automatically normalizes text internally to separate * accents * from base characters where possible. So, if you type in an * "ä" * (a-umlaut), after you reset the collation you will see * "a\u0308" * 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. "a < , b" * <LI>Two text arguments in a row (e.g. "a < b c < d") * <LI>A reset where the text-argument is not already in the sequence * (e.g."a < b & e < f") * </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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -