intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / actions / GeneratePHPAction.java
1 package net.sourceforge.phpdt.sql.actions;
2
3 import java.io.File;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.util.StringTokenizer;
8 import java.util.Vector;
9
10 import org.eclipse.jface.action.Action;
11 import org.eclipse.jface.action.IAction;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.FileDialog;
15 import org.eclipse.ui.IViewActionDelegate;
16 import org.eclipse.ui.IViewPart;
17
18 import net.sourceforge.phpdt.sql.Messages;
19 import net.sourceforge.phpdt.sql.utils.PHPStringThing;
20 import net.sourceforge.phpdt.sql.view.BookmarkView;
21 import net.sourceforge.phpdt.sql.view.LogProxy;
22 import net.sourceforge.phpdt.sql.view.SQLLogView;
23 import net.sourceforge.phpdt.sql.view.SQLQueryView;
24 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
25
26 public class GeneratePHPAction extends Action implements IViewActionDelegate {
27         SQLQueryView view;
28         FileDialog dialog;
29         /**
30          * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
31          */
32         public void init(IViewPart view) {
33                 this.view = (SQLQueryView) view;
34                 dialog = new FileDialog(view.getSite().getShell(), SWT.SAVE);
35                 dialog.setFilterExtensions(new String[] { "*.php", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
36                 dialog.setFilterNames(new String[] { Messages.getString("filedialog.phpFiles"), //$NON-NLS-1$
37                         Messages.getString("filedialog.allfiles")}); //$NON-NLS-1$ //$NON-NLS-2$
38         }
39
40         /**
41          * @see org.eclipse.ui.IActionDelegate#run(IAction)
42          */
43         public void run(IAction action) {
44                 run();
45         }
46
47         public void run() {
48 //TODO
49 /*              String filename = dialog.open();
50                 if (filename != null) {
51                         try {
52                                 /*Check for the presence of a "." - either indicates an
53                                 * extension has been provided or that a filename with a '.'
54                                 * has been specified - if the latter, it is assumed the user
55                                 * knows what they're doing - could be dangerous! :-)
56                                 */
57 /*                              if (filename.indexOf(".") == -1)
58                                         filename += ".php";
59                                 File exportFile = new File(filename);
60                                 FileWriter fileWriter = new FileWriter(exportFile);
61                                 PrintWriter writer = new PrintWriter(fileWriter);
62                                 String query = view.getQuery();
63                                 Vector phpStrings = PHPStringThing.toPHP(query);
64
65                                 while (!phpStrings.isEmpty()) {
66                                         String buffer = (String) phpStrings.remove(0);
67                                         BookmarkNode current =
68                                                 BookmarkView.getInstance().getCurrentBookmark();
69
70                                         buffer.replaceAll(
71                                                 "%%CONNLIST%%",
72                                                 current.getConnect()
73                                                         + ", "
74                                                         + current.getUsername()
75                                                         + ", "
76                                                         + current.getPassword());
77                                         buffer.replaceAll("%%DBNAME%%", current.getName());
78
79                                         /*Find the %%NL%% templates and substitute in the correct
80                                          * newline character for the platform - println does this
81                                          * automagically!
82                                          */
83 /*                                      StringTokenizer tokenizer =
84                                                 new StringTokenizer(buffer, "%%NL%%");
85                                         while (tokenizer.hasMoreElements()) {
86                                                 String line = (String) tokenizer.nextElement();
87                                                 writer.println(line);
88                                         }
89                                 }
90                                 writer.close();
91                         } catch (IOException e) {
92                                 LogProxy.getInstance().addText(SQLLogView.ERROR, e.toString());
93                                 e.printStackTrace();
94                         }
95                 }
96 */      }
97
98         /**
99          * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
100          */
101         public void selectionChanged(IAction action, ISelection selection) {
102         }
103 }