package com.quantum.properties;

import java.sql.SQLException;

import com.quantum.ImageStore;
import com.quantum.Messages;
import com.quantum.model.Column;
import com.quantum.model.Entity;
import com.quantum.model.NotConnectedException;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class ColumnPropertyPage extends BaseEntityPropertyPage {
    
	protected void createInformationArea(Composite composite) {
        if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) {
			try {
				Column[] columns = getEntity().getColumns();
	        	
	        	Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
	            table.setHeaderVisible(true);
	            GridData gridData = new GridData(GridData.FILL_BOTH);
	            gridData.horizontalSpan = 2;
	            table.setLayoutData(gridData);
	            for (int i = 0, length = 7; i < length; i++) {
	                TableColumn column = new TableColumn(table, SWT.NONE);
	                column.setText(Messages.getString(getClass(), "column" + i));
	            }
	        	
	        	TableViewer viewer = new TableViewer(table);
	            setTableContents(viewer, columns);
	            setColumnWidths(table);
			} catch (NotConnectedException e) {
				createErrorMessage(composite, e);
			} catch (SQLException e) {
				createErrorMessage(composite, e);
			} catch (RuntimeException e) {
				createErrorMessage(composite, e);
			}
        }
	}

    private void setTableContents(TableViewer viewer, Column[] columns) {
		viewer.setContentProvider(new BasicContentProvider());
		
		viewer.setLabelProvider(new ITableLabelProvider() {

			public Image getColumnImage(Object element, int columnIndex) {
				if (columnIndex == 0 && element instanceof Column) {
					return ((Column) element).isPrimaryKey() 
							? ImageStore.getImage(ImageStore.KEYCOLUMN) 
							: ImageStore.getImage(ImageStore.COLUMN);
				} else {
					return null;
				}
			}
			public String getColumnText(Object element, int columnIndex) {
				String label = null;
				if (element instanceof Column) {
					Column column = (Column) element;
					switch (columnIndex) {
					case 0: 
						label = column.getName();
						break;
					case 1: 
						label = column.getTypeName();
						break;
					case 2: 
						label = String.valueOf(column.getSize());
						break;
					case 3: 
						label = String.valueOf(column.getNumberOfFractionalDigits());
						break;
					case 4: 
						label = column.isPrimaryKey() 
							? Messages.getString(ColumnPropertyPage.class, "true") 
							: Messages.getString(ColumnPropertyPage.class, "false");
						break;
					case 5: 
						label = column.isNullable() 
							? Messages.getString(ColumnPropertyPage.class, "true") 
							: Messages.getString(ColumnPropertyPage.class, "false");
						break;
					case 6: 
						label = column.getRemarks();
						break;
					default:
					}
				}
				
				return label == null ? "" : label;
			}
			public void addListener(ILabelProviderListener listener) {
			}
			public void dispose() {
			}
			public boolean isLabelProperty(Object element, String property) {
				return false;
			}
			public void removeListener(ILabelProviderListener listener) {
			}
		});
		
		viewer.setInput(columns);
    }
}