package com.quantum.properties;

import com.quantum.ImageStore;
import com.quantum.Messages;
import com.quantum.model.Entity;
import com.quantum.model.EntityHolder;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
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.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * @author BC
 */
public abstract class BaseEntityPropertyPage extends PropertyPage {

	class BasicContentProvider implements IStructuredContentProvider {
	
		public Object[] getElements(Object inputElement) {
			return (inputElement instanceof Object[]) ? (Object[]) inputElement : null; 
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
    protected Control createContents(Composite parent) {

        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));

		Label label = new Label(composite, SWT.NONE);
        label.setText(Messages.getString(BaseEntityPropertyPage.class, "name"));

        Entity entity = getEntity();

        Label name = new Label(composite, SWT.NONE);
        name.setText(entity.getName());

        label = new Label(composite, SWT.NONE);
        label.setText(Messages.getString(BaseEntityPropertyPage.class, "schema"));

        Label schema = new Label(composite, SWT.NONE);
        schema.setText(entity.getSchema());
        
        createInformationArea(composite);
        
        return composite;
    }	
	/**
	 * @param composite
	 */
	protected void createErrorMessage(Composite composite, Exception e) {
		Label icon = new Label(composite, SWT.NONE);
		icon.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
		icon.setImage(ImageStore.getImage(ImageStore.WARNING));
		
		Label error = new Label(composite, SWT.NONE);
		error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
		error.setText(Messages.getString(BaseEntityPropertyPage.class, "error") 
				+ (e.getMessage() == null ? "" : "\n" + e.getMessage()));
	}


    /**
	 * @param composite
	 */
	protected abstract void createInformationArea(Composite composite);

	protected Entity getEntity() {
        Entity entity =
            ((EntityHolder) getElement()).getEntity();
        return entity;
    }
	/**
	 * @param table
	 */
	protected void setColumnWidths(Table table) {
		for (int i = 0, length = table.getColumnCount(); i < length; i++) {
		    table.getColumn(i).pack();
		}
	}

}