1 package com.quantum.actions;
3 import java.io.FileOutputStream;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.util.StringTokenizer;
9 import com.quantum.ImageStore;
10 import com.quantum.Messages;
11 import com.quantum.util.StringUtil;
12 import com.quantum.view.LogProxy;
13 import com.quantum.view.SQLLogView;
14 import com.quantum.view.SQLQueryView;
15 import com.quantum.view.ViewHelper;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.IViewActionDelegate;
21 import org.eclipse.ui.IViewPart;
23 public class ExportQueryAction extends Action implements IViewActionDelegate {
26 public ExportQueryAction() {
27 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.EXPORT));
28 setText(Messages.getString("sqlqueryview.exportQuery"));
29 setToolTipText(Messages.getString("sqlqueryview.exportQuery"));
33 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
35 public void init(IViewPart view) {
36 this.view = (SQLQueryView) view;
40 * @see org.eclipse.ui.IActionDelegate#run(IAction)
42 public void run(IAction action) {
47 FileOutputStream out = ViewHelper.askSaveFile("exportquery", view.getSite().getShell(),
48 new String[]{"*.sql", "*.ddl", "*.*"},
50 Messages.getString("filedialog.sqlFiles"),
51 Messages.getString("filedialog.ddlFiles"),
52 Messages.getString("filedialog.allfiles")
58 FileWriter fileWriter = new FileWriter(out.getFD());
59 PrintWriter writer = new PrintWriter(fileWriter);
60 String output = view.getQuery();
61 output = StringUtil.substituteString(output, "\r", "");
62 StringTokenizer tokenizer = new StringTokenizer(output, "\n", true); //$NON-NLS-1$
63 String prevToken = "";
64 while (tokenizer.hasMoreElements()) {
65 String token = (String) tokenizer.nextElement();
66 // If it's a normal line end, we won't write it, because the println() will
67 // adapting it to the OS (have to test that). But if it's a line end after
68 // another, then it's a blank line.
69 if (token.equals("\n"))
70 if (prevToken.equals("\n"))
71 writer.println(); // Two consecutives "\n", means a separate line
73 ; // Do nothing, the end of line is already written
75 writer.println(token); //Normal line
79 } catch (IOException e) {
80 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
86 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
88 public void selectionChanged(IAction action, ISelection selection) {