package TestApp01_call;

import java.io.File;

import javax.swing.JOptionPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class XMLResponse extends Shell {
	private boolean isDone;
	private String title = "";
	private String message = "";
	private String fileLocation = "";

	private Label lblTitle;
	private Label lblMessage;
	private Label lblFilename;

//	protected static Shell shell;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			XMLResponse shell = new XMLResponse(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public XMLResponse(Display display) {
		super(display, SWT.SHELL_TRIM);

		lblTitle = new Label(this, SWT.NONE);
		lblTitle.setText(title);
		lblTitle.setBounds(50, 10, 404, 15);

		lblMessage = new Label(this, SWT.NONE);
		lblMessage.setText(message);
		lblMessage.setBounds(50, 41, 404, 15);

		Label lblXmlPath = new Label(this, SWT.NONE);
		lblXmlPath.setText("XML Path:");
		lblXmlPath.setBounds(30, 85, 75, 20);

		Button btnBrowse = new Button(this, SWT.NONE);
		btnBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
				String fileName = dialog.open();

				if (fileName != null) {
					Display.getDefault().asyncExec(new Runnable() {

						@Override
						public void run() {
							File file = new File(fileName);
							fileLocation = file.getAbsolutePath();

							lblFilename.setText(file.getName());
							lblFilename.setVisible(true);
						}

					});
				}
			}
		});
		btnBrowse.setBounds(111, 85, 75, 25);
		btnBrowse.setText("Browse");

		Button btnDone = new Button(this, SWT.NONE);
		btnDone.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!fileLocation.isEmpty()) {
					if (!fileLocation.contains(".xml" )) {
						JOptionPane.showMessageDialog(null, "File must be in XML Document.");
						return;
					}
					
					isDone = true;
					close();
				} else {
					JOptionPane.showMessageDialog(null, "File location cannot be empty");
				}
			}
		});
		btnDone.setText("DONE");
		btnDone.setBounds(330, 111, 124, 52);

		lblFilename = new Label(this, SWT.NONE);
		lblFilename.setText("FileName");
		lblFilename.setBounds(111, 119, 207, 15);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setSize(480, 213);

		lblFilename.setVisible(false);
	}

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

	public void setTitle(String title) {
		lblTitle.setText(title);
	}

	public void setMessage(String message) {
		lblMessage.setText(message);
	}

	public boolean getIsDone() {
		return isDone;
	}

	public String getFileLocation() {
		return fileLocation;
	}
}
