5eda806071b4826b09b0e2c94a15379a728c3648
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenSQLTableEditorAction.java
1 /***********************************************************************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This program and the accompanying materials are made
3  * available under the terms of the Common Public License v1.0 which accompanies this distribution, and is available at
4  * http://www.eclipse.org/legal/cpl-v10.html
5  * 
6  * Contributors: www.phpeclipse.de
7  **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.actions;
9
10 import java.sql.SQLException;
11
12 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
13 import net.sourceforge.phpeclipse.ui.WebUI;
14 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.text.TextSelection;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.ui.IEditorActionDelegate;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.IViewPart;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.actions.ActionDelegate;
35
36 import com.quantum.ExternalInterface;
37 import com.quantum.QuantumPlugin;
38 import com.quantum.model.NotConnectedException;
39 import com.quantum.view.tableview.TableView;
40
41 public class PHPOpenSQLTableEditorAction extends ActionDelegate implements IEditorActionDelegate {
42
43   private IWorkbenchWindow fWindow;
44
45   private PHPEditor fEditor;
46
47   private IProject fProject;
48
49   public void dispose() {
50   }
51
52   public void init(IWorkbenchWindow window) {
53     this.fWindow = window;
54   }
55
56   public void selectionChanged(IAction action, ISelection selection) {
57     if (!selection.isEmpty()) {
58       if (selection instanceof TextSelection) {
59         action.setEnabled(true);
60       } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
61         //
62       }
63     }
64   }
65
66   private IWorkbenchPage getActivePage() {
67     fWindow = fEditor.getEditorSite().getWorkbenchWindow();
68     IWorkbenchPage page = fWindow.getActivePage();
69     return page;
70   }
71
72   public IContainer getWorkingLocation(IFileEditorInput editorInput) {
73     if (editorInput == null || editorInput.getFile() == null) {
74       return null;
75     }
76     return editorInput.getFile().getParent();
77   }
78
79   private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
80     //          IContainer container = getWorkingLocation(editorInput);
81     //          String fullPath = project.getLocation().toString();
82     Path path = new Path(relativeFilename);
83     IFile file = project.getFile(path);
84     return file;
85   }
86
87   public void run(IAction action) {
88     if (fEditor == null) {
89       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
90       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
91         fEditor = (PHPEditor) targetEditor;
92       }
93     }
94     if (fEditor != null) {
95       fWindow = fEditor.getEditorSite().getWorkbenchWindow();
96       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
97       fProject = f.getProject();
98       String bookmarkString = ProjectPrefUtil.getMiscProjectsPreferenceValue(fProject, WebUI.PHP_BOOKMARK_DEFAULT);
99       if (bookmarkString != null && !bookmarkString.equals("")) {
100         ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
101         IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
102         int pos = selection.getOffset();
103         //  System.out.println(selection.getText());
104         String tableName = getSQLTableName(doc, pos);
105         if (tableName != null && tableName.length() > 0)
106           try {
107             ExternalInterface.displayTable(bookmarkString, tableName);
108             
109             IViewPart viewPart = null;
110             String view = "com.quantum.view.tableview.TableView";
111             try {
112               IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
113               viewPart = page.findView(view);
114               if (viewPart == null) {
115                 viewPart = page.showView(view);
116               }
117               page.bringToTop(viewPart);
118             } catch (PartInitException e) {
119               e.printStackTrace();
120             }
121           } catch (NotConnectedException e) {
122             // TODO Auto-generated catch block
123             e.printStackTrace();
124           } catch (SQLException e) {
125             // TODO Auto-generated catch block
126             e.printStackTrace();
127           }
128       }
129
130       //
131       //      IViewPart viewPart = null;
132       //      String view = "com.quantum.view.tableview.TableView";
133       //      try {
134       //        IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
135       //        viewPart = page.findView(view);
136       //        if (viewPart == null) {
137       //          viewPart = page.showView(view);
138       //        }
139       //        page.bringToTop(viewPart);
140       //        getTables((TableView) viewPart, fProject, tableName);
141       //      } catch (PartInitException e) {
142       //        e.printStackTrace();
143       //      }
144
145     }
146   }
147
148   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
149     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
150       fEditor = (PHPEditor) targetEditor;
151     }
152   }
153
154   private String getSQLTableName(IDocument doc, int pos) {
155     Point word = null;
156     int start = -1;
157     int end = -1;
158
159     try {
160
161       int position = pos;
162       char character;
163
164       while (position >= 0) {
165         character = doc.getChar(position);
166         if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
167             || (character == '\n'))
168           break;
169         --position;
170       }
171
172       start = position;
173
174       position = pos;
175       int length = doc.getLength();
176
177       while (position < length) {
178         character = doc.getChar(position);
179         if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
180             || (character == '\n'))
181           break;
182         ++position;
183       }
184
185       start++;
186       end = position;
187
188       if (end > start)
189         word = new Point(start, end - start);
190
191     } catch (BadLocationException x) {
192     }
193
194     if (word != null) {
195       try {
196         return doc.get(word.x, word.y);
197       } catch (BadLocationException e) {
198       }
199     }
200     return "";
201   }
202
203   //  public void getTables(TableView tableView, IProject project, String tableName) {
204   //    // Get The Database bookmark from the Quantum SQL plugin:
205   //    BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
206   //    if (sqlBookMarks != null) {
207   //      String bookmarkString = ProjectPrefUtil.getMiscProjectsPreferenceValue(project,
208   //          WebUI.PHP_BOOKMARK_DEFAULT);
209   //      if (bookmarkString != null && !bookmarkString.equals("")) {
210   //        Bookmark bookmark = sqlBookMarks.find(bookmarkString);
211   //        ArrayList sqlList = new ArrayList();
212   //        if (bookmark != null && !bookmark.isConnected()) {
213   //          new ConnectionUtil().connect(bookmark, null);
214   //        }
215   //        if (bookmark != null && bookmark.isConnected()) {
216   //          try {
217   //            Connection connection = bookmark.getConnection();
218   //            DatabaseMetaData metaData = connection.getMetaData();
219   //            ConnectionUtil connectionUtil = new ConnectionUtil();
220   //            Entity entity;
221   //            DatabaseAdapter adapter;
222   //
223   //            if (metaData != null) {
224   //              String columnName;
225   //              String prefixWithoutDollar = tableName;
226   //              if (prefixWithoutDollar.charAt(0) == '$') {
227   //                prefixWithoutDollar = prefixWithoutDollar.substring(1);
228   //              }
229   //              ResultSet set;
230   //              set = metaData.getTables(null, null, "%" + prefixWithoutDollar
231   //                  + "%", null);
232   //              while (set.next()) {
233   //                tableName = set.getString("TABLE_NAME");
234   //                tableName = (tableName == null) ? "" : tableName.trim();
235   //                if (tableName != null && tableName.length() > 0) {
236   //                  sqlList.add(tableName);
237   //                }
238   //              }
239   //              set.close();
240   //              EntityFactory entityFactory = EntityFactory.getInstance();
241   //              if (sqlList.size() == 1) {
242   //                adapter = bookmark.getAdapter();
243   //                entity = entityFactory.create(bookmark, null, (String) sqlList
244   //                    .get(0), Entity.TABLE_TYPE, false);
245   //                String query = adapter.getTableQuery(entity.getQualifiedName());
246   //
247   //                try {
248   //                  SQLResults results = MultiSQLServer.getInstance().execute(
249   //                      bookmark, connectionUtil.connect(bookmark, fWindow.getShell()),
250   //                      entity, query);
251   //
252   //                  if (results != null && results.isResultSet()) {
253   //                    SQLResultSetCollection.getInstance().addSQLResultSet(
254   //                        (SQLResultSetResults) results);
255   //                  }
256   //                } catch (SQLException e) {
257   //                  ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
258   //                }
259   //                // tableView.loadTable(entityFactory.create(
260   //                // bookmark, null,
261   //                // (String) sqlList.get(0),
262   //                // Entity.TABLE_TYPE));
263   //              } else if (sqlList.size() > 1) {
264   //                ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
265   //                    PHPeclipsePlugin.getDefault().getWorkbench()
266   //                        .getActiveWorkbenchWindow().getShell(), sqlList,
267   //                    new ListContentProvider(), new LabelProvider(),
268   //                    "Select the SQL table to open.");
269   //                listSelectionDialog.setTitle("Multiple tablenames found");
270   //                if (listSelectionDialog.open() == Window.OK) {
271   //                  Object[] locations = listSelectionDialog.getResult();
272   //                  if (locations != null) {
273   //                    for (int i = 0; i < locations.length; i++) {
274   //                      adapter = bookmark.getAdapter();
275   //                      entity = entityFactory.create(bookmark, null,
276   //                          (String) locations[i], Entity.TABLE_TYPE, false);
277   //                      String query = adapter.getTableQuery(entity
278   //                          .getQualifiedName());
279   //
280   //                      try {
281   //                        SQLResults results = MultiSQLServer.getInstance()
282   //                            .execute(bookmark,
283   //                                connectionUtil.connect(bookmark, fWindow.getShell()),
284   //                                entity, query);
285   //
286   //                        if (results != null && results.isResultSet()) {
287   //                          SQLResultSetCollection.getInstance().addSQLResultSet(
288   //                              (SQLResultSetResults) results);
289   //                        }
290   //                      } catch (SQLException e) {
291   //                        ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
292   //                      }
293   //
294   //                      // tableView
295   //                      // .loadTable(entityFactory
296   //                      // .create(
297   //                      // bookmark,
298   //                      // null,
299   //                      // (String) locations[i],
300   //                      // Entity.TABLE_TYPE));
301   //                    }
302   //
303   //                  }
304   //                }
305   //              }
306   //            }
307   //          } catch (NotConnectedException e) {
308   //            // ignore this - not mission critical
309   //          } catch (SQLException e) {
310   //            e.printStackTrace();
311   //          }
312   //        }
313   //      }
314   //    }
315   //  }
316
317 }