collatedemo.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,001 行 · 第 1/3 页
JAVA
1,001 行
// TEXT
textentry.addKeyListener(this);
textentry.setFont(editFont);
textentry.setText("black-birds\nPat\np\u00E9ch\u00E9\np\u00EAche\n" +
"p\u00E9cher\np\u00EAcher\nTod\nT\u00F6ne\nTofu\nblackbirds\n" +
"Ton\nPAT\nblackbird\nblack-bird\npat\n");
//RULE ENTRY AREA
Panel ruleEntryPanel = new Panel();
Panel rulePanel = new Panel();
if (theCollation instanceof RuleBasedCollator)
{
String ruleText = ((RuleBasedCollator)theCollation).getRules();
ruleEntry.setFont(ruleFont);
ruleEntry.setText(createUnicodeString(ruleText));
}
ruleEntryPanel.add(new Label("Collator Rules", Label.LEFT));
ruleEntryPanel.add(ruleEntry);
collateRulesButton = new Button("Set Rules");
collateRulesButton.addActionListener(this);
collationName = new TextField(10);
collationName.setText("custom-" + untitledIndex);
ruleEntryPanel.add(collateRulesButton);
ruleEntryPanel.add(new Label("Collator Name", Label.LEFT));
ruleEntryPanel.add(collationName);
Utility.fixGrid(ruleEntryPanel,1);
// PUT ALL TOGETHER
Panel centerPanel = new Panel();
centerPanel.add(textentry);
centerPanel.add(topPanel);
centerPanel.add(ruleEntryPanel);
errorMsg.setFont(Utility.labelFont);
centerPanel.add(errorMsg);
Utility.fixGrid(centerPanel,3);
add("Center", centerPanel);
Panel bottomPanel = new Panel();
bottomPanel.setLayout(new GridLayout(2,1,0,0));
addWithFont (bottomPanel,
new Label(copyrightString, Label.LEFT),
Utility.creditFont);
addWithFont (bottomPanel,
new Label(copyrightString2, Label.LEFT),
Utility.creditFont);
Utility.fixGrid(bottomPanel,1);
add("South", bottomPanel);
}
//------------------------------------------------------------
// private
//------------------------------------------------------------
/**
* This function is called by handleSortAscend and handleSortDescend
* to seperate each line of the textArea into individual strings,
* and add them to the vector to be sorted.
*/
private void InitializeListVector()
{
textList.removeAllElements();
String composite = textentry.getText();
char compositeArray[] = composite.toCharArray();
for (int i = 0; i < compositeArray.length; i++)
{
compositeArray[i] = (char) (compositeArray[i] & 0xFF);
}
composite = new String(compositeArray);
String substr;
int startOffset = 0;
int endOffset = 0;
for (; endOffset < composite.length(); endOffset++)
{
char ch = composite.charAt(endOffset);
if (ch == '\n' || ch == '\r')
{
if (endOffset > startOffset)
{
substr = composite.substring(startOffset, endOffset);
textList.addElement(substr);
}
startOffset = endOffset + 1;
}
}
if (startOffset < composite.length())
{
substr = composite.substring(startOffset, composite.length());
textList.addElement(substr);
}
}
/**
* This function is called by handleSortAscend and handleSortDescend
* to reset the text area based on the sort results stored as a vector
* of substrings.
*/
private void resetTextArea()
{
String composite = new String();
int i = 0;
for (; i < textList.size() - 1; i++)
{
composite = composite.concat((String) textList.elementAt(i));
composite = composite.concat("\n");
}
//don't add the \n to the end
composite = composite.concat((String) textList.elementAt(i));
textentry.setText(composite);
textList.removeAllElements();
}
private void errorText(String s)
{
if (!s.equals(errorMsg.getText()))
{
errorMsg.setText(s);
errorMsg.setSize(300, 50);
}
}
private static StringBuffer oneChar = new StringBuffer(7);
private String makeDisplayString(char ch) {
oneChar.setLength(0);
if (ch < 0x0020 ||
(ch > 0x007D && ch < 0x00A0) ||
ch > 0x00FF) {
String temp = Integer.toString((int)ch,16).toUpperCase();
oneChar.append(temp);
if (temp.length() < 4)
oneChar.append(zeros.substring(0,4-temp.length()));
} else {
oneChar.append(ch);
}
return oneChar.toString();
}
/**
Changes a string into a representation that can be pasted into a program.
Uses \ t, \ uxxxx, etc. to display characters outside of Latin1.
Others are themselves.
*/
private String createUnicodeString(String source)
{
StringBuffer result = new StringBuffer(source.length());
int len = 0;
int i = 0;
while (i < source.length()) {
char ch = source.charAt(i);
switch(ch) {
case '\'':
int mytemp = i;
int n = 0;
result.append(ch);
ch = source.charAt(++i);
while((ch != '\'') && (i < source.length() -1))
{
String inquote = makeDisplayString(ch);
n++; i++;
if (inquote.length() > 1) {
result.append("\\u");
result.append(inquote);
len += 6;
} else {
result.append(inquote);
len++;
}
ch = source.charAt(i);
}
if (n == 0) {
result.append('\'');
n++; i++;
ch = source.charAt(i);
}
result.append(ch);
len += n;
break;
case ';': case '=':
if (len > 15)
{
result.append('\n');
len = 0;
}
result.append(' ');
result.append(ch);
len += 2;
break;
case ',':
result.append(' ');
result.append(ch);
len += 2;
break;
case '<': case '&':
result.append('\n');
result.append(ch);
result.append(' ');
len = 0;
len += 2;
break;
case '\n':
break;
default:
String dspString = makeDisplayString(ch);
if (dspString.length() > 1) {
result.append("\\u");
result.append(dspString);
len += 6;
} else {
result.append(dspString);
len++;
}
break;
}
i++;
}
return result.toString();
}
private static final String zeros = "0000";
private String convertStringToRules(String source)
{
StringBuffer result = new StringBuffer(source.length());
for (int i = 0; i < source.length(); ++i) {
//hack around TextArea bug
char ch = (char) (source.charAt(i) & 0xFF);
switch(ch) {
case '\n':
break;
case '\'':
if ((i+6 < source.length()) && (source.charAt(i+2) == 'u')) {
String temp = source.substring(i+3,i+7);
if (temp.equals("005c")) {
//have to handle backslash differently since
//it is a special character in Java
result.append("'\\u005c'");
} else
result.append((char)(Integer.parseInt(temp,16)));
i = i + 7;
}
else result.append(ch);
break;
case '\\':
if ( (i+5 < source.length()) && (source.charAt(i+1) == 'u') )
{
String temp = source.substring(i+2,i+6);
result.append((char)(Integer.parseInt(temp,16)));
i = i + 6;
}
else result.append(ch);
break;
default:
result.append(ch);
break;
}
}
return result.toString();
}
private static final String creditString
= "v1.1a7, Demo";
private static final String copyrightString
= "";
private static final String copyrightString2
= "";
// HSYS : fix
private static int MAX_COLLATIONS = 12;
private int untitledIndex = 1;
private static final int FIELD_COLUMNS = 45;
private static final Font editFont = new Font("TimesRoman",Font.PLAIN,18);
private static final Font ruleFont = new Font("TimesRoman",Font.BOLD,14);
private static final boolean DEBUG = false;
private TextArea textentry = new TextArea(10, 10);
private TextArea ruleEntry = new TextArea(10, 20);
private Vector textList = new Vector(15, 10);
private Choice localeChoice;
private static final String no_decomposition = "None";
private static final String canonical_decomposition = "Canonical";
private static final String full_decomposition = "Full";
private Choice decompChoice;
private static final String tertiary = "Tertiary - a vs. A";
private static final String secondary = "Secondary - a vs. \u00E0";
private static final String primary = "Primary - a vs. b";
private Choice strengthChoice;
private Locale theLocale = null;
private Collator theCollation = null;
private Locale[] locales;
private Collator[] collations;
private String[] ruleStrings;
private Label errorMsg = new Label("", Label.LEFT);
CheckboxGroup checkboxes;
Checkbox sortAscending;
Checkbox sortDescending;
Checkbox noSort;
Button collateRulesButton;
TextField collationName;
private DemoApplet applet;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?