collatedemo.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,001 行 · 第 1/3 页
JAVA
1,001 行
/**
* This function is called when you press the "SortDescend" button.
* It does an extremely simple sort, using the Collator.compare function.
* To improve performance, you could transform the strings into
* a sort key which is a series of characters that can be
* compared using bit-wise comparison. In addition, you can also use
* collation keys to compare two strings based on the collation rules.
* @see java.util.Collator#compare
* @see java.util.Collator#getSortKey
* @see java.util.Collator#getCollationKey
*/
public boolean handleSort(boolean ascending, boolean descending) {
if (ascending == descending)
return true;
int exchangeResult = ascending ? -1 : 1;
InitializeListVector();
String targetItem;
String sourceItem;
byte compareResult;
String strengthName = strengthChoice.getSelectedItem();
if (strengthName.equals(tertiary)) {
theCollation.setStrength(Collator.TERTIARY);
}else if (strengthName.equals(secondary)) {
theCollation.setStrength(Collator.SECONDARY);
} else theCollation.setStrength(Collator.PRIMARY);
int decompItem = decompChoice.getSelectedIndex();
if (decompItem == 0) {
theCollation.setDecomposition(Collator.NO_DECOMPOSITION);
}else if (decompItem == 1) {
theCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
} else theCollation.setDecomposition(Collator.FULL_DECOMPOSITION);
int numItems = textList.size();
for (int sourceIndex = 1; sourceIndex < numItems; sourceIndex++)
{
sourceItem = (String) textList.elementAt(sourceIndex);
for (int targetIndex = 0; targetIndex < sourceIndex; targetIndex++)
{
targetItem = (String) textList.elementAt(targetIndex);
compareResult = (byte) theCollation.compare(sourceItem, targetItem);
if (compareResult == exchangeResult)
{
textList.removeElementAt(sourceIndex);
textList.insertElementAt(sourceItem, targetIndex);
break;
}
}
}
resetTextArea();
return true;
}
/**
* This function is called when you press the Locale button.
* It sets the locale that is to be used to create the collation object.
* @see java.util.Collator#getInstance
*/
public boolean handleLocale()
{
int index = localeChoice.getSelectedIndex();
theCollation = collations[index];
String theRules = ruleStrings[index];
if (theRules != null)
{
ruleEntry.setText(createUnicodeString
(((RuleBasedCollator)theCollation).getRules()));
} else ruleEntry.setText("");
return true;
}
/**
* This function is called when you press the "Set Rules" button.
* It sets the rules that are to be used by the current collation object.
* @see java.util.RuleBasedCollator#getRules
*/
public void handleSetRules()
{
int index = localeChoice.getSelectedIndex();
String rules = ruleEntry.getText();
if ((rules.equals(ruleStrings[index]) == false)
&& (theCollation instanceof RuleBasedCollator) )
{
int idx = 0;
try
{
String collName = collationName.getText();
for (int n = 0; n < localeChoice.getItemCount(); n++)
{
if (collName.equals(localeChoice.getItem(n)))
{
theCollation = new
RuleBasedCollator(convertStringToRules(rules));
collations[n] = theCollation;
idx = n;
break;
}
}
if (idx == 0)
{
if (collName.startsWith("custom-"))
{
untitledIndex++;
}
if (localeChoice.getItemCount() < MAX_COLLATIONS)
{
idx = localeChoice.getItemCount();
theCollation = new
RuleBasedCollator(convertStringToRules(rules));
collations[idx] = theCollation;
localeChoice.addItem( collName );
}
else
{
throw new ParseException("Max # exceeded!" +
"Please replace an existing one.", 0);
}
}
localeChoice.select(idx);
ruleStrings[idx] = rules;
ruleEntry.setText(createUnicodeString
(((RuleBasedCollator)theCollation).getRules()));
if (localeChoice.getItemCount() < MAX_COLLATIONS)
collationName.setText("custom-" + untitledIndex);
}
catch (ParseException foo)
{
collateRulesButton.setLabel("Set Rules");
errorText(foo.getMessage());
}
}
}
/**
* Must be called after the array of locales has been initialized.
*/
public void loadCollationTables()
{
MAX_COLLATIONS = locales.length + 10;
collations = new Collator[MAX_COLLATIONS];
ruleStrings = new String[MAX_COLLATIONS];
for (int i = 0; i < locales.length; i++)
{
Locale currentLocale = locales[i];
Collator newcollation = null;
//If the collation for this language has already
//been created, then use it.
int j = 0;
for (; j < i; j++)
{
if (currentLocale.getLanguage() == locales[j].getLanguage())
{
newcollation = collations[j];
break;
}
}
if (newcollation == null)
{
collations[i] = Collator.getInstance(currentLocale);
if (collations[i] instanceof RuleBasedCollator)
{
String ruleText = ((RuleBasedCollator)
collations[i]).getRules();
ruleStrings[i] = createUnicodeString(ruleText);
}
}else {
collations[i] = newcollation;
ruleStrings[i] = ruleStrings[j];
}
}
}
//------------------------------------------------------------
// package private
//------------------------------------------------------------
Panel makePanel(Component demo, Component code) {
Panel temp = new Panel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
temp.setLayout(gridbag);
constraints.anchor = GridBagConstraints.NORTHWEST;
constraints.gridwidth = GridBagConstraints.REMAINDER; //end row
gridbag.setConstraints(demo, constraints);
constraints.gridheight = GridBagConstraints.REMAINDER; //end column
gridbag.setConstraints(code, constraints);
temp.add(demo);
temp.add(code);
return temp;
}
void addWithFont(Container container, Component foo, Font font) {
if (font != null)
foo.setFont(font);
container.add(foo);
}
void buildGUI()
{
setBackground(Color.white);
setLayout(new BorderLayout());
// TITLE
Label title=new Label("Collate Demo", Label.CENTER);
title.setFont(Utility.titleFont);
Label demo=new Label(creditString, Label.CENTER);
demo.setFont(Utility.creditFont);
Panel titlePanel = new Panel();
titlePanel.add(title);
titlePanel.add(demo);
Utility.fixGrid(titlePanel,1);
add("North", titlePanel);
// CHECKBOXES
checkboxes= new CheckboxGroup();
sortAscending = new Checkbox("Sort Ascending",checkboxes, false);
sortAscending.addItemListener(this);
sortAscending.setFont(Utility.choiceFont);
sortAscending.setSize(100, 20);
sortDescending = new Checkbox("Sort Descending",checkboxes, false);
sortDescending.addItemListener(this);
sortDescending.setSize(100, 20);
sortDescending.setFont(Utility.choiceFont);
noSort = new Checkbox("Not Sorted",checkboxes, true);
noSort.setFont(Utility.choiceFont);
noSort.setSize(100, 20);
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(3,1,0,0));
buttonPanel.add(sortAscending);
buttonPanel.add(sortDescending);
buttonPanel.add(noSort);
// LOCALE
Locale myLocale = new Locale("","","");
Label localeLabel = new Label("Locale: ");
localeLabel.setFont(Utility.choiceFont);
localeChoice = new Choice();
localeChoice.addItemListener(this);
//Get all locales for debugging, but only get G7 locales for demos.
if (DEBUG == true)
locales = Collator.getAvailableLocales();
else locales = Utility.getG7Locales();
Locale displayLocale = null;
displayLocale = Locale.getDefault();
int defaultLoc = 0;
for (int i = 0; i < locales.length; i++) {
if (locales[i].getCountry().length() > 0) {
localeChoice.addItem
(locales[i].getDisplayName());
if (locales[i].equals(displayLocale)) {
defaultLoc = i;
}
}
}
localeChoice.setFont(Utility.choiceFont);
//must be called after the array of locales has been initialized
loadCollationTables();
localeChoice.select(defaultLoc);
Panel localePanel = new Panel();
localePanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
localePanel.add(localeChoice);
// localePanel.setSize(200, 20);
Label decompLabel = new Label("Decomposition Mode:");
decompLabel.setFont(Utility.labelFont);
decompChoice = new Choice();
decompChoice.addItemListener(this);
decompChoice.addItem(no_decomposition);
decompChoice.addItem(canonical_decomposition);
decompChoice.addItem(full_decomposition);
decompChoice.select(canonical_decomposition);
decompChoice.setFont(Utility.choiceFont);
Panel decompPanel = new Panel();
decompPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0,0));
decompPanel.add(decompChoice);
Label strengthLabel = new Label("Strength:");
strengthLabel.setFont(Utility.labelFont);
strengthChoice = new Choice();
strengthChoice.addItemListener(this);
strengthChoice.addItem(tertiary);
strengthChoice.addItem(secondary);
strengthChoice.addItem(primary);
strengthChoice.setFont(Utility.choiceFont);
Panel strengthPanel = new Panel();
strengthPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
strengthPanel.add(strengthChoice);
Panel topPanel = new Panel();
//topPanel.setLayout(new GridLayout(4,1,0,0));
topPanel.add(buttonPanel);
topPanel.add(new Label(" ")); // quick & dirty gap
topPanel.add(localeLabel);
topPanel.add(localePanel);
topPanel.add(decompLabel);
topPanel.add(decompPanel);
topPanel.add(strengthLabel);
topPanel.add(strengthPanel);
Utility.fixGrid(topPanel,1);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?