package com.quantum.wizards;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import com.quantum.Messages;
import com.quantum.model.JDBCDriver;
import com.quantum.view.JDBCDriverTableViewer;

/**
 * @author BC
 */
public class JDBCDriverSelectionWizardPage extends PropertyChangeWizardPage implements ISelectionChangedListener {

	private JDBCDriverTableViewer tableViewer;
	
	private JDBCDriver driver;
	
	/**
	 * @param pageName
	 */
	public JDBCDriverSelectionWizardPage(String pageName) {
		super(pageName);

		setTitle(Messages.getString(getClass(), "title"));
		setDescription(Messages.getString(getClass(), "description"));
	}

	public void createControl(Composite parent) {
		setPageComplete(false);
		Composite container = createBasicContainer(parent, 1);
		GridLayout layout = new GridLayout(1, false);
		container.setLayout(layout);
		
		this.tableViewer = new JDBCDriverTableViewer(container);
		GridData data = new GridData(GridData.FILL_BOTH);
		this.tableViewer.getControl().setLayoutData(data);
		this.tableViewer.addSelectionChangedListener(this);

		Button newDriver = new Button(container, SWT.PUSH);
		newDriver.setText(Messages.getString(getClass(), "addDriver"));
		newDriver.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
		        AddDriverWizard wizard = new AddDriverWizard();
		        WizardDialog dialog =
		            new WizardDialog(getShell(), wizard);
		        dialog.open();
			}
		});
		data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
		newDriver.setLayoutData(data);
		
		setControl(container);
	}
	/**
	 * @param parent
	 */
	private Composite createBasicContainer(Composite parent, int numberOfColumns) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = numberOfColumns;
		layout.verticalSpacing = 9;
		GridData fullHorizontal = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(fullHorizontal);
		return container;
	}
	public void selectionChanged(SelectionChangedEvent event) {
		IStructuredSelection selection = (IStructuredSelection) event.getSelection();
		setPageComplete(!selection.isEmpty());
		if (!selection.isEmpty()) {
			setDriver((JDBCDriver) selection.getFirstElement());
		}
		
	}
	/**
	 * @return Returns the driver.
	 */
	public JDBCDriver getDriver() {
		return this.driver;
	}
	/**
	 * @param driver The driver to set.
	 */
	public void setDriver(JDBCDriver driver) {
		if (this.driver != driver) {
			JDBCDriver original = this.driver;
			this.driver = driver;
			firePropertyChange("driver", original, driver);
		}
	}
	public void dispose() {
		this.tableViewer.dispose();
		super.dispose();
	}
}