/**
 * @author tyl
 * @Action  ini
 * 2011-3-15
 */
package cn.com.origin.autocodemanager.common;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

public class CreateCompositeUtil {

	public static Group createCompositeGroup(Composite parent, String title) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalSpan = 1;
		gridData.horizontalAlignment = GridData.FILL;
		Group codeTreeGroup = new Group(parent, SWT.NONE);
		codeTreeGroup.setText(title);
		codeTreeGroup.setLayout(gridLayout);
		codeTreeGroup.setLayoutData(gridData);
		return codeTreeGroup;
	}

	public static Label createLabel(Composite composite, String text,
			int heightLength, int widthLength) {
		Label newLabel = new Label(composite, SWT.NONE);
		// Label newLabel = toolkit.createLabel(composite, text);
		newLabel.setText(text);
		GridData gridData = new GridData();
		gridData.heightHint = heightLength;
		gridData.widthHint = widthLength;
		newLabel.setLayoutData(gridData);
		newLabel.setVisible(true);
		return newLabel;
	}

	public static Label createSeparator(Composite composite, String text,
			int heightLength, int widthLength) {
		// Label separator = toolkit.createLabel(composite, "");
		Label separator = new Label(composite, SWT.NONE);
		separator.setText("");
		GridData gridData1 = new GridData();
		gridData1.heightHint = heightLength;
		gridData1.widthHint = widthLength;
		separator.setLayoutData(gridData1);
		separator.setVisible(true);
		return separator;
	}

	public static Combo createNotFillNewCombo(Composite parent, String[] items) {
		Combo newCombo = new Combo(parent, SWT.BORDER);
		newCombo.setLayoutData(getNotFillNewGridData());
		newCombo.setItems(items);
		return newCombo;
	}

	// public static Combo createNotFillNewCombo(Table parent, String[] items) {
	// Combo newCombo = new Combo(parent, SWT.BORDER);
	// newCombo.setLayoutData(getNotFillNewGridData());
	// newCombo.setItems(items);
	// return newCombo;
	// }

	public static GridData getNotFillNewGridData() {
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		return gridData;
	}

	public static Text createText(Composite composite, String text,
			int heightLength, int widthLength) {
		// Text newText = toolkit.createText(composite, "");
		Text newText = new Text(composite, SWT.BORDER);
		newText.setText(text);

		GridData gridData1 = new GridData();
		gridData1.heightHint = heightLength;
		gridData1.widthHint = widthLength;
		newText.setLayoutData(gridData1);
		newText.setVisible(true);
		return newText;
	}

	public static Combo createCombo(Composite composite, String[] items,
			int heightLength, int widthLength) {
		Combo newCombo = new Combo(composite, 8);
		GridData gridData = new GridData();
		gridData.heightHint = heightLength;
		gridData.widthHint = widthLength;
		newCombo.setLayoutData(gridData);
		if ((items != null) && (items.length > 0)) {
			newCombo.setItems(items);
		}
		newCombo.setVisible(true);
		return newCombo;
	}

	public static Combo createNewCombo(Composite composite, int heightLength,
			int widthLength) {
		Combo newCombo = new Combo(composite, 8);
		GridData gridData = new GridData();
		gridData.heightHint = heightLength;
		gridData.widthHint = widthLength;
		newCombo.setLayoutData(gridData);
		newCombo.setVisible(true);
		return newCombo;
	}

	public static Button createButton(Composite composite, String text,
			int heightLength, int widthLength) {
		// Button newButton = toolkit.createButton(composite, text, 8);
		Button newButton = new Button(composite, SWT.NONE);
		GridData gridData1 = new GridData();
		gridData1.heightHint = heightLength;
		gridData1.widthHint = widthLength;
		newButton.setLayoutData(gridData1);
		newButton.setVisible(true);
		return newButton;
	}

	public static Text createTextArea(Composite composite, String text,
			int heightLength, int widthLength) {
		Text newTextArea = new Text(composite, SWT.MULTI | SWT.WRAP
				| SWT.V_SCROLL | SWT.BORDER);
		GridData gridData1 = new GridData();
		gridData1.heightHint = heightLength;
		gridData1.widthHint = widthLength;
		newTextArea.setLayoutData(gridData1);
		newTextArea.setVisible(true);
		return newTextArea;
	}

	public static Text createNewTextArea(Composite parent, String initValue) {
		Text newText = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL
				| SWT.BORDER);
		newText.setLayoutData(getNewGridData());
		newText.setText(initValue);
		return newText;
	}

	public static DateTime createDateButton(Composite composite, Date date,
			int heightLength, int widthLength) {
		DateTime newCombo = new DateTime(composite, SWT.MULTI | SWT.WRAP
				| SWT.V_SCROLL | SWT.BORDER);
		GridData gridData = new GridData();
		gridData.heightHint = heightLength;
		gridData.widthHint = widthLength;
		newCombo.setLayoutData(gridData);
		if (date != null) {
			newCombo.setData(date);
		}
		newCombo.setVisible(true);
		return newCombo;
	}

	public static GridData getNewGridData() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = false;
		gridData.verticalAlignment = GridData.FILL;
		return gridData;
	}
	
	

	public static Text createNewText(Composite parent, String initValue) {
		Text newText = new Text(parent, SWT.BORDER);
		newText.setLayoutData(getNewGridData());
		newText.setText(initValue);
		return newText;
	}
	
	

	public static Button createNewCheckBox(Composite parent, String title) {
		Button newButton = new Button(parent, SWT.CHECK);
		newButton.setLayoutData(getNewGridData());
		newButton.setText(title);
		return newButton;
	}

	public static Combo createNewCombo(Composite parent, String[] items) {
		Combo newCombo = new Combo(parent, SWT.BORDER);
		newCombo.setLayoutData(getNewGridData());
		newCombo.setItems(items);
		return newCombo;
	}

	public static Combo createNewCombo(Composite parent) {
		Combo newCombo = new Combo(parent, SWT.BORDER);
		newCombo.setLayoutData(getNewGridData());
		return newCombo;
	}

	public static Button createNewButton(Composite composite, String text) {
		// Button newButton = toolkit.createButton(composite, text, 8);
		Button newButton = new Button(composite, SWT.BUTTON1 | SWT.BORDER
				| SWT.COLOR_BLUE);
		GridData gridData1 = new GridData();
		gridData1.heightHint = 40;
		gridData1.widthHint = 100;
		newButton.setLayoutData(gridData1);
		newButton.setText(text);
		newButton.setVisible(true);
		return newButton;
	}

	public static Label createNewLabel(Composite parent, String title) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title + "：");
		return label;
	}

	public static Label createNewWhiteLabel(Composite parent, String title) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title + "：");
		label.setBackground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		return label;
	}

	/***
	 * 继峰特殊要求扩展
	 * 
	 * @param composite
	 * @param text
	 * @param heightLength
	 * @param widthLength
	 * @return
	 */
	public static Table createNewTable(Composite composite, String text,
			int heightLength, int widthLength) {
		final Table table = new Table(composite, SWT.MULTI | SWT.FULL_SELECTION);

		// Text newTextArea = new Text(composite, SWT.MULTI | SWT.WRAP |
		// SWT.V_SCROLL | SWT.BORDER);
		GridData gridData1 = new GridData();
		// gridData1.heightHint = heightLength;
		// gridData1.widthHint = widthLength;
		// gridData1.horizontalAlignment = SWT.FILL;
		// gridData1.grabExcessHorizontalSpace = true;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = SWT.FILL;
		table.addListener(SWT.MeasureItem, new Listener() { // 向表格增加一个SWT.MeasureItem监听器，每当需要单元内容的大小的时候就会被调用。
					public void handleEvent(Event event) {
						event.width = table.getGridLineWidth(); // 设置宽度
						event.height = (int) Math.floor(event.gc
								.getFontMetrics().getHeight() * 2); // 设置高度为字体高度的1.5倍
					}
				});
		table.setLayoutData(gridData1);
		table.setLinesVisible(true);
		table.setVisible(true);
		return table;
	}

}
