package TestApp01_call;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import java.math.BigDecimal;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;

public class popUp02 extends Shell {
	private Text text;
	private String cashBack = "";
	
	public String getCashBack() {
		return cashBack;
	}
	
	public void setCashback(String cashBack) {
		this.cashBack = cashBack;
		text.setText(this.cashBack);
	}
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			popUp02 shell = new popUp02(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Create the shell.
	 * @param display
	 */
	public popUp02(Display display) {
		super(display, SWT.DIALOG_TRIM);
		
		text = new Text(this, SWT.BORDER | SWT.CENTER);
		text.setBounds(105, 36, 105, 26);
		text.setTextLimit(8);
		text.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				char[] chars = new char[string.length()];
				string.getChars(0, chars.length, chars, 0);
				for (int i = 0; i < chars.length; i++) {
					if (!('0' <= chars[i] && chars[i] <= '9' || chars[i] == '.')) {
						e.doit = false;
						return;
					}
				}
			}
		});
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("  ");
		setSize(322, 139);
		
		Label lblTableNumber = new Label(this, SWT.NONE);
		lblTableNumber.setAlignment(SWT.CENTER);
		lblTableNumber.setBounds(94, 10, 127, 20);
		lblTableNumber.setText("Cashback Amount");
		
		Button btnOk = new Button(this, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (text.getText() != "") {
					BigDecimal cashbackAmount = new BigDecimal(text.getText());
					if (cashbackAmount.compareTo(BigDecimal.ZERO) <= 0)
						JOptionPane.showMessageDialog(null, "CASHBACK AMOUNT CANNOT BE LESS THAN EQUAL 0.");
					else  {
						cashBack = text.getText();
						close();
					}
				} else JOptionPane.showMessageDialog(null, "CASHBACK AMOUNT CANNOT BE EMPTY");
			}
		});
		btnOk.setBounds(121, 68, 74, 30);
		btnOk.setText("OK");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
