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
6 * Contributors: www.phpeclipse.de
7 **********************************************************************************************************************************/
8 package net.sourceforge.phpeclipse.wizards.actions;
10 import java.sql.SQLException;
12 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
13 import net.sourceforge.phpeclipse.ui.WebUI;
14 import net.sourceforge.phpeclipse.ui.overlaypages.ProjectPrefUtil;
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;
36 import com.quantum.ExternalInterface;
37 import com.quantum.QuantumPlugin;
38 // import com.quantum.util.connection.NotConnectedException;
40 public class PHPOpenSQLTableEditorAction extends ActionDelegate implements IEditorActionDelegate {
42 private IWorkbenchWindow fWindow;
44 private PHPEditor fEditor;
46 private IProject fProject;
48 public void dispose() {
51 public void init(IWorkbenchWindow window) {
52 this.fWindow = window;
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) {
65 private IWorkbenchPage getActivePage() {
66 fWindow = fEditor.getEditorSite().getWorkbenchWindow();
67 IWorkbenchPage page = fWindow.getActivePage();
71 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
72 if (editorInput == null || editorInput.getFile() == null) {
75 return editorInput.getFile().getParent();
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);
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;
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)
106 ExternalInterface.displayTable(bookmarkString, tableName);
108 IViewPart viewPart = null;
109 String view = "com.quantum.view.tableview.TableView";
111 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
112 viewPart = page.findView(view);
113 if (viewPart == null) {
114 viewPart = page.showView(view);
116 page.bringToTop(viewPart);
117 } catch (PartInitException e) {
120 } catch (SQLException e) {
121 // TODO Auto-generated catch block
123 } catch (Exception /* NotConnectedException */ e) {
124 // TODO Auto-generated catch block
130 // IViewPart viewPart = null;
131 // String view = "com.quantum.view.tableview.TableView";
133 // IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
134 // viewPart = page.findView(view);
135 // if (viewPart == null) {
136 // viewPart = page.showView(view);
138 // page.bringToTop(viewPart);
139 // getTables((TableView) viewPart, fProject, tableName);
140 // } catch (PartInitException e) {
141 // e.printStackTrace();
147 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
148 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
149 fEditor = (PHPEditor) targetEditor;
153 private String getSQLTableName(IDocument doc, int pos) {
163 while (position >= 0) {
164 character = doc.getChar(position);
165 if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
166 || (character == '\n'))
174 int length = doc.getLength();
176 while (position < length) {
177 character = doc.getChar(position);
178 if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
179 || (character == '\n'))
188 word = new Point(start, end - start);
190 } catch (BadLocationException x) {
195 return doc.get(word.x, word.y);
196 } catch (BadLocationException e) {
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);
214 // if (bookmark != null && bookmark.isConnected()) {
216 // Connection connection = bookmark.getConnection();
217 // DatabaseMetaData metaData = connection.getMetaData();
218 // ConnectionUtil connectionUtil = new ConnectionUtil();
220 // DatabaseAdapter adapter;
222 // if (metaData != null) {
223 // String columnName;
224 // String prefixWithoutDollar = tableName;
225 // if (prefixWithoutDollar.charAt(0) == '$') {
226 // prefixWithoutDollar = prefixWithoutDollar.substring(1);
229 // set = metaData.getTables(null, null, "%" + prefixWithoutDollar
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);
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());
247 // SQLResults results = MultiSQLServer.getInstance().execute(
248 // bookmark, connectionUtil.connect(bookmark, fWindow.getShell()),
251 // if (results != null && results.isResultSet()) {
252 // SQLResultSetCollection.getInstance().addSQLResultSet(
253 // (SQLResultSetResults) results);
255 // } catch (SQLException e) {
256 // ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
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());
280 // SQLResults results = MultiSQLServer.getInstance()
281 // .execute(bookmark,
282 // connectionUtil.connect(bookmark, fWindow.getShell()),
285 // if (results != null && results.isResultSet()) {
286 // SQLResultSetCollection.getInstance().addSQLResultSet(
287 // (SQLResultSetResults) results);
289 // } catch (SQLException e) {
290 // ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
294 // // .loadTable(entityFactory
298 // // (String) locations[i],
299 // // Entity.TABLE_TYPE));
306 // } catch (NotConnectedException e) {
307 // // ignore this - not mission critical
308 // } catch (SQLException e) {
309 // e.printStackTrace();