001: package com.quantum.flatfiles.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.io.ByteArrayInputStream;
006: import java.io.InputStream;
007: import java.sql.SQLException;
008:
009: import org.eclipse.core.resources.IFile;
010: import org.eclipse.jface.viewers.IStructuredSelection;
011: import org.eclipse.jface.wizard.Wizard;
012: import org.eclipse.ui.IExportWizard;
013: import org.eclipse.ui.IWorkbench;
014:
015: import com.quantum.flatfiles.MessageUtil;
016: import com.quantum.model.Bookmark;
017: import com.quantum.model.Table;
018: import com.quantum.ui.dialog.ExceptionDisplayDialog;
019: import com.quantum.ui.dialog.SQLExceptionDialog;
020: import com.quantum.util.StringUtil;
021: import com.quantum.util.connection.ConnectionException;
022: import com.quantum.util.connection.NotConnectedException;
023: import com.quantum.util.versioning.VersioningHelper;
024:
025: /**
026: * @author BC Holmes
027: */
028: public class ExportDDLWizard extends Wizard implements IExportWizard,
029: InputStreamProvider, PropertyChangeListener {
030:
031: private ExportDDLWizardFileSelectionPage outputPage;
032: private ExportDatabaseObjectsWizardPage objectsPage;
033: private SelectBookmarkWizardPage bookmarkPage;
034:
035: public ExportDDLWizard() {
036: setWindowTitle(MessageUtil.getString(getClass(), "windowTitle"));
037: }
038:
039: public void init(IWorkbench workbench,
040: IStructuredSelection selection) {
041: this .bookmarkPage = new SelectBookmarkWizardPage("bookmark");
042: this .bookmarkPage.addPropertyChangeListener(this );
043: this .objectsPage = new ExportDatabaseObjectsWizardPage(
044: "objects");
045: this .outputPage = new ExportDDLWizardFileSelectionPage(
046: "outputLocation", this );
047: }
048:
049: /**
050: * @see org.eclipse.jface.wizard.IWizard#addPages()
051: */
052: public void addPages() {
053: super .addPages();
054: addPage(this .bookmarkPage);
055: addPage(this .objectsPage);
056: addPage(this .outputPage);
057: }
058:
059: public InputStream getInputStream() {
060: StringBuffer buffer = new StringBuffer();
061: Bookmark bookmark = this .objectsPage.getBookmark();
062: try {
063: Object[] exports = this .objectsPage.getCheckedElements();
064: for (int i = 0, length = exports == null ? 0
065: : exports.length; i < length; i++) {
066:
067: if (exports[i] instanceof Table) {
068: Table table = (Table) exports[i];
069: appendTableData(buffer, bookmark, table);
070: }
071: }
072:
073: } catch (SQLException e) {
074: SQLExceptionDialog.openException(getShell(), bookmark, e);
075: } catch (ConnectionException e) {
076: ExceptionDisplayDialog.openError(getShell(), null, null, e);
077: }
078: return new ByteArrayInputStream(buffer.toString().getBytes());
079: }
080:
081: /**
082: * @param buffer
083: * @param bookmark
084: * @param table
085: * @throws SQLException
086: * @throws NotConnectedException
087: */
088: private void appendTableData(StringBuffer buffer,
089: Bookmark bookmark, Table table)
090: throws NotConnectedException, SQLException {
091:
092: buffer.append("-- table ");
093: buffer.append(table.getQuotedTableName());
094: buffer.append(StringUtil.LINE_SEPARATOR);
095: buffer.append(bookmark.getAdapter().buildCreateTable(
096: bookmark,
097: table,
098: bookmark.getDatabase()
099: .supportsSchemasInTableDefinitions() ? bookmark
100: .getSchema(table.getSchema()) : bookmark
101: .getDefaultSchema(), false));
102: }
103:
104: /**
105: * This method is called when 'Finish' button is pressed in
106: * the wizard. We will create an operation and run it
107: * using wizard as execution context.
108: */
109: public boolean performFinish() {
110: Object file = this .outputPage.createOutputFile();
111: if (file != null && file instanceof IFile) {
112: VersioningHelper.openEditor(getShell(), (IFile) file);
113: }
114: return true;
115: }
116:
117: public void dispose() {
118: this .bookmarkPage.removePropertyChangeListener(this );
119: super .dispose();
120: }
121:
122: public void propertyChange(PropertyChangeEvent event) {
123: if ("bookmark".equals(event.getPropertyName())) {
124: this .objectsPage
125: .setBookmark((Bookmark) event.getNewValue());
126: }
127: }
128: }
|