001: package com.quantum.csv.wizard;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.FileWriter;
008: import java.io.IOException;
009: import java.lang.reflect.InvocationTargetException;
010: import java.sql.SQLException;
011: import java.util.zip.Deflater;
012: import java.util.zip.ZipEntry;
013: import java.util.zip.ZipOutputStream;
014:
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.jface.dialogs.MessageDialog;
019: import org.eclipse.jface.operation.IRunnableWithProgress;
020: import org.eclipse.jface.preference.IPreferenceStore;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.jface.wizard.Wizard;
023: import org.eclipse.ui.IExportWizard;
024: import org.eclipse.ui.IWorkbench;
025: import org.eclipse.ui.PlatformUI;
026: import org.eclipse.ui.progress.IProgressService;
027:
028: import com.quantum.ImageStore;
029: import com.quantum.PluginPreferences;
030: import com.quantum.QuantumPlugin;
031: import com.quantum.flatfiles.ImageStoreKeys;
032: import com.quantum.flatfiles.MessageUtil;
033: import com.quantum.flatfiles.QuantumFlatFilesPlugin;
034: import com.quantum.model.Entity;
035: import com.quantum.ui.dialog.ExceptionDisplayDialog;
036: import com.quantum.util.connection.NotConnectedException;
037: import com.quantum.view.bookmark.BookmarkView;
038: import com.quantum.view.bookmark.EntityNode;
039:
040: /**
041: * @author BC Holmes
042: * @author Julen
043: */
044: public class ExportCSVWizard extends Wizard implements IExportWizard,
045: PropertyChangeListener {
046:
047: private ExportCSVDetailsPage page1;
048:
049: public ExportCSVWizard() {
050: setWindowTitle(MessageUtil.getString(getClass(), "windowTitle"));
051: setDefaultPageImageDescriptor(ImageStore.getImageDescriptor(
052: ImageStoreKeys.EXPORT_CSV, QuantumFlatFilesPlugin
053: .getDefault()));
054: }
055:
056: public boolean performFinish() {
057: IProgressService progressService = PlatformUI.getWorkbench()
058: .getProgressService();
059: File file = new File(this .page1.getFileName());
060:
061: // Check if the given file exists, and if it does, if it should be overwritten
062: // Quantum has a preference that we should check about this
063: IPreferenceStore store = QuantumPlugin.getDefault()
064: .getPreferenceStore();
065: boolean confirmOverwrite = store
066: .getBoolean(com.quantum.PluginPreferences.EXPORT_CONFIRM_OVERWRITE);
067:
068: if (!(new File(file.getParent()).isDirectory())) {
069: MessageDialog.openInformation(getShell(), "Cannot export",
070: "Your selected export file directory doesn't seem to exist:\n"
071: + file.getParent());
072: return false;
073: }
074:
075: if (!file.exists()
076: || !confirmOverwrite
077: || (file.exists() && MessageDialog.openConfirm(
078: getShell(), MessageUtil.getString(getClass(),
079: "overwriteTitle"), MessageUtil
080: .getString(getClass(),
081: "confirmOverwrite",
082: new Object[] { file
083: .getAbsoluteFile() })))) {
084: try {
085: progressService
086: .busyCursorWhile(new IRunnableWithProgress() {
087: public void run(IProgressMonitor monitor) {
088: doExport();
089: }
090: });
091: } catch (InvocationTargetException e) {
092: e.printStackTrace();
093: return false;
094: } catch (InterruptedException e) {
095: e.printStackTrace();
096: return false;
097: }
098: return true;
099: }
100: return false;
101: }
102:
103: /**
104: * @return
105: */
106: private boolean doExport() {
107: boolean successful = false;
108: try {
109: // Get the entities from the wizard data
110: Entity[] entities = this .page1.getEntities();
111: // If more than one entity, then will be a zip file
112: if (entities.length > 1) {
113: ZipOutputStream out = new ZipOutputStream(
114: new FileOutputStream(this .page1.getFileName()));
115: ConverterCSV converter = new ConverterCSV();
116: out.setLevel(Deflater.DEFAULT_COMPRESSION);
117: for (int i = 0; i < entities.length; i++) {
118: Entity entity = entities[i];
119: out.putNextEntry(new ZipEntry(entity.getName()
120: + ".csv"));
121: // convert() will do all the work
122: converter.convert(out, entity, this .page1
123: .isWriteHeaderRow(), this .page1
124: .getColumnSeparator());
125: out.closeEntry();
126: }
127: out.close();
128: }
129: // If only one entity, a regular file will do
130: else {
131: File outputFile = new File(this .page1.getFileName());
132: FileWriter writer = new FileWriter(outputFile);
133: ConverterCSV converter = new ConverterCSV();
134: // convert() will do all the work
135: converter.convert(writer, entities[0], this .page1
136: .isWriteHeaderRow(), this .page1
137: .getColumnSeparator());
138: writer.close();
139: }
140: successful = true;
141: } catch (IOException e) {
142: ExceptionDisplayDialog.openError(getShell(), null, null, e);
143: } catch (NotConnectedException e) {
144: e.printStackTrace();
145: } catch (SQLException e) {
146: e.printStackTrace();
147: } catch (Exception e) {
148: // TODO Auto-generated catch block
149: e.printStackTrace();
150: }
151:
152: return successful;
153: }
154:
155: public void init(IWorkbench workbench,
156: IStructuredSelection selection) {
157: Object[] nodes = BookmarkView.getInstance().getSelection()
158: .toArray();
159: if (nodes.length == 0) {
160: MessageDialog
161: .openInformation(getShell(), "Invalid selection",
162: "Please select some tables and views in the Quantum Bookmark View");
163: return;
164: }
165: for (int i = 0; i < nodes.length; i++) {
166: Object node = nodes[i];
167: if (!(node instanceof EntityNode)
168: || (!((EntityNode) node).isTable() && !((EntityNode) node)
169: .isView())) {
170: MessageDialog
171: .openInformation(getShell(),
172: "Invalid selection",
173: "Please select ONLY tables and/or views in the Quantum Bookmark View");
174: return;
175: }
176: }
177: this .page1 = new ExportCSVDetailsPage("page1");
178:
179: }
180:
181: public void dispose() {
182: super .dispose();
183: }
184:
185: /**
186: * @see org.eclipse.jface.wizard.IWizard#addPages()
187: */
188: public void addPages() {
189: if (this .page1 != null)
190: addPage(this .page1);
191: }
192:
193: /* (non-Javadoc)
194: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
195: */
196: public void propertyChange(PropertyChangeEvent arg0) {
197: }
198: }
|