/*******************************************************************************
 * Copyright (c) 2015, 2016 CNRS, IRIT, IRT AESE (IRT Saint Exupéry).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the LGPL 3.0
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0-standalone.html
 *
 * Contributors:
 *     Faiez Zalila - initial API and implementation and/or initial documentation
 *     Jorge Enrique Gutierrez Zuluaga
 *******************************************************************************/
package com.irtsaintexupery.fiacre.simulator.core;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ReadDelay extends TitleAreaDialog {

	private Text textValue;

	private String value;

	private Shell shell;

	public ReadDelay(Shell parentShell) {
		super(parentShell);
		shell = parentShell;
	}

	@Override
	public void create() {
		super.create();
		setTitle("Delaying Fiacre model");
		final ReadDelay read_delay = this;
		setMessage("This is a TitleAreaDialog", IMessageProvider.INFORMATION);
		this.getButton(OK).setEnabled(false);

		textValue.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				try {
					value = textValue.getText();
					read_delay.getButton(OK).setEnabled(true);
				} catch (Exception e) {
					read_delay.getButton(OK).setEnabled(false);
				}
			}
		});

		shell.addListener(SWT.Traverse, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.TRAVERSE_ESCAPE)
					event.doit = false;
			}
		});

	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayout layout = new GridLayout(2, false);
		container.setLayout(layout);

		createTextValue(container);


		return area;
	}

	private void createTextValue(Composite container) {
		Label lbtTextValue = new Label(container, SWT.NONE);
		lbtTextValue.setText("enter delay");

		GridData dataTextValue = new GridData();
		dataTextValue.grabExcessHorizontalSpace = true;
		dataTextValue.horizontalAlignment = GridData.FILL;

		textValue = new Text(container, SWT.BORDER);
		textValue.setLayoutData(dataTextValue);
	}

	@Override
	protected boolean isResizable() {
		return true;
	}

	// save content of the Text fields because they get disposed
	// as soon as the Dialog closes
	private void saveInput() {
		value = textValue.getText();
	}

	@Override
	protected void okPressed() {
		saveInput();
		super.okPressed();
	}

	public String getValue() {
		return value;
	}

}