-
Notifications
You must be signed in to change notification settings - Fork 4
Calculator
greipadmin edited this page Feb 26, 2017
·
18 revisions
A simple calculator, displayed as a whole widget or inside a tool window.
Calculator(Composite parent)
-
parent
the parent Composite
-
setValue(BigDecimal)
sets the initial value. -
BigDecimal getValue()
gets the value after calculation. -
setDecimalFormat(DecimalFormat)
sets the format for displaying the value. Default is "#,##0.##########". -
setBackground(Color)
sets the background color. -
setResultBackround(Color)
sets the result area background color. -
setResultForeground(Color)
sets the result area foreground color.
final Calculator calculator = new Calculator(parent);
calculator.setDecimalFormat(new DecimalFormat("#,##0.## €"));
The easiest way is to use an CalculatorTextAdapter
. The adapter handles three 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\<BigDecimal\>)
sets the getter for initial value. -
setResultConsumer(Consumer\<BigDecimal\>)
defines the calculation result consumer. -
setCalculatorConfiguerer(Consumer\<Calculator\>)
defines a callback for calculator initialization. -
openCalculatorWhen(Predicate\<Event\>)
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 DecimalFormatSymbols dfs = format.getDecimalFormatSymbols();
final String groupingSeparator = String.valueOf(dfs.getGroupingSeparator());
txt.setText(format.format(value).replace(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();
}