package gui;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;

/**
 * InputDialog Klasse
 * Mit Hilfe dieser Klasse wird ein Eingabefenster erzeugt,
 * welcher zur Eingabe von einfachen Texten eingesetzt wird. 
 */

public class InputDialog extends Dialog {

	// Attribute
	private Shell sShell = null;
	private Label label = null;
	private Text text = null;
	private Button button = null;
	private Button button1 = null;
	private Label label1 = null;
	private String sReturn = "";

	/**
	 * Konstruktor
	 * @param parent übergeordnete Shell
	 */
	public InputDialog(Shell parent){
		super(parent);
		createSShell();
	}
	
	/**
	 * Öffnet den Dialog
	 * @return den eingegebenen Text oder einen leeren String
	 */
	public String open(){
		// Ereignisschleife
		sShell.open();
		Display display = getParent().getDisplay();
		while(!sShell.isDisposed()){
			if (!display.readAndDispatch()) display.sleep();
		}
		return sReturn;
	}

	/**
	 * Erzeugt den Dialog und ordnet alle Elemente auf diesem an
	 */
	private void createSShell() {
		GridData gridData11 = new GridData();
		gridData11.grabExcessHorizontalSpace = true;
		GridData gridData1 = new GridData();
		gridData1.horizontalSpan = 4;
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData = new GridData();
		gridData.horizontalSpan = 4;
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		gridData.grabExcessHorizontalSpace = true;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		gridLayout.marginBottom = 5;
		gridLayout.marginLeft = 5;
		gridLayout.marginRight = 5;
		gridLayout.marginTop = 5;
		gridLayout.horizontalSpacing = 10;
		gridLayout.verticalSpacing = 8;
		sShell = new Shell(SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.TITLE);
		sShell.setText("Eingabe");
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(300, 140));
		label = new Label(sShell, SWT.NONE);
		label.setText("Frage");
		label.setLayoutData(gridData);
		text = new Text(sShell, SWT.BORDER);
		text.setLayoutData(gridData1);
		Label filler2 = new Label(sShell, SWT.NONE);
		label1 = new Label(sShell, SWT.NONE);
		label1.setText("");
		label1.setLayoutData(gridData11);
		button1 = new Button(sShell, SWT.NONE);
		button1.setText("Abbrechen");
		button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    
        		sShell.close();
        	}
        });
		button = new Button(sShell, SWT.NONE);
		button.setText("OK");
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    
        		sReturn = text.getText();
        		sShell.close();
        	}
        });
		sShell.setDefaultButton(button);
	}

	/* ##########################################################################
	 * 						ÖFFENTLICHE METHODEN
	 * ##########################################################################*/	
	
	/**
	 * Setzt den Fragetext
	 * @param t der zu setzende Text
	 */
	public void setText(String t){
		label.setText(t);
	}
	
	/**
	 * Setzt den Text in der Textbox
	 * @param t der zu setzende Text
	 */
	public void setBoxText(String t){
		text.setText(t);
		text.selectAll();
	}
}
