reworked all PHP icons to use the official PHP logo.
[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.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;
16
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;
22
23 public class ExportQueryAction extends Action implements IViewActionDelegate  {
24         SQLQueryView view;
25         
26         public ExportQueryAction() {
27                 setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.EXPORT));
28                 setText(Messages.getString("sqlqueryview.exportQuery"));
29                 setToolTipText(Messages.getString("sqlqueryview.exportQuery"));
30         }
31         
32         /**
33          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
34          */
35         public void init(IViewPart view) {
36                 this.view = (SQLQueryView) view;                
37         }
38
39         /**
40          * @see org.eclipse.ui.IActionDelegate#run(IAction)
41          */
42         public void run(IAction action) {
43                 run();
44         }
45
46         public void run() {
47                 FileOutputStream out = ViewHelper.askSaveFile("exportquery", view.getSite().getShell(), 
48                                                                                                                 new String[]{"*.sql", "*.ddl", "*.*"},
49                                                                                                                 new String[]{
50                                                                                                                                         Messages.getString("filedialog.sqlFiles"),
51                                                                                                                                         Messages.getString("filedialog.ddlFiles"),
52                                                                                                                                         Messages.getString("filedialog.allfiles")
53                                                                                                                 });
54                 if (out == null)
55                         return;
56                         
57                 try {
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
72                                 else
73                                         ; // Do nothing, the end of line is already written 
74                                 else
75                                         writer.println(token); //Normal line
76                                 prevToken = token;
77                         }
78                         writer.close();
79                 } catch (IOException e) {
80                         LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
81                         e.printStackTrace();
82                 }
83         }
84
85         /**
86          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
87          */
88         public void selectionChanged(IAction action, ISelection selection) {
89         }
90 }