package com.gx.obe.management.shell;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 选择界面
 * @author chenxw
 */
public class SelectShell extends Shell {
	
	private Composite composite;
	
	private SelectListener listener;
	
	/**
	 * Create the shell.
	 * @param display
	 */
	public SelectShell(Shell shell) {
		super(shell, SWT.NO_TRIM | SWT.TOP);
		
		this.addListener(SWT.Deactivate, new Listener() {
			public void handleEvent(Event arg0) {
				close();
			}
		});
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.marginHeight = 1;
		fillLayout.marginWidth = 1;
		this.setLayout(fillLayout);
		this.setBackground(ColorConstants.SKIN_BG);
		this.setBackgroundMode(SWT.INHERIT_FORCE);
		this.setAlpha(230);
		
		composite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		gl_composite.verticalSpacing = 0;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
	}
	
	/**
	 * @Description: 创建按钮
	 * @author chenxw
	 * @param image 
	 * @return
	 */
	public void addItem(String name, final String type, Image image) {
		final ImageButton itemLabel = new ImageButton(composite, SWT.LEFT);
		itemLabel.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				if (null != listener) {
					listener.selectAction(type);
				}
			}
		});
		itemLabel.setText(name);
		itemLabel.setImage(image);
		itemLabel.setBackground(ColorConstants.LIGHT_BACKGROUND_COLOR);
		itemLabel.setLeftMargin(5);
		itemLabel.setRightMargin(5);
		itemLabel.setTopMargin(5);
		itemLabel.setBottomMargin(5);
		itemLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		itemLabel.setSurroundMargin(3);
		itemLabel.setActiveSurroundColor(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		itemLabel.setActiveBgColor(ColorConstants.SKIN_BG);
		itemLabel.setActiveForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		itemLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		composite.layout();
	}
	
	public interface SelectListener {
		void selectAction(String type);
	}
	
	public void addSelectListener(SelectListener listener) {
		this.listener = listener;
	}
	
	protected void checkSubclass() {}
	
}
