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 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 popUp01 extends Shell {
	private Text text;
	private String tableNumber = "";
	
	public String getTableNumber() {
		return tableNumber;
	}
	
	public void setTableNumber(String tableNumber) {
		this.tableNumber = tableNumber;
		text.setText(this.tableNumber);
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			popUp01 shell = new popUp01(display);
			
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Create the shell.
	 * @param display
	 */
	public popUp01(Display display) {
		super(display, SWT.DIALOG_TRIM);
		
		text = new Text(this, SWT.BORDER | SWT.CENTER);
		text.setBounds(116, 36, 83, 26);
		text.setTextLimit(8);
		text.addVerifyListener(new VerifyListener() {

			@Override
			public void verifyText(VerifyEvent e) {
				String string = e.text;
				boolean isAlphaNumeric = string != null & string.chars().allMatch(Character::isLetterOrDigit);
				if (!isAlphaNumeric) {
					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(107, 10, 101, 20);
		lblTableNumber.setText("Table Number");
		
		Button btnOk = new Button(this, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (text.getText() != "") {
					tableNumber = text.getText();
					close();
				} else JOptionPane.showMessageDialog(null, "TABLE NUMBER 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
	}
}
