1 /*******************************************************************************
2 * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
3 * program and the accompanying materials are made available under the terms of
4 * the Common Public License v1.0 which accompanies this distribution, and is
5 * available at http://www.eclipse.org/legal/cpl-v10.html
7 * Contributors: Klaus Hartlage - www.eclipseproject.de
8 ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
11 import java.sql.Connection;
12 import java.sql.DatabaseMetaData;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.ArrayList;
17 import net.sourceforge.phpeclipse.IPreferenceConstants;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.overlaypages.Util;
20 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextSelection;
31 import org.eclipse.jface.text.TextSelection;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.graphics.Point;
36 import org.eclipse.ui.IEditorActionDelegate;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IViewPart;
40 import org.eclipse.ui.IWorkbenchPage;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.actions.ActionDelegate;
44 import org.eclipse.ui.dialogs.ListSelectionDialog;
45 import org.eclipse.ui.internal.dialogs.ListContentProvider;
47 import com.quantum.QuantumPlugin;
48 import com.quantum.adapters.DatabaseAdapter;
49 import com.quantum.model.Bookmark;
50 import com.quantum.model.BookmarkCollection;
51 import com.quantum.model.Entity;
52 import com.quantum.model.EntityFactory;
53 import com.quantum.model.NotConnectedException;
54 import com.quantum.sql.MultiSQLServer;
55 import com.quantum.sql.SQLResultSetCollection;
56 import com.quantum.sql.SQLResultSetResults;
57 import com.quantum.sql.SQLResults;
58 import com.quantum.ui.dialog.ExceptionDisplayDialog;
59 import com.quantum.util.connection.ConnectionUtil;
60 import com.quantum.view.tableview.TableView;
62 public class PHPOpenSQLTableEditorAction extends ActionDelegate implements
63 IEditorActionDelegate {
65 private IWorkbenchWindow fWindow;
67 private PHPEditor fEditor;
69 private IProject fProject;
71 public void dispose() {
74 public void init(IWorkbenchWindow window) {
75 this.fWindow = window;
78 public void selectionChanged(IAction action, ISelection selection) {
79 if (!selection.isEmpty()) {
80 if (selection instanceof TextSelection) {
81 action.setEnabled(true);
82 } else if (fWindow.getActivePage() != null
83 && fWindow.getActivePage().getActivePart() != null) {
89 private IWorkbenchPage getActivePage() {
90 fWindow = fEditor.getEditorSite()
91 .getWorkbenchWindow();
92 IWorkbenchPage page = fWindow.getActivePage();
96 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
97 if (editorInput == null || editorInput.getFile() == null) {
100 return editorInput.getFile().getParent();
103 private IFile getIncludeFile(IProject project, IFileEditorInput editorInput,
104 String relativeFilename) {
105 // IContainer container = getWorkingLocation(editorInput);
106 // String fullPath = project.getLocation().toString();
107 Path path = new Path(relativeFilename);
108 IFile file = project.getFile(path);
112 public void run(IAction action) {
113 if (fEditor == null) {
114 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
115 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
116 fEditor = (PHPEditor) targetEditor;
119 if (fEditor != null) {
120 // TableView view = TableView.getInstance();
122 // determine the current Project from a (file-based) Editor
123 fWindow = fEditor.getEditorSite().getWorkbenchWindow();
124 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
125 fProject = f.getProject();
127 ITextSelection selection = (ITextSelection) fEditor
128 .getSelectionProvider().getSelection();
129 IDocument doc = fEditor.getDocumentProvider().getDocument(
130 fEditor.getEditorInput());
131 int pos = selection.getOffset();
132 // System.out.println(selection.getText());
133 String tableName = getSQLTableName(doc, pos);
135 IViewPart viewPart = null;
136 String view = "com.quantum.view.tableview.TableView";
138 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
139 viewPart = page.findView(view);
140 if (viewPart == null) {
141 viewPart = page.showView(view);
143 page.bringToTop(viewPart);
144 getTables((TableView) viewPart, fProject, tableName);
145 } catch (PartInitException e) {
152 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
153 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
154 fEditor = (PHPEditor) targetEditor;
158 private String getSQLTableName(IDocument doc, int pos) {
168 while (position >= 0) {
169 character = doc.getChar(position);
170 if (Character.isWhitespace(character) || (character == '\"')
171 || (character == '\'') || (character == '\r')
172 || (character == '\n'))
180 int length = doc.getLength();
182 while (position < length) {
183 character = doc.getChar(position);
184 if (Character.isWhitespace(character) || (character == '\"')
185 || (character == '\'') || (character == '\r')
186 || (character == '\n'))
195 word = new Point(start, end - start);
197 } catch (BadLocationException x) {
202 return doc.get(word.x, word.y);
203 } catch (BadLocationException e) {
209 public void getTables(TableView tableView, IProject project, String tableName) {
210 // Get The Database bookmark from the Quantum SQL plugin:
211 BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
212 if (sqlBookMarks != null) {
213 String bookmarkString = Util.getMiscProjectsPreferenceValue(project,
214 IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
215 if (bookmarkString != null && !bookmarkString.equals("")) {
216 Bookmark bookmark = sqlBookMarks.find(bookmarkString);
217 ArrayList sqlList = new ArrayList();
218 if (bookmark != null && !bookmark.isConnected()) {
219 new ConnectionUtil().connect(bookmark, null);
221 if (bookmark != null && bookmark.isConnected()) {
223 Connection connection = bookmark.getConnection();
224 DatabaseMetaData metaData = connection.getMetaData();
225 ConnectionUtil connectionUtil = new ConnectionUtil();
227 DatabaseAdapter adapter;
229 if (metaData != null) {
231 String prefixWithoutDollar = tableName;
232 if (prefixWithoutDollar.charAt(0) == '$') {
233 prefixWithoutDollar = prefixWithoutDollar.substring(1);
236 set = metaData.getTables(null, null, "%" + prefixWithoutDollar
239 tableName = set.getString("TABLE_NAME");
240 tableName = (tableName == null) ? "" : tableName.trim();
241 if (tableName != null && tableName.length() > 0) {
242 sqlList.add(tableName);
246 EntityFactory entityFactory = EntityFactory.getInstance();
247 if (sqlList.size() == 1) {
248 adapter = bookmark.getAdapter();
249 entity = entityFactory.create(bookmark, null, (String) sqlList
250 .get(0), Entity.TABLE_TYPE);
251 String query = adapter.getTableQuery(entity.getQualifiedName());
254 SQLResults results = MultiSQLServer.getInstance().execute(
255 bookmark, connectionUtil.connect(bookmark, fWindow.getShell()),
258 if (results != null && results.isResultSet()) {
259 SQLResultSetCollection.getInstance().addSQLResultSet(
260 (SQLResultSetResults) results);
262 } catch (SQLException e) {
263 ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
265 // tableView.loadTable(entityFactory.create(
267 // (String) sqlList.get(0),
268 // Entity.TABLE_TYPE));
269 } else if (sqlList.size() > 1) {
270 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
271 PHPeclipsePlugin.getDefault().getWorkbench()
272 .getActiveWorkbenchWindow().getShell(), sqlList,
273 new ListContentProvider(), new LabelProvider(),
274 "Select the SQL table to open.");
275 listSelectionDialog.setTitle("Multiple tablenames found");
276 if (listSelectionDialog.open() == Window.OK) {
277 Object[] locations = listSelectionDialog.getResult();
278 if (locations != null) {
279 for (int i = 0; i < locations.length; i++) {
280 adapter = bookmark.getAdapter();
281 entity = entityFactory.create(bookmark, null,
282 (String) locations[i], Entity.TABLE_TYPE);
283 String query = adapter.getTableQuery(entity
284 .getQualifiedName());
287 SQLResults results = MultiSQLServer.getInstance()
289 connectionUtil.connect(bookmark, fWindow.getShell()),
292 if (results != null && results.isResultSet()) {
293 SQLResultSetCollection.getInstance().addSQLResultSet(
294 (SQLResultSetResults) results);
296 } catch (SQLException e) {
297 ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
301 // .loadTable(entityFactory
305 // (String) locations[i],
306 // Entity.TABLE_TYPE));
313 } catch (NotConnectedException e) {
314 // ignore this - not mission critical
315 } catch (SQLException e) {