/*
 * Copyright (C) 2005 NTT DATA Corporation
 * 
 */
package org.postgresforest.tool.view.tablecolumn;

import java.util.ArrayList;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.TableViewer;
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.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.postgresforest.tool.bean.TableInfo;
import org.postgresforest.tool.util.CommonTableContentProvider;
import org.postgresforest.tool.util.EnableSelection;
import org.postgresforest.tool.util.Messages;
import org.postgresforest.tool.util.SessionProperty;
import org.postgresforest.tool.view.ViewBase;
import org.postgresforest.tool.view.action.MakePartitionAction;

/**
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TableColumnView extends ViewBase {
	
	public static final String ID = "org.postgresforest.tool.view.TableColumnView";
	
	private TableViewer viewer = null;
	private Label label = null;
	private Label label1 = null;
	

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl(Composite parent) {

		//儗僀傾僂僩
		GridLayout layout = new GridLayout(1, true);
        parent.setLayout(layout);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        
        //儔儀儖
        label = new Label(parent, SWT.NONE);
		label.setText(Messages.getString("form.db.name") + "                    ");
        label1 = new Label(parent, SWT.NONE);
		label1.setText(Messages.getString("form.table") + "                    ");
		
		//僥乕僽儖
		Table table = new Table(parent,
				SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.SINGLE | SWT.BORDER);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
        GridData gridData = new GridData();
        gridData.horizontalAlignment = GridData.FILL;
        gridData.verticalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessVerticalSpace = true;
        table.setLayoutData(gridData);
		
		TableColumn column = new TableColumn(table, SWT.CENTER, 0);
		column.setText(Messages.getString("form.partition.Attribute"));
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT, 1);
		column.setText(Messages.getString("form.data.name"));
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT, 2);
		column.setText(Messages.getString("form.data.type"));
		column.setWidth(150);
		
		viewer = new TableViewer(table);
		
		viewer.setContentProvider(new CommonTableContentProvider());
		viewer.setLabelProvider(new TableColumnLabelProvider());
		
		String[] properties = new String[]{
				"attribute",
				"name",
				"type"
		};
		// 僇儔儉丒僾儘僷僥傿偺愝掕
		viewer.setColumnProperties(properties);
	    // 奺僇儔儉偵愝掕偡傞僙儖丒僄僨傿僞偺攝楍
		CellEditor[] editors = new CellEditor[]{ // 嘇
				new CheckboxCellEditor(table),
				null,
				null
		};

	    //僙儖丒僄僨傿僞偺愝掕
	    viewer.setCellEditors(editors);
	    
	    //僙儖丒儌僨傿僼傽僀儎偺愝掕
	    viewer.setCellModifier(new TableColumnCellModifier(viewer));
	    //MASUCOMMENT 塃僋儕僢僋懳嶔
		createContextMenu(viewer.getTable());
	    
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
	 */
	public void update() {
		//僙僢僔儑儞v儘僷僥傿偐傜慖戰偝傟偨僨乕僞儀乕僗柤傪庢摼

		String selectDb = (String)SessionProperty.get("selectDb");
		TableInfo tableInfo = (TableInfo)SessionProperty.get("tableInfo");

		
		if(selectDb == null || 
				tableInfo == null ){
			label.setText(Messages.getString("form.db.name") + "                    ");
			label1.setText(Messages.getString("form.table") + "                    ");
			viewer.setInput(null);
			setEnabledAction(MakePartitionAction.ID, EnableSelection.FALSE);
			return ;
		}

		String selectTable = tableInfo.getName();
		
		
		//僥乕僽儖堦棗傪庢摼
		
		ArrayList tableColumn = tableInfo.getColumn();

		//TableViewer偵僥乕僽儖堦棗傪愝掕
		viewer.setInput(tableColumn);
		
		//儐乕僓乕僨乕僞儀乕僗柤傪愝掕偡傞丅
		label.setText(Messages.getString("form.db.name") + "丗" + selectDb);
		//僥乕僽儖柤傪愝掕偡傞丅
		label1.setText(Messages.getString("form.table") + "丗" + selectTable);

		//僷乕僥傿僔儑儞壔僥乕僽儖偺応崌曇廤晄壜丄懡廳壔偺応崌曇廤壜偵
		boolean editenable = tableInfo.isCanPartition();
		TableColumnCellModifier modifyer = (TableColumnCellModifier)viewer.getCellModifier();
		modifyer.setEditable(editenable);
		if(editenable)
		    setEnabledAction(MakePartitionAction.ID, EnableSelection.TRUE);
		else
			setEnabledAction(MakePartitionAction.ID, EnableSelection.FALSE);
		    
		
	}



}
