001: package com.quantum.dbunit.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.io.File;
006: import java.io.FileOutputStream;
007:
008: import org.eclipse.jface.dialogs.MessageDialog;
009: import org.eclipse.jface.preference.IPreferenceStore;
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.ImageStore;
016: import com.quantum.QuantumPlugin;
017: import com.quantum.dbunit.ImageStoreKeys;
018: import com.quantum.dbunit.MessageUtil;
019: import com.quantum.dbunit.QuantumDbUnitPlugin;
020: import com.quantum.model.Entity;
021: import com.quantum.ui.dialog.ExceptionDisplayDialog;
022: import com.quantum.view.bookmark.BookmarkView;
023: import com.quantum.view.bookmark.EntityNode;
024:
025: /**
026: * @author BC Holmes
027: * @author Julen
028: */
029: public class ExportDbUnitWizard extends Wizard implements
030: IExportWizard, PropertyChangeListener {
031:
032: private ExportDbUnitDetailsPage page1;
033:
034: public ExportDbUnitWizard() {
035: setWindowTitle(MessageUtil.getString(getClass(), "windowTitle"));
036: setDefaultPageImageDescriptor(ImageStore.getImageDescriptor(
037: ImageStoreKeys.EXPORT_DBUNIT, QuantumDbUnitPlugin
038: .getDefault()));
039: }
040:
041: public boolean performFinish() {
042:
043: boolean successful = false;
044: File file = new File(this .page1.getFileName());
045:
046: // Check if the given file exists, and if it does, if it should be overwritten
047: // Quantum has a preference that we should check about this
048: IPreferenceStore store = QuantumPlugin.getDefault()
049: .getPreferenceStore();
050: boolean confirmOverwrite = store
051: .getBoolean(com.quantum.PluginPreferences.EXPORT_CONFIRM_OVERWRITE);
052:
053: if (!file.exists()
054: || !confirmOverwrite
055: || (file.exists() && MessageDialog.openConfirm(
056: getShell(), MessageUtil.getString(getClass(),
057: "overwriteTitle"), MessageUtil
058: .getString(getClass(),
059: "confirmOverwrite",
060: new Object[] { file
061: .getAbsoluteFile() })))) {
062:
063: try {
064: // Get the entities from the wizard data
065: Entity[] entities = this .page1.getEntities();
066: File outputFile = new File(this .page1.getFileName());
067: FileOutputStream writer = new FileOutputStream(
068: outputFile);
069: ConverterDBUnit converter = new ConverterDBUnit();
070: // convert() will do all the work
071: converter.convert(writer, entities, this .page1
072: .getSchema(), this .page1.useDBSequence(),
073: this .page1.isQualifyNames(), getShell());
074: writer.close();
075: successful = true;
076: } catch (Exception e) {
077: e.printStackTrace();
078: ExceptionDisplayDialog.openError(getShell(), null,
079: null, e);
080: }
081: }
082: return successful;
083: }
084:
085: public void init(IWorkbench workbench,
086: IStructuredSelection selection) {
087: Object[] nodes = BookmarkView.getInstance().getSelection()
088: .toArray();
089: if (nodes.length == 0) {
090: MessageDialog
091: .openInformation(getShell(), "Invalid selection",
092: "Please select some tables or views in the Quantum Bookmark View");
093: return;
094: }
095: boolean oneTable = false;
096: boolean oneView = false;
097: boolean oneSynonym = false;
098: for (int i = 0; i < nodes.length; i++) {
099: Object node = nodes[i];
100: if (!(node instanceof EntityNode)
101: || (!((EntityNode) node).isTable() && !((EntityNode) node)
102: .isView())) {
103: MessageDialog
104: .openInformation(getShell(),
105: "Invalid selection",
106: "Please select ONLY Tables or Views in the Quantum Bookmark View");
107: return;
108: }
109: Entity entity = ((EntityNode) node).getEntity();
110: if (entity.isSynonym()) {
111: oneSynonym = true;
112: if (oneTable || oneView)
113: break;
114: } else if (entity.getType().equals(Entity.TABLE_TYPE)) {
115: oneTable = true;
116: if (oneSynonym || oneView)
117: break;
118: } else if (entity.getType().equals(Entity.VIEW_TYPE)) {
119: oneView = true;
120: if (oneSynonym || oneTable)
121: break;
122: }
123: }
124: if ((oneSynonym && (oneTable || oneView))
125: || (oneTable && oneView)) {
126: MessageDialog
127: .openInformation(getShell(), "Invalid selection",
128: "Sorry, you cannot mix Tables, Views or Synonyms in the same selection");
129: return;
130: }
131:
132: this .page1 = new ExportDbUnitDetailsPage("page1");
133:
134: }
135:
136: public void dispose() {
137: super .dispose();
138: }
139:
140: /**
141: * @see org.eclipse.jface.wizard.IWizard#addPages()
142: */
143: public void addPages() {
144: if (this .page1 != null)
145: addPage(this .page1);
146: }
147:
148: /* (non-Javadoc)
149: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
150: */
151: public void propertyChange(PropertyChangeEvent arg0) {
152: }
153: }
|