import java.util.Locale;
import com.ibm.icu.dev.demo.impl.DemoApplet;
+import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.text.RuleBasedNumberFormat;
public class RbnfDemo extends DemoApplet {
new RbnfDemo().showDemo();
}
+ @Override
protected Dimension getDefaultFrameSize(DemoApplet applet, Frame f) {
return new Dimension(430,270);
}
+ @Override
protected Frame createDemoFrame(DemoApplet applet) {
final Frame window = new Frame("Number Spellout Demo");
window.setSize(800, 600);
populateRuleSetMenu();
numberFormatter = new DecimalFormat("#,##0.##########");
parsePosition = new ParsePosition(0);
- theNumber = 0;
+ theNumber = BigDecimal.ZERO;
numberField = new TextField();
numberField.setFont(new Font("Serif", Font.PLAIN, 24));
lenientParseButton = new Checkbox("Lenient parse", lenientParse);
numberField.addTextListener(new TextListener() {
+ @Override
public void textValueChanged(TextEvent e) {
if (!numberFieldHasFocus)
return;
parsePosition.setIndex(0);
Number temp = numberFormatter.parse(fieldText, parsePosition);
if (temp == null || parsePosition.getIndex() == 0) {
- theNumber = 0;
+ theNumber = BigDecimal.ZERO;
textField.setText("PARSE ERROR");
}
else {
- theNumber = temp.doubleValue();
- textField.setText(spelloutFormatter.format(theNumber, ruleSetName));
+ theNumber = new BigDecimal(fieldText);
+ textField.setText(spelloutFormatter.format(theNumber.doubleValue(), ruleSetName));
}
}
} );
numberField.addFocusListener(new FocusAdapter() {
+ @Override
public void focusLost(FocusEvent e) {
numberFieldHasFocus = false;
numberField.setText(numberFormatter.format(theNumber));
}
+ @Override
public void focusGained(FocusEvent e) {
numberFieldHasFocus = true;
numberField.selectAll();
} );
textField.addKeyListener(new KeyAdapter() {
+ @Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\t') {
String fieldText = ((TextComponent)(e.getSource())).getText();
parsePosition.setIndex(0);
- theNumber = spelloutFormatter.parse(fieldText, parsePosition)
- .doubleValue();
+ theNumber = new BigDecimal(spelloutFormatter.parse(fieldText, parsePosition)
+ .doubleValue());
if (parsePosition.getIndex() == 0) {
- theNumber = 0;
+ theNumber = BigDecimal.ZERO;
numberField.setText("PARSE ERROR");
textField.selectAll();
}
} );
textField.addFocusListener(new FocusAdapter() {
+ @Override
public void focusLost(FocusEvent e) {
String fieldText = ((TextComponent)(e.getSource())).getText();
parsePosition.setIndex(0);
- theNumber = spelloutFormatter.parse(fieldText, parsePosition)
- .doubleValue();
+ theNumber = new BigDecimal(spelloutFormatter.parse(fieldText, parsePosition)
+ .doubleValue());
if (parsePosition.getIndex() == 0)
numberField.setText("PARSE ERROR");
else
textField.setText(textField.getText()); // textField.repaint() didn't work right
}
+ @Override
public void focusGained(FocusEvent e) {
textField.selectAll();
}
} );
rulesField.addKeyListener(new KeyAdapter() {
+ @Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == '\t') {
String fieldText = ((TextComponent)(e.getSource())).getText();
} );
rulesField.addFocusListener(new FocusAdapter() {
+ @Override
public void focusLost(FocusEvent e) {
String fieldText = ((TextComponent)(e.getSource())).getText();
if (formatterMenu.getSelectedItem().equals("Custom") || !fieldText.equals(
} );
lenientParseButton.addItemListener(new ItemListener() {
+ @Override
public void itemStateChanged(ItemEvent e) {
lenientParse = lenientParseButton.getState();
spelloutFormatter.setLenientParseMode(lenientParse);
numberField.setText(numberFormatter.format(theNumber));
numberField.selectAll();
- textField.setText(spelloutFormatter.format(theNumber, ruleSetName));
+ textField.setText(spelloutFormatter.format(theNumber.doubleValue(), ruleSetName));
Panel leftPanel = new Panel();
leftPanel.setLayout(new BorderLayout());
panel2.setLayout(new GridLayout(3, 3));
Button button = new Button("+100");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(100);
}
panel2.add(button);
button = new Button("+10");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(10);
}
panel2.add(button);
button = new Button("+1");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(1);
}
panel2.add(button);
button = new Button("<");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
- theNumber *= 10;
+ theNumber = new BigDecimal(theNumber.toString()).multiply(new BigDecimal("10"));
redisplay();
}
} );
panel2.add(new Panel());
button = new Button(">");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
- theNumber /= 10;
+ theNumber = new BigDecimal(theNumber.toString()).multiply(new BigDecimal("0.1"));
redisplay();
}
} );
panel2.add(button);
button = new Button("-100");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(-100);
}
panel2.add(button);
button = new Button("-10");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(-10);
}
panel2.add(button);
button = new Button("-1");
button.addActionListener( new ActionListener() {
+ @Override
public void actionPerformed(ActionEvent e) {
roll(-1);
}
formatterMenu.addItem(RbnfSampleRuleSets.sampleRuleSetNames[i]);
formatterMenu.addItem("Custom");
formatterMenu.addItemListener(new ItemListener() {
+ @Override
public void itemStateChanged(ItemEvent e) {
Choice source = (Choice)(e.getSource());
int item = source.getSelectedIndex();
populateRuleSetMenu();
ruleSetMenu.addItemListener(new ItemListener() {
+ @Override
public void itemStateChanged(ItemEvent e) {
ruleSetName = ruleSetMenu.getSelectedItem();
redisplay();
final DemoApplet theApplet = applet;
window.addWindowListener(
new WindowAdapter() {
+ @Override
public void windowClosing(WindowEvent e) {
setVisible(false);
window.dispose();
}
void roll(int delta) {
- theNumber += delta;
+ theNumber = theNumber.add(new BigDecimal(delta));
redisplay();
}
void redisplay() {
numberField.setText(numberFormatter.format(theNumber));
- textField.setText(spelloutFormatter.format(theNumber, ruleSetName));
+ textField.setText(spelloutFormatter.format(theNumber.doubleValue(), ruleSetName));
}
void makeNewSpelloutFormatter() {
private boolean lenientParse = true;
- private double theNumber = 0;
+ private BigDecimal theNumber = BigDecimal.ZERO;
// private boolean canEdit = true;
private Choice formatterMenu;
return text;
}
+ @Override
public void paint(Graphics g) {
Font font = getFont();
FontMetrics fm = g.getFontMetrics();
add(sp, "ScrollPane");
}
+ @Override
public void addFocusListener(FocusListener l) {
tf1.addFocusListener(l);
}
+ @Override
public void addKeyListener(KeyListener l) {
tf1.addKeyListener(l);
}