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.Messages;
10 import com.quantum.util.StringUtil;
11 import com.quantum.view.LogProxy;
12 import com.quantum.view.SQLLogView;
13 import com.quantum.view.SQLQueryView;
14 import com.quantum.view.ViewHelper;
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.ui.IViewActionDelegate;
20 import org.eclipse.ui.IViewPart;
22 public class ExportQueryAction extends Action implements IViewActionDelegate {
26 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
28 public void init(IViewPart view) {
29 this.view = (SQLQueryView) view;
33 * @see org.eclipse.ui.IActionDelegate#run(IAction)
35 public void run(IAction action) {
40 FileOutputStream out = ViewHelper.askSaveFile("exportquery", view.getSite().getShell(),
41 new String[]{"*.sql", "*.ddl", "*.*"},
43 Messages.getString("filedialog.sqlFiles"),
44 Messages.getString("filedialog.ddlFiles"),
45 Messages.getString("filedialog.allfiles")
51 FileWriter fileWriter = new FileWriter(out.getFD());
52 PrintWriter writer = new PrintWriter(fileWriter);
53 String output = view.getQuery();
54 output = StringUtil.substituteString(output, "\r", "");
55 StringTokenizer tokenizer = new StringTokenizer(output, "\n", true); //$NON-NLS-1$
56 String prevToken = "";
57 while (tokenizer.hasMoreElements()) {
58 String token = (String) tokenizer.nextElement();
59 // If it's a normal line end, we won't write it, because the println() will
60 // adapting it to the OS (have to test that). But if it's a line end after
61 // another, then it's a blank line.
62 if (token.equals("\n"))
63 if (prevToken.equals("\n"))
64 writer.println(); // Two consecutives "\n", means a separate line
66 ; // Do nothing, the end of line is already written
68 writer.println(token); //Normal line
72 } catch (IOException e) {
73 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
79 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
81 public void selectionChanged(IAction action, ISelection selection) {