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.model.Bookmark;
49 import com.quantum.model.BookmarkCollection;
50 import com.quantum.model.Entity;
51 import com.quantum.model.EntityFactory;
52 import com.quantum.model.NotConnectedException;
53 import com.quantum.util.connection.ConnectionUtil;
54 import com.quantum.view.tableview.TableView;
56 public class PHPOpenSQLTableEditorAction extends ActionDelegate
58 IEditorActionDelegate {
60 private IWorkbenchWindow fWindow;
61 private PHPEditor fEditor;
62 private IProject fProject;
64 public void dispose() {
67 public void init(IWorkbenchWindow window) {
68 this.fWindow = window;
71 public void selectionChanged(IAction action, ISelection selection) {
72 if (!selection.isEmpty()) {
73 if (selection instanceof TextSelection) {
74 action.setEnabled(true);
75 } else if (fWindow.getActivePage() != null
76 && fWindow.getActivePage().getActivePart() != null) {
81 private IWorkbenchPage getActivePage() {
82 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
83 .getWorkbenchWindow();
84 IWorkbenchPage page = workbenchWindow.getActivePage();
87 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
88 if (editorInput == null || editorInput.getFile() == null) {
91 return editorInput.getFile().getParent();
93 private IFile getIncludeFile(IProject project,
94 IFileEditorInput editorInput, String relativeFilename) {
95 // IContainer container = getWorkingLocation(editorInput);
96 // String fullPath = project.getLocation().toString();
97 Path path = new Path(relativeFilename);
98 IFile file = project.getFile(path);
102 public void run(IAction action) {
103 if (fEditor == null) {
104 IEditorPart targetEditor = fWindow.getActivePage()
106 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
107 fEditor = (PHPEditor) targetEditor;
110 if (fEditor != null) {
111 // TableView view = TableView.getInstance();
113 // determine the current Project from a (file-based) Editor
114 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
115 fProject = f.getProject();
117 ITextSelection selection = (ITextSelection) fEditor
118 .getSelectionProvider().getSelection();
119 IDocument doc = fEditor.getDocumentProvider().getDocument(
120 fEditor.getEditorInput());
121 int pos = selection.getOffset();
122 // System.out.println(selection.getText());
123 String tableName = getSQLTableName(doc, pos);
125 IViewPart viewPart = null;
126 String view = "com.quantum.view.tableview.TableView";
128 IWorkbenchPage page = QuantumPlugin.getDefault()
130 viewPart = page.findView(view);
131 if (viewPart == null) {
132 viewPart = page.showView(view);
134 page.bringToTop(viewPart);
135 getTables((TableView) viewPart, fProject, tableName);
136 } catch (PartInitException e) {
143 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
144 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
145 fEditor = (PHPEditor) targetEditor;
149 private String getSQLTableName(IDocument doc, int pos) {
159 while (position >= 0) {
160 character = doc.getChar(position);
161 if (Character.isWhitespace(character) || (character == '\"')
162 || (character == '\'') || (character == '\r')
163 || (character == '\n'))
171 int length = doc.getLength();
173 while (position < length) {
174 character = doc.getChar(position);
175 if (Character.isWhitespace(character) || (character == '\"')
176 || (character == '\'') || (character == '\r')
177 || (character == '\n'))
186 word = new Point(start, end - start);
188 } catch (BadLocationException x) {
193 return doc.get(word.x, word.y);
194 } catch (BadLocationException e) {
200 public void getTables(TableView tableView, IProject project,
202 // Get The Database bookmark from the Quantum SQL plugin:
203 BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
204 if (sqlBookMarks != null) {
205 String bookmarkString = Util.getMiscProjectsPreferenceValue(
206 project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
207 if (bookmarkString != null && !bookmarkString.equals("")) {
208 Bookmark bookmark = sqlBookMarks.find(bookmarkString);
209 ArrayList sqlList = new ArrayList();
210 if (bookmark != null && !bookmark.isConnected()) {
211 new ConnectionUtil().connect(bookmark, null);
213 if (bookmark != null && bookmark.isConnected()) {
215 Connection connection = bookmark.getConnection();
216 DatabaseMetaData metaData = connection.getMetaData();
218 if (metaData != null) {
220 String prefixWithoutDollar = tableName;
221 if (prefixWithoutDollar.charAt(0) == '$') {
222 prefixWithoutDollar = prefixWithoutDollar
226 set = metaData.getTables(null, null, "%"
227 + prefixWithoutDollar+"%", null);
229 tableName = set.getString("TABLE_NAME");
230 tableName = (tableName == null)
233 if (tableName != null && tableName.length() > 0) {
234 sqlList.add(tableName);
238 EntityFactory entityFactory = EntityFactory
240 if (sqlList.size() == 1) {
241 tableView.loadTable(entityFactory.create(
243 (String) sqlList.get(0),
245 } else if (sqlList.size() > 1) {
246 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
247 PHPeclipsePlugin.getDefault()
249 .getActiveWorkbenchWindow()
250 .getShell(), sqlList,
251 new ListContentProvider(),
253 "Select the SQL to open.");
255 .setTitle("Multiple tablenames found");
256 if (listSelectionDialog.open() == Window.OK) {
257 Object[] locations = listSelectionDialog
259 if (locations != null) {
260 for (int i = 0; i < locations.length; i++) {
262 .loadTable(entityFactory
266 (String) locations[i],
274 } catch (NotConnectedException e) {
275 // ignore this - not mission critical
276 } catch (SQLException e) {