-
Notifications
You must be signed in to change notification settings - Fork 4
Calculator
greipadmin edited this page Feb 2, 2017
·
18 revisions
A simple calculator, displayed as a whole widget or inside a tool window.
- setValue sets the initial value.
- getValue gets the value after calculation.
- setDecimalFormat sets the format for displaying the value. Default is "#,##0.##########".
- setBackground sets the background color.
- setResultBackround sets the result area background color.
- setResultForeground sets the result area foreground color.
final Calculator calculator = new Calculator(parent);
calculator.setDecimalFormat(new DecimalFormat("#,##0.## €"));
The simplest way is to use a CalculatorTextAdapter. The adapter handle this tasks: user input verification, open and close calculator and apply calculation results to text widget.
- setDecimalFormat(DecimalFormat) defines the value formatting. Default is "#,##0.##########".
- setValueInitializer(Supplier) sets the getter for initial value.
- setResultConsumer(Consumer) defines the calculation result consumer.
- setCalculatorConfiguerer(Consumer) defines a callback for calculator initialization.
- openCalculatorWhen(Predicate) defines the key sequence for open calculator. Default is "=".
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Text txt = new Text(shell, SWT.RIGHT | SWT.BORDER);
txt.setLayoutData(new GridData(100, SWT.DEFAULT));
new CalculatorTextAdapter(txt);
txt.setText("0");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Greip - Calculator Popup Demo");
shell.setLayout(new GridLayout(2, false));
new Label(shell, SWT.NONE).setText("Enter a number:");
final Text txt = new Text(shell, SWT.RIGHT | SWT.BORDER);
txt.setLayoutData(new GridData(100, SWT.DEFAULT));
final Label lbl = new Label(shell, SWT.NONE);
lbl.setText("Press Ctrl+Enter to open calculator popup.");
lbl.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
final CalculatorTextAdapter adapter = new CalculatorTextAdapter(txt);
// optional: define your own decimal format (default is
// "#,##0.##########")
final DecimalFormat format = new DecimalFormat("#,##0.00 €");
format.setMaximumIntegerDigits(6);
adapter.setDecimalFormat(format);
// optional: press SHIFT+CR to open calculator (default is '=')
adapter.openCalculatorWhen(event -> {
return event.keyCode == SWT.CR && event.stateMask == SWT.SHIFT;
});
// optional: initialize calculator properties (default is use default
// properties)
adapter.setCalculatorConfigurer(calculator -> {
calculator.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
});
// optional: define your own result consumer (default is set formatted
// value to text field)
adapter.setResultConsumer(value -> {
final char groupingSeparator = format.getDecimalFormatSymbols().getGroupingSeparator();
txt.setText(format.format(value).replace(String.valueOf(groupingSeparator), ""));
txt.setSelection(0, txt.getText().length() - 2);
});
// optional: define your own value initializer (default is text field
// content)
adapter.setValueInitializer(() -> BigDecimal.TEN);
// initialize text widget with ten
txt.setText(format.format(10));
txt.setSelection(0, 5);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}