/*******************************************************************************
 * Copyright (c) 2009 Skhiri Sabri
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http:// www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Olivier Moïses- initial API and implementation
 *******************************************************************************/

package org.wazaabi.engine.swt.snippets.standalone;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.wazaabi.engine.swt.SWTStandaloneHelper;
import org.wazaabi.engine.swt.viewers.SWTControlViewer;
import org.wazaabi.model.swt.layouts.GridLayout;
import org.wazaabi.model.swt.layouts.LayoutsFactory;
import org.wazaabi.model.swt.widgets.Composite;
import org.wazaabi.model.swt.widgets.Label;
import org.wazaabi.model.swt.widgets.PushButton;
import org.wazaabi.model.swt.widgets.Text;
import org.wazaabi.model.swt.widgets.WidgetsFactory;

/**
 * Example of implementation of simple form.
 * @author sskhiri
 * 
 */
public class FormSnippet {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// init SWT Engine in standalone mode
		SWTStandaloneHelper.init();

		// create the shell
		Display display = new Display();
		Shell mainShell = new Shell(display, SWT.SHELL_TRIM);
		mainShell.setText("Wazaabi UI form");
		mainShell.setLayout(new FillLayout());
		mainShell.setSize(300, 100);

		// create the viewer
		SWTControlViewer viewer = new SWTControlViewer(mainShell);

		// create a composite and set its layout
		Composite composite = WidgetsFactory.eINSTANCE.createComposite();
		GridLayout gridLayout = LayoutsFactory.eINSTANCE.createGridLayout();
		composite.setLayout(gridLayout);
		//Set the number of columns
		gridLayout.setNumColumns(2);
		
		// create two labels with text
		Label nameLabel = WidgetsFactory.eINSTANCE.createLabel();
		Text nameText = WidgetsFactory.eINSTANCE.createText();
		
		Label addressLabel = WidgetsFactory.eINSTANCE.createLabel();
		Text addressText = WidgetsFactory.eINSTANCE.createText();
		
		//Set the labels
		nameLabel.setText("Name:");
		addressLabel.setText("Address:");
		
		//Save & cancel button
		PushButton save = WidgetsFactory.eINSTANCE.createPushButton();
		save.setText("Save");
		
		PushButton cancel = WidgetsFactory.eINSTANCE.createPushButton();
		cancel.setText("cancel");
		
		//Add to the composite
		composite.getChildren().add(nameLabel);
		composite.getChildren().add(nameText);

		composite.getChildren().add(addressLabel);
		composite.getChildren().add(addressText);
		
		composite.getChildren().add(save);
		composite.getChildren().add(cancel);
		
		// Set the content
		viewer.setContents(composite);
		
		//Open the shell
		mainShell.open();

		while (!mainShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();

	}

}

