Skip to content

Calculator

greipadmin edited this page Feb 26, 2017 · 18 revisions

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

Minimal example

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();
}

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 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();
}
Clone this wiki locally