93 lines
2.4 KiB
Java
93 lines
2.4 KiB
Java
|
|
package snippets;
|
||
|
|
|
||
|
|
import java.text.DecimalFormat;
|
||
|
|
import java.text.NumberFormat;
|
||
|
|
import java.text.ParseException;
|
||
|
|
import java.util.Currency;
|
||
|
|
import java.util.Locale;
|
||
|
|
|
||
|
|
import org.eclipse.swt.*;
|
||
|
|
import org.eclipse.swt.events.*;
|
||
|
|
import org.eclipse.swt.layout.GridData;
|
||
|
|
import org.eclipse.swt.layout.GridLayout;
|
||
|
|
import org.eclipse.swt.widgets.*;
|
||
|
|
|
||
|
|
import com.common.debug.Debug;
|
||
|
|
|
||
|
|
public class TextCurrency {
|
||
|
|
public static boolean disableFocusSelection = false;
|
||
|
|
//public static Currency currency = Currency.getInstance(Locale.US);
|
||
|
|
public static NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(Locale.US);
|
||
|
|
public static boolean ignoreModifyEvents = false;
|
||
|
|
public static Text text = null;
|
||
|
|
|
||
|
|
public static void main(String [] args) {
|
||
|
|
Display display = new Display();
|
||
|
|
Shell shell = new Shell(display);
|
||
|
|
shell.setLayout(new GridLayout(1, false));
|
||
|
|
|
||
|
|
text = new Text(shell, SWT.SINGLE | SWT.BORDER);
|
||
|
|
text.setLayoutData(new GridData(150, -1));
|
||
|
|
text.setText(defaultFormat.format(123));
|
||
|
|
text.addModifyListener(new ModifyListener() {
|
||
|
|
public void modifyText(ModifyEvent event) {
|
||
|
|
if(!ignoreModifyEvents) {
|
||
|
|
int caretPosition = text.getCaretPosition();
|
||
|
|
Number newNumber = null;
|
||
|
|
|
||
|
|
try {
|
||
|
|
ignoreModifyEvents = true;
|
||
|
|
newNumber = defaultFormat.parse(text.getText());
|
||
|
|
text.setText(defaultFormat.format(newNumber));
|
||
|
|
}//if//
|
||
|
|
catch(ParseException e) {
|
||
|
|
Debug.log(e);
|
||
|
|
//Reset text.
|
||
|
|
}//catch//
|
||
|
|
finally {
|
||
|
|
ignoreModifyEvents = false;
|
||
|
|
}//finally//
|
||
|
|
|
||
|
|
text.setSelection(caretPosition);
|
||
|
|
}//if//
|
||
|
|
}
|
||
|
|
});
|
||
|
|
text.addFocusListener(new FocusListener() {
|
||
|
|
public void focusLost(FocusEvent e) {
|
||
|
|
}
|
||
|
|
public void focusGained(FocusEvent e) {
|
||
|
|
if(!disableFocusSelection) {
|
||
|
|
text.getDisplay().asyncExec(new Runnable() {
|
||
|
|
public void run() {
|
||
|
|
text.selectAll();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}//if//
|
||
|
|
}
|
||
|
|
});
|
||
|
|
text.getShell().addShellListener(new ShellListener() {
|
||
|
|
public void shellIconified(ShellEvent e) {
|
||
|
|
}
|
||
|
|
public void shellDeiconified(ShellEvent e) {
|
||
|
|
}
|
||
|
|
public void shellDeactivated(ShellEvent e) {
|
||
|
|
disableFocusSelection = true;
|
||
|
|
}
|
||
|
|
public void shellActivated(ShellEvent e) {
|
||
|
|
text.getDisplay().asyncExec(new Runnable() {
|
||
|
|
public void run() {
|
||
|
|
disableFocusSelection = false;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
public void shellClosed(ShellEvent e) {
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
shell.open();
|
||
|
|
while (!shell.isDisposed()) {
|
||
|
|
if (!display.readAndDispatch()) display.sleep();
|
||
|
|
}
|
||
|
|
display.dispose();
|
||
|
|
}
|
||
|
|
}
|