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

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

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.sql.SQLException;
import java.util.Arrays;

import com.quantum.ImageStore;
import com.quantum.QuantumPlugin;
import com.quantum.flatfiles.MessageUtil;
import com.quantum.model.Bookmark;
import com.quantum.model.Schema;
import com.quantum.ui.dialog.ExceptionDisplayDialog;
import com.quantum.ui.dialog.SQLExceptionDialog;
import com.quantum.util.DisplayableComparator;
import com.quantum.util.connection.NotConnectedException;
import com.quantum.view.widget.SimpleLabelProvider;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


/**
 * @author BC Holmes
 */
public class ExportDetailsPage extends WizardPage {
  
  public class ContentProviderImpl implements IStructuredContentProvider, PropertyChangeListener {
    
    public Object[] getElements(Object inputElement) {
      if (inputElement == null) {
        return null;
      } else if (inputElement instanceof Bookmark 
          && ((Bookmark) inputElement).isConnected()) {
        Schema[] schemas = new Schema[0];
        try {
          schemas = ((Bookmark) inputElement).getSchemas();
          Arrays.sort(schemas, new DisplayableComparator());
        } catch (NotConnectedException e) {
          ExceptionDisplayDialog.openError(getShell(), null, null, e);
        } catch (SQLException e) {
          SQLExceptionDialog.openException(getShell(), (Bookmark) inputElement, e);
        }
        return schemas;
      } else if (inputElement instanceof Bookmark) {
        return new Schema[0];
      } else {
        return null;
      }
    }

    public void dispose() {
    }

    public void propertyChange(PropertyChangeEvent event) {
      if ("connected".equals(event.getPropertyName())) {
        refreshTable();
      }
    }
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      if (newInput != null && newInput instanceof Bookmark) {
        if (oldInput != null && oldInput instanceof Bookmark) {
          ((Bookmark) oldInput).removePropertyChangeListener(this);
        }
        
        ((Bookmark) newInput).addPropertyChangeListener(this);
      }
    }
  }
  
  private Bookmark bookmark;
  private Schema schema;
  private String idMethod;
  private String fileName;
  private String databaseName;
  private TableViewer tableViewer;
  private ContentProviderImpl contentProvider;
  
  protected void refreshTable() {
    if (this.bookmark.isConnected()) {
      this.tableViewer.refresh();
    }
  }
  
  public void dispose() {
    if (this.bookmark != null) {
      this.bookmark.removePropertyChangeListener(this.contentProvider);
    }
    super.dispose();
  }

  /**
   * @param pageName
   */
  protected ExportDetailsPage(String pageName) {
    super(pageName);
    
    setTitle(MessageUtil.getString(ExportDetailsPage.class, "title"));
    setDescription(MessageUtil.getString(ExportDetailsPage.class, "description"));
  }

  public void createControl(Composite parent) {
    
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));
    
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    
    Label label = new Label(composite, SWT.NONE);
    label.setText(MessageUtil.getString(ExportDetailsPage.class, "schema"));
    label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
    
    this.tableViewer = new TableViewer(composite);
    this.tableViewer.setLabelProvider(new SimpleLabelProvider(
        ImageStore.getImage(ImageStore.SCHEMA, QuantumPlugin.getDefault())));
    this.tableViewer.setContentProvider(this.contentProvider = new ContentProviderImpl());
    if (this.bookmark != null) {
      this.tableViewer.setInput(this.bookmark);
    }
    this.tableViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
    
    this.tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
      public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = 
          (IStructuredSelection) event.getSelection();
        setSchema(selection.isEmpty() 
            ? null : (Schema) selection.getFirstElement());
        updateState();
      }
    });
    
    Group group = new Group(composite, SWT.NONE);
    group.setText(MessageUtil.getString(ExportDetailsPage.class, "torque"));
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    label = new Label(group, SWT.NONE);
    label.setText(MessageUtil.getString(ExportDetailsPage.class, "databaseName"));
    
    Text databaseName = new Text(group, SWT.BORDER);
    databaseName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    databaseName.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent event) {
        setDatabaseName(((Text) event.getSource()).getText());
        updateState();
      }
    });
    
    label = new Label(group, SWT.NONE);
    label.setText(MessageUtil.getString(ExportDetailsPage.class, "idMethod"));
    
    Combo idMethodCombo = new Combo(group, SWT.READ_ONLY);
    idMethodCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    idMethodCombo.setItems(new String[] { "none", "native", "idbroker" });
    
    idMethodCombo.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        setIdMethod(((Combo) event.getSource()).getText());
        updateState();
      }
    });
    
    Label blankArea = new Label(composite, SWT.NONE);
    blankArea.setText("");
    
    createDestinationArea(composite);
    
    setControl(composite);
    updateState();
  }

    private void createDestinationArea(Composite composite) {
        Composite fileArea = new Composite(composite, SWT.NULL);
        fileArea.setLayout(new GridLayout(3, false));
        fileArea.setLayoutData(
            new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
        Label label = new Label(fileArea, SWT.NONE);
        label.setText(MessageUtil.getString(ExportDetailsPage.class, "fileName"));
        
        final Text fileNameText = new Text(fileArea, SWT.BORDER);
        fileNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        fileNameText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent event) {
                String text = ((Text) event.getSource()).getText();
                setFileName(text);
                updateState();
            }
        });
        
        Button button = new Button(fileArea, SWT.NONE);
        button.setText(MessageUtil.getString(ExportDetailsPage.class, "browse"));
        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
                dialog.setFilterExtensions(new String[] { "xml" });
                dialog.setFilterNames(new String[] { 
                    MessageUtil.getString(ExportDetailsPage.class, "xmlFiles") });
                String filename = dialog.open();
                if (filename != null) {
                    fileNameText.setText(filename);
                    setFileName(filename);
                    updateState();
                }
            }
        });
    }
    
  protected void updateState() {
    boolean pageComplete = (this.bookmark != null && this.bookmark.isConnected());
    pageComplete &= (this.databaseName != null && this.databaseName.trim().length() > 0);
    pageComplete &= (this.fileName != null && !(new File(this.fileName)).isDirectory());
    pageComplete &= (this.idMethod != null);
    pageComplete &= (this.schema != null);

    setPageComplete(pageComplete);
  }
  
  public Bookmark getBookmark() {
    return this.bookmark;
  }
  public String getFileName() {
    return this.fileName;
  }
  protected void setFileName(String fileName) {
    this.fileName = fileName;
  }
  public String getDatabaseName() {
    return this.databaseName;
  }
  public void setBookmark(Bookmark bookmark) {
    this.bookmark = bookmark;
    this.tableViewer.setInput(this.bookmark);
  }
  public Schema getSchema() {
    return this.schema;
  }
  public void setSchema(Schema schema) {
    this.schema = schema;
  }
  public void setDatabaseName(String databaseName) {
    this.databaseName = databaseName;
  }
  public String getIdMethod() {
    return this.idMethod;
  }
  public void setIdMethod(String idMethod) {
    this.idMethod = idMethod;
  }
}
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.