Calculator - greipadmin/greip GitHub Wiki

Introduction

A simple calculator, displayed as a whole widget or inside a tool window.

Constructor

  • Calculator(Composite parent)
    • parent the parent Composite

Methods

  • 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.

Use Calculator as a whole widget

final Calculator calculator = new Calculator(parent);
calculator.setDecimalFormat(new DecimalFormat("#,##0.## €"));

Use Calculator with an SWT Text widget

The easiest way is to use an CalculatorTextAdapter. The adapter handles three tasks:

  1. user input verification
  2. open and close calculator and
  3. apply calculation results to text widget.

Methods of CalculatorTextAdapter

  • 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 "=".

Examples

  • Sample1 shows how to use the calculator in a resizable window.
  • Sample2 demonstrates how to use the calculator in conjunction with an SWT Text widget.

Full featured example

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 Shift+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 = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
  format.setMaximumIntegerDigits(6);

  adapter.setDecimalFormat(format);

  // optional: press SHIFT+CR to open calculator (default is '=')
  adapter.openCalculatorWhen(e -> e.keyCode == SWT.CR && e.stateMask == SWT.SHIFT);

  // optional: initialize calculator properties (default is use default properties)
  adapter.setCalculatorConfigurer(c -> c.setBackground(display.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());

    // remove grouping separator
    txt.setText(format.format(value).replace(groupingSeparator, ""));
    txt.selectAll();
  });

  // 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.selectAll();

  shell.pack();
  shell.open();

  while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
  }
  display.dispose();
}
⚠️ **GitHub.com Fallback** ⚠️