ICU-1547 updated the demo for testing transliterator

X-SVN-Rev: 7028
This commit is contained in:
Mark Davis 2001-11-20 19:51:37 +00:00
parent 035d46a9a1
commit 48d1e321f2
3 changed files with 295 additions and 42 deletions

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/demo/translit/Attic/Demo.java,v $
* $Date: 2000/03/10 03:47:44 $
* $Revision: 1.4 $
* $Date: 2001/11/20 19:51:37 $
* $Revision: 1.5 $
*
*****************************************************************************************
*/
@ -27,18 +27,24 @@ import com.ibm.text.*;
* <p>Copyright (c) IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Demo.java,v $ $Revision: 1.4 $ $Date: 2000/03/10 03:47:44 $
* @version $RCSfile: Demo.java,v $ $Revision: 1.5 $ $Date: 2001/11/20 19:51:37 $
*/
public class Demo extends Frame {
static final boolean DEBUG = false;
Transliterator translit = null;
String fontName = "Arial Unicode MS";
int fontSize = 36;
/*
boolean compound = false;
Transliterator[] compoundTranslit = new Transliterator[MAX_COMPOUND];
static final int MAX_COMPOUND = 128;
int compoundCount = 0;
*/
TransliteratingTextComponent text = null;
@ -73,7 +79,7 @@ public class Demo extends Frame {
});
text = new TransliteratingTextComponent();
Font font = new Font("serif", Font.PLAIN, 48);
Font font = new Font(fontName, Font.PLAIN, fontSize);
text.setFont(font);
text.setSize(width, height);
text.setVisible(true);
@ -81,6 +87,10 @@ public class Demo extends Frame {
add(text);
setSize(width, height);
translit = Transliterator.getInstance("Latin-Greek");
text.setTransliterator(translit);
}
private void initMenus() {
@ -97,7 +107,7 @@ public class Demo extends Frame {
handleClose();
}
});
/*
final ItemListener setTransliteratorListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
CheckboxMenuItem item = (CheckboxMenuItem) e.getSource();
@ -117,9 +127,8 @@ public class Demo extends Frame {
}
}
};
translit = null;
mbar.add(translitMenu = new Menu("Transliterator"));
*/
/*
translitMenu.add(translitItem = noTranslitItem =
new CheckboxMenuItem(NO_TRANSLITERATOR, true));
noTranslitItem.addItemListener(new ItemListener() {
@ -130,7 +139,9 @@ public class Demo extends Frame {
});
translitMenu.addSeparator();
*/
/*
translitMenu.add(citem = new CheckboxMenuItem("Compound"));
citem.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
@ -153,13 +164,64 @@ public class Demo extends Frame {
});
translitMenu.addSeparator();
*/
/*
for (Enumeration e=getSystemTransliteratorNames().elements();
e.hasMoreElements(); ) {
String s = (String) e.nextElement();
translitMenu.add(citem = new CheckboxMenuItem(s));
citem.addItemListener(setTransliteratorListener);
}
*/
Menu fontMenu = new Menu("Font");
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; ++i) {
MenuItem mItem = new MenuItem(fonts[i]);
mItem.addActionListener(new FontActionListener(fonts[i]));
fontMenu.add(mItem);
}
mbar.add(fontMenu);
Menu sizeMenu = new Menu("Size");
for (double i = 9; i < 100; i = i * 4/3) {
MenuItem mItem = new MenuItem("" + (int)i);
mItem.addActionListener(new SizeActionListener((int)i));
sizeMenu.add(mItem);
}
mbar.add(sizeMenu);
translit = null;
mbar.add(translitMenu = new Menu("Transliterator"));
Iterator sources = add(new TreeSet(), Transliterator.getAvailableSources()).iterator();
while(sources.hasNext()) {
String source = (String) sources.next();
Iterator targets = add(new TreeSet(), Transliterator.getAvailableTargets(source)).iterator();
Menu targetMenu = new Menu(source);
while(targets.hasNext()) {
String target = (String) targets.next();
Set variantSet = add(new TreeSet(), Transliterator.getAvailableVariants(source, target));
if (variantSet.size() < 2) {
mitem = new MenuItem(target);
mitem.addActionListener(new TransliterationListener(source + "-" + target));
targetMenu.add(mitem);
} else {
Iterator variants = variantSet.iterator();
Menu variantMenu = new Menu(target);
while(variants.hasNext()) {
String variant = (String) variants.next();
mitem = new MenuItem(variant == "" ? "<default>" : variant);
mitem.addActionListener(new TransliterationListener(source + "-" + target + "/" + variant));
variantMenu.add(mitem);
}
targetMenu.add(variantMenu);
}
}
translitMenu.add(targetMenu);
}
mbar.add(menu = new Menu("Batch"));
menu.add(mitem = new MenuItem("Transliterate Selection"));
@ -169,10 +231,54 @@ public class Demo extends Frame {
}
});
}
class TransliterationListener implements ActionListener {
String name;
public TransliterationListener(String name) {
this.name = name;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Got: " + name);
translit = Transliterator.getInstance(name);
text.setTransliterator(translit);
}
}
class FontActionListener implements ActionListener {
String name;
public FontActionListener(String name) {
this.name = name;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Font: " + name);
fontName = name;
text.setFont(new Font(fontName, Font.PLAIN, fontSize));
}
}
class SizeActionListener implements ActionListener {
int size;
public SizeActionListener(int size) {
this.size = size;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Size: " + size);
fontSize = size;
text.setFont(new Font(fontName, Font.PLAIN, fontSize));
}
}
Set add(Set s, Enumeration enum) {
while(enum.hasMoreElements()) {
s.add(enum.nextElement());
}
return s;
}
/**
* Get a sorted list of the system transliterators.
*/
/*
private static Vector getSystemTransliteratorNames() {
Vector v = new Vector();
for (Enumeration e=Transliterator.getAvailableIDs();
@ -193,7 +299,9 @@ public class Demo extends Frame {
}
return v;
}
*/
/*
private void setNoTransliterator() {
translitItem = noTranslitItem;
noTranslitItem.setState(true);
@ -206,7 +314,8 @@ public class Demo extends Frame {
}
}
}
*/
/*
private void handleAddToCompound(String name) {
if (compoundCount < MAX_COMPOUND) {
compoundTranslit[compoundCount] = decodeTranslitItem(name);
@ -217,19 +326,23 @@ public class Demo extends Frame {
text.setTransliterator(translit);
}
}
*/
/*
private void handleSetTransliterator(String name) {
translit = decodeTranslitItem(name);
text.setTransliterator(translit);
}
*/
/**
* Decode a menu item that looks like <translit name>.
*/
/*
private static Transliterator decodeTranslitItem(String name) {
return (name.equals(NO_TRANSLITERATOR))
? null : Transliterator.getInstance(name);
}
*/
private void handleBatchTransliterate() {
if (translit == null) {

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/demo/translit/Demo.java,v $
* $Date: 2000/03/10 03:47:44 $
* $Revision: 1.4 $
* $Date: 2001/11/20 19:51:37 $
* $Revision: 1.5 $
*
*****************************************************************************************
*/
@ -27,18 +27,24 @@ import com.ibm.text.*;
* <p>Copyright (c) IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: Demo.java,v $ $Revision: 1.4 $ $Date: 2000/03/10 03:47:44 $
* @version $RCSfile: Demo.java,v $ $Revision: 1.5 $ $Date: 2001/11/20 19:51:37 $
*/
public class Demo extends Frame {
static final boolean DEBUG = false;
Transliterator translit = null;
String fontName = "Arial Unicode MS";
int fontSize = 36;
/*
boolean compound = false;
Transliterator[] compoundTranslit = new Transliterator[MAX_COMPOUND];
static final int MAX_COMPOUND = 128;
int compoundCount = 0;
*/
TransliteratingTextComponent text = null;
@ -73,7 +79,7 @@ public class Demo extends Frame {
});
text = new TransliteratingTextComponent();
Font font = new Font("serif", Font.PLAIN, 48);
Font font = new Font(fontName, Font.PLAIN, fontSize);
text.setFont(font);
text.setSize(width, height);
text.setVisible(true);
@ -81,6 +87,10 @@ public class Demo extends Frame {
add(text);
setSize(width, height);
translit = Transliterator.getInstance("Latin-Greek");
text.setTransliterator(translit);
}
private void initMenus() {
@ -97,7 +107,7 @@ public class Demo extends Frame {
handleClose();
}
});
/*
final ItemListener setTransliteratorListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
CheckboxMenuItem item = (CheckboxMenuItem) e.getSource();
@ -117,9 +127,8 @@ public class Demo extends Frame {
}
}
};
translit = null;
mbar.add(translitMenu = new Menu("Transliterator"));
*/
/*
translitMenu.add(translitItem = noTranslitItem =
new CheckboxMenuItem(NO_TRANSLITERATOR, true));
noTranslitItem.addItemListener(new ItemListener() {
@ -130,7 +139,9 @@ public class Demo extends Frame {
});
translitMenu.addSeparator();
*/
/*
translitMenu.add(citem = new CheckboxMenuItem("Compound"));
citem.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
@ -153,13 +164,64 @@ public class Demo extends Frame {
});
translitMenu.addSeparator();
*/
/*
for (Enumeration e=getSystemTransliteratorNames().elements();
e.hasMoreElements(); ) {
String s = (String) e.nextElement();
translitMenu.add(citem = new CheckboxMenuItem(s));
citem.addItemListener(setTransliteratorListener);
}
*/
Menu fontMenu = new Menu("Font");
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fonts.length; ++i) {
MenuItem mItem = new MenuItem(fonts[i]);
mItem.addActionListener(new FontActionListener(fonts[i]));
fontMenu.add(mItem);
}
mbar.add(fontMenu);
Menu sizeMenu = new Menu("Size");
for (double i = 9; i < 100; i = i * 4/3) {
MenuItem mItem = new MenuItem("" + (int)i);
mItem.addActionListener(new SizeActionListener((int)i));
sizeMenu.add(mItem);
}
mbar.add(sizeMenu);
translit = null;
mbar.add(translitMenu = new Menu("Transliterator"));
Iterator sources = add(new TreeSet(), Transliterator.getAvailableSources()).iterator();
while(sources.hasNext()) {
String source = (String) sources.next();
Iterator targets = add(new TreeSet(), Transliterator.getAvailableTargets(source)).iterator();
Menu targetMenu = new Menu(source);
while(targets.hasNext()) {
String target = (String) targets.next();
Set variantSet = add(new TreeSet(), Transliterator.getAvailableVariants(source, target));
if (variantSet.size() < 2) {
mitem = new MenuItem(target);
mitem.addActionListener(new TransliterationListener(source + "-" + target));
targetMenu.add(mitem);
} else {
Iterator variants = variantSet.iterator();
Menu variantMenu = new Menu(target);
while(variants.hasNext()) {
String variant = (String) variants.next();
mitem = new MenuItem(variant == "" ? "<default>" : variant);
mitem.addActionListener(new TransliterationListener(source + "-" + target + "/" + variant));
variantMenu.add(mitem);
}
targetMenu.add(variantMenu);
}
}
translitMenu.add(targetMenu);
}
mbar.add(menu = new Menu("Batch"));
menu.add(mitem = new MenuItem("Transliterate Selection"));
@ -169,10 +231,54 @@ public class Demo extends Frame {
}
});
}
class TransliterationListener implements ActionListener {
String name;
public TransliterationListener(String name) {
this.name = name;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Got: " + name);
translit = Transliterator.getInstance(name);
text.setTransliterator(translit);
}
}
class FontActionListener implements ActionListener {
String name;
public FontActionListener(String name) {
this.name = name;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Font: " + name);
fontName = name;
text.setFont(new Font(fontName, Font.PLAIN, fontSize));
}
}
class SizeActionListener implements ActionListener {
int size;
public SizeActionListener(int size) {
this.size = size;
}
public void actionPerformed(ActionEvent e) {
System.out.println("Size: " + size);
fontSize = size;
text.setFont(new Font(fontName, Font.PLAIN, fontSize));
}
}
Set add(Set s, Enumeration enum) {
while(enum.hasMoreElements()) {
s.add(enum.nextElement());
}
return s;
}
/**
* Get a sorted list of the system transliterators.
*/
/*
private static Vector getSystemTransliteratorNames() {
Vector v = new Vector();
for (Enumeration e=Transliterator.getAvailableIDs();
@ -193,7 +299,9 @@ public class Demo extends Frame {
}
return v;
}
*/
/*
private void setNoTransliterator() {
translitItem = noTranslitItem;
noTranslitItem.setState(true);
@ -206,7 +314,8 @@ public class Demo extends Frame {
}
}
}
*/
/*
private void handleAddToCompound(String name) {
if (compoundCount < MAX_COMPOUND) {
compoundTranslit[compoundCount] = decodeTranslitItem(name);
@ -217,19 +326,23 @@ public class Demo extends Frame {
text.setTransliterator(translit);
}
}
*/
/*
private void handleSetTransliterator(String name) {
translit = decodeTranslitItem(name);
text.setTransliterator(translit);
}
*/
/**
* Decode a menu item that looks like <translit name>.
*/
/*
private static Transliterator decodeTranslitItem(String name) {
return (name.equals(NO_TRANSLITERATOR))
? null : Transliterator.getInstance(name);
}
*/
private void handleBatchTransliterate() {
if (translit == null) {

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/text/components/Attic/TransliteratingTextComponent.java,v $
* $Date: 2000/06/28 20:36:45 $
* $Revision: 1.6 $
* $Date: 2001/11/20 19:51:37 $
* $Revision: 1.7 $
*
*****************************************************************************************
*/
@ -25,21 +25,23 @@ import com.ibm.text.*;
* <p>Copyright &copy; IBM Corporation 1999. All rights reserved.
*
* @author Alan Liu
* @version $RCSfile: TransliteratingTextComponent.java,v $ $Revision: 1.6 $ $Date: 2000/06/28 20:36:45 $
* @version $RCSfile: TransliteratingTextComponent.java,v $ $Revision: 1.7 $ $Date: 2001/11/20 19:51:37 $
*/
public class TransliteratingTextComponent extends DumbTextComponent {
private static boolean DEBUG = false;
private static boolean DEBUG = true;
private Transliterator translit = null;
// NOTE: DISABLE THE START AND CURSOR UNTIL WE CAN GET IT TO WORK AT ALL
// Index into getText() where the start of transliteration is.
// As we commit text during transliteration, we advance
// this.
private int start = 0;
//private int start = 0;
// Index into getText() where the cursor is; cursor >= start
private int cursor = 0;
//private int cursor = 0;
private static final String COPYRIGHT =
"\u00A9 IBM Corporation 1999. All rights reserved.";
@ -49,12 +51,14 @@ public class TransliteratingTextComponent extends DumbTextComponent {
*/
public TransliteratingTextComponent() {
super();
/*
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// We get an ActionEvent only when the selection changes
resetTransliterationStart();
}
});
*/
}
/**
@ -98,27 +102,44 @@ public class TransliteratingTextComponent extends DumbTextComponent {
// Need to save start because calls to replaceRange will update
// start and cursor.
int saveStart = start;
//int saveStart = start;
ReplaceableString buf = new ReplaceableString();
buf.replace(0, 1, getText().substring(start,
getSelectionStart()));
String sourceText = getText().substring(0,getSelectionStart());
ReplaceableString buf = new ReplaceableString(sourceText);
/*buf.replace(0, 1, getText().substring(start,
getSelectionStart()));*/
Transliterator.Position index =
new Transliterator.Position(0, getSelectionStart() - start,
cursor - start);
Transliterator.Position index = new Transliterator.Position();
index.contextLimit = buf.length();
index.contextStart = 0;
index.start = 0;
index.limit = buf.length();;
StringBuffer log = null;
if (DEBUG) {
log = new StringBuffer();
log.append("start " + start + ", cursor " + cursor);
log.append(", sel " + getSelectionStart());
log.append(", {" + index.contextStart + ", " + index.contextLimit + ", " + index.start + "}, ");
log.append('"' + buf.toString() + "\" + '" + ch + "' -> \"");
System.out.println("Transliterator: " + translit.getID());
System.out.println("From:\t" + '"' + buf.toString() + '"'
+ "; {cs: " + index.contextStart + ", cl: " + index.contextLimit + ", s: " + index.start + ", l: " + index.limit + "}"
+ "; '" + ch + "'");
}
translit.transliterate(buf, index, ch);
replaceRange(buf.toString(), start, getSelectionEnd());
if (DEBUG) {
System.out.println("To:\t" + '"' + buf.toString() + '"'
+ "; {cs: " + index.contextStart + ", cl: " + index.contextLimit + ", s: " + index.start + ", l: " + index.limit + "}"
);
System.out.println();
}
/*
buf.replace(buf.length(), buf.length(), String.valueOf(ch));
translit.transliterate(buf);
*/
String result = buf.toString();
//if (result.equals(sourceText + ch)) return;
replaceRange(result, 0, getSelectionEnd());
// At this point start has been changed by the callback to
// resetTransliteratorStart() via replaceRange() -- so use our
// local copy, saveStart.
@ -127,11 +148,12 @@ public class TransliteratingTextComponent extends DumbTextComponent {
// it was zero. We can therefore just add it to our original
// getText()-based index value of start (in saveStart) to get
// the new getText()-based start.
start = saveStart + index.contextStart;
// start = saveStart + index.contextStart;
// Make the cursor getText()-based. The CURSOR index is zero-based.
cursor = start + index.start - index.contextStart;
// cursor = start + index.start - index.contextStart;
/*
if (DEBUG) {
String out = buf.toString();
log.append(out.substring(0, index.contextStart)).
@ -141,10 +163,11 @@ public class TransliteratingTextComponent extends DumbTextComponent {
append(out.substring(index.start)).
append('"');
log.append(", {" + index.contextStart + ", " + index.contextLimit + ", " + index.start + "}, ");
log.append("start " + start + ", cursor " + cursor);
// log.append("start " + start + ", cursor " + cursor);
log.append(", sel " + getSelectionStart());
System.out.println(escape(log.toString()));
}
*/
}
/**
@ -153,9 +176,11 @@ public class TransliteratingTextComponent extends DumbTextComponent {
* @param t the {@link com.ibm.text.Transliterator} to use
*/
public void setTransliterator(Transliterator t) {
/*
if (translit != t) { // [sic] pointer compare ok; singletons
resetTransliterationStart();
}
*/
translit = t;
}
@ -164,10 +189,12 @@ public class TransliteratingTextComponent extends DumbTextComponent {
* needs to be done when the user moves the cursor or when the
* current {@link com.ibm.text.Transliterator} is changed.
*/
/*
private void resetTransliterationStart() {
start = getSelectionStart();
cursor = start;
}
*/
/**
* Escape non-ASCII characters as Unicode.