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