KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > composite > CurrencyEditComposite


1 /*
2  * Created on Jul 10, 2005
3  */

4 package com.nightlabs.rcp.composite;
5
6 import java.text.ParseException JavaDoc;
7
8 import org.eclipse.jface.dialogs.MessageDialog;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyEvent;
11 import org.eclipse.swt.events.ModifyListener;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 import com.nightlabs.l10n.Currency;
17 import com.nightlabs.l10n.NumberFormatter;
18
19 /**
20  * @author Marco Schulze - marco at nightlabs dot de
21  */

22 public class CurrencyEditComposite extends TightWrapperComposite
23 {
24     private Currency currency;
25     private Text numberText;
26     private long value;
27
28     /**
29      * @param parent
30      */

31     public CurrencyEditComposite(Composite parent, Currency _currency)
32     {
33         super(parent, SWT.NONE, true);
34         this.currency = _currency;
35
36         getGridLayout().numColumns = 2;
37         numberText = new Text(this, SWT.BORDER);
38         setValue(0);
39         new Label(this, SWT.NONE).setText(currency.getCurrencySymbol());
40
41         numberText.addModifyListener(new ModifyListener(){
42             /**
43              * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
44              */

45             public void modifyText(ModifyEvent e)
46             {
47                 String JavaDoc s = numberText.getText();
48                 try {
49                     value = NumberFormatter.parseCurrency(s, currency, false);
50                 } catch (ParseException JavaDoc x) {
51                     MessageDialog.openError(getShell(), "Invalid text!", "The text you entered is not a valid number: " + x.getLocalizedMessage());
52                 }
53             }
54         });
55     }
56
57     public void setValue(long currencyValue)
58     {
59         this.value = currencyValue;
60         numberText.setText(
61                 NumberFormatter.formatCurrency(currencyValue, currency, false));
62     }
63
64     public long getValue()
65     {
66         return value;
67     }
68     
69     /**
70      * @return Returns the currency.
71      */

72     public Currency getCurrency()
73     {
74         return currency;
75     }
76 }
77
Popular Tags