added button to start tests.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / actions / ExportQueryAction.java
1 package com.quantum.actions;
2
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;
8
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;
15
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;
21
22 public class ExportQueryAction extends Action implements IViewActionDelegate  {
23         SQLQueryView view;
24         
25         /**
26          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
27          */
28         public void init(IViewPart view) {
29                 this.view = (SQLQueryView) view;                
30         }
31
32         /**
33          * @see org.eclipse.ui.IActionDelegate#run(IAction)
34          */
35         public void run(IAction action) {
36                 run();
37         }
38
39         public void run() {
40                 FileOutputStream out = ViewHelper.askSaveFile("exportquery", view.getSite().getShell(), 
41                                                                                                                 new String[]{"*.sql", "*.ddl", "*.*"},
42                                                                                                                 new String[]{
43                                                                                                                                         Messages.getString("filedialog.sqlFiles"),
44                                                                                                                                         Messages.getString("filedialog.ddlFiles"),
45                                                                                                                                         Messages.getString("filedialog.allfiles")
46                                                                                                                 });
47                 if (out == null)
48                         return;
49                         
50                 try {
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
65                                 else
66                                         ; // Do nothing, the end of line is already written 
67                                 else
68                                         writer.println(token); //Normal line
69                                 prevToken = token;
70                         }
71                         writer.close();
72                 } catch (IOException e) {
73                         LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
74                         e.printStackTrace();
75                 }
76         }
77
78         /**
79          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
80          */
81         public void selectionChanged(IAction action, ISelection selection) {
82         }
83 }