ExportDDLWizardFileSelectionPage.java :  » Database-Client » QuantumDB » com » quantum » flatfiles » wizard » Java Open Source

Java Open Source » Database Client » QuantumDB 
QuantumDB » com » quantum » flatfiles » wizard » ExportDDLWizardFileSelectionPage.java
package com.quantum.flatfiles.wizard;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.InputStream;

import org.eclipse.jface.viewers.StructuredSelection;
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 org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;

import com.quantum.flatfiles.MessageUtil;
import com.quantum.view.widget.ErrorMessageDisplayer;
import com.quantum.view.widget.FileSelectionWidget;

/**
 * @author BC Holmes
 */
public class ExportDDLWizardFileSelectionPage extends WizardNewFileCreationPage
    implements ErrorMessageDisplayer, PropertyChangeListener {
    
    private Tree resourceTree;
    private Text resourceName;
    private Text workspaceFileName;

  private final InputStreamProvider inputStreamProvider;
    private FileSelectionWidget fileSelection;
    private boolean saveInFileSystem = true;
    
  /**
     * Creates a new wizard page. 
     *
     * @param pageName the name of the page
   */
  public ExportDDLWizardFileSelectionPage(
      String pageName, InputStreamProvider inputStreamProvider) {
    super(pageName, new StructuredSelection());
    this.inputStreamProvider = inputStreamProvider;
        setFileName("create.sql");
        setTitle(MessageUtil.getString(getClass(), "title"));
        setDescription(MessageUtil.getString(getClass(), "description"));
  }

    public void createControl(Composite parent) {
        Composite topLevel1 = new Composite(parent, SWT.NONE);
        topLevel1.setLayout(new GridLayout(2, false));
        topLevel1.setLayoutData(new GridData(
                GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
        Button fileSystemRadioButton = new Button(topLevel1, SWT.RADIO);
        GridData gridData = new GridData();
        gridData.horizontalSpan = 2;
        fileSystemRadioButton.setLayoutData(gridData);
        fileSystemRadioButton.setText(MessageUtil.getString(getClass(), "saveInFileSystem"));
        fileSystemRadioButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                Button button = (Button) event.getSource();
                changeOptions(button.getSelection());
            }
        });
        Label label = new Label(topLevel1, SWT.NONE);
        label.setText("");
        
        this.fileSelection = new FileSelectionWidget(topLevel1, SWT.NONE, this);
        
        Button workspaceRadioButton = new Button(topLevel1, SWT.RADIO);
        gridData = new GridData();
        gridData.horizontalSpan = 2;
        workspaceRadioButton.setLayoutData(gridData);
        workspaceRadioButton.setText(MessageUtil.getString(getClass(), "saveInWorkspace"));
        workspaceRadioButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                Button button = (Button) event.getSource();
                changeOptions(!button.getSelection());
            }
        });
        
        label = new Label(topLevel1, SWT.NONE);
        label.setText("");
        Composite composite = new Composite(topLevel1, SWT.NONE);
        composite.setLayout(new GridLayout());
        composite.setLayoutData(new GridData(GridData.FILL_BOTH));
        super.createControl(composite);
        
        Composite control = (Composite) getControl();
        findParts(control);
        
        setControl(topLevel1);
        
        fileSystemRadioButton.setSelection(true);
        changeOptions(true);
        this.fileSelection.addPropertyChangeListener(this);
    }
    
    /**
     * I worry that this approach to finding the sub-components is fragile, but the 
     * Eclipse API doesn't really give me a better alternative.
     * @param composite
     */
    private void findParts(Composite composite) {
        Control[] controls = composite.getChildren();
        for (int i = 0, length = (controls == null ? 0 : controls.length); i < length; i++) {
            if (controls[i] instanceof Tree) {
                this.resourceTree = (Tree) controls[i];
            } else if (controls[i] instanceof Composite) {
                findParts((Composite) controls[i]);
            } else if (controls[i] instanceof Text && this.resourceName != null) {
                this.workspaceFileName = (Text) controls[i];
            } else if (controls[i] instanceof Text && this.resourceName == null) {
                this.resourceName = (Text) controls[i];
            } else if (controls[i] instanceof Button) {
                Button button = (Button) controls[i];
                button.setVisible(false);
                button.setEnabled(false);
            }
        }
    }
    
    private void changeOptions(boolean fileSystem) {
        if (this.resourceName != null) {
            this.resourceName.setEnabled(!fileSystem);
        }
        if (this.resourceTree != null) {
            this.resourceTree.setEnabled(!fileSystem);
        }
        if (this.workspaceFileName != null) {
            this.workspaceFileName.setEnabled(!fileSystem);
        }

        this.fileSelection.setEnabled(fileSystem);
        if (fileSystem) {
            setPageComplete(this.fileSelection.getFile() != null);
        } else {
            setPageComplete(super.validatePage());
        }
        this.saveInFileSystem = fileSystem;
    }

    public void dispose() {
        super.dispose();
        this.fileSelection.removePropertyChangeListener(this);
    }
    /**
     * Returns a stream containing the initial contents to be given to new file resource
     * instances.  <b>Subclasses</b> may wish to override.  This default implementation
     * provides no initial contents.
     *
     * @return initial contents to be given to new file resource instances
     */
    protected InputStream getInitialContents() {
      return this.inputStreamProvider.getInputStream();
    }

    public void propertyChange(PropertyChangeEvent event) {
        if ("file".equals(event.getPropertyName())) {
            setPageComplete(event.getNewValue() != null);
        }
    }
    
    public Object createOutputFile() {
        if (this.saveInFileSystem) {
            return this.fileSelection.createFile(getInitialContents());
        } else {
            return createNewFile();
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.