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.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.model.NotConnectedException;
39 import com.quantum.view.tableview.TableView;
41 public class PHPOpenSQLTableEditorAction extends ActionDelegate implements IEditorActionDelegate {
43 private IWorkbenchWindow fWindow;
45 private PHPEditor fEditor;
47 private IProject fProject;
49 public void dispose() {
52 public void init(IWorkbenchWindow window) {
53 this.fWindow = window;
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) {
66 private IWorkbenchPage getActivePage() {
67 fWindow = fEditor.getEditorSite().getWorkbenchWindow();
68 IWorkbenchPage page = fWindow.getActivePage();
72 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
73 if (editorInput == null || editorInput.getFile() == null) {
76 return editorInput.getFile().getParent();
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);
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;
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)
107 ExternalInterface.displayTable(bookmarkString, tableName);
109 IViewPart viewPart = null;
110 String view = "com.quantum.view.tableview.TableView";
112 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
113 viewPart = page.findView(view);
114 if (viewPart == null) {
115 viewPart = page.showView(view);
117 page.bringToTop(viewPart);
118 } catch (PartInitException e) {
121 } catch (NotConnectedException e) {
122 // TODO Auto-generated catch block
124 } catch (SQLException e) {
125 // TODO Auto-generated catch block
131 // IViewPart viewPart = null;
132 // String view = "com.quantum.view.tableview.TableView";
134 // IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
135 // viewPart = page.findView(view);
136 // if (viewPart == null) {
137 // viewPart = page.showView(view);
139 // page.bringToTop(viewPart);
140 // getTables((TableView) viewPart, fProject, tableName);
141 // } catch (PartInitException e) {
142 // e.printStackTrace();
148 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
149 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
150 fEditor = (PHPEditor) targetEditor;
154 private String getSQLTableName(IDocument doc, int pos) {
164 while (position >= 0) {
165 character = doc.getChar(position);
166 if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
167 || (character == '\n'))
175 int length = doc.getLength();
177 while (position < length) {
178 character = doc.getChar(position);
179 if (Character.isWhitespace(character) || (character == '\"') || (character == '\'') || (character == '\r')
180 || (character == '\n'))
189 word = new Point(start, end - start);
191 } catch (BadLocationException x) {
196 return doc.get(word.x, word.y);
197 } catch (BadLocationException e) {
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);
215 // if (bookmark != null && bookmark.isConnected()) {
217 // Connection connection = bookmark.getConnection();
218 // DatabaseMetaData metaData = connection.getMetaData();
219 // ConnectionUtil connectionUtil = new ConnectionUtil();
221 // DatabaseAdapter adapter;
223 // if (metaData != null) {
224 // String columnName;
225 // String prefixWithoutDollar = tableName;
226 // if (prefixWithoutDollar.charAt(0) == '$') {
227 // prefixWithoutDollar = prefixWithoutDollar.substring(1);
230 // set = metaData.getTables(null, null, "%" + prefixWithoutDollar
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);
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());
248 // SQLResults results = MultiSQLServer.getInstance().execute(
249 // bookmark, connectionUtil.connect(bookmark, fWindow.getShell()),
252 // if (results != null && results.isResultSet()) {
253 // SQLResultSetCollection.getInstance().addSQLResultSet(
254 // (SQLResultSetResults) results);
256 // } catch (SQLException e) {
257 // ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
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());
281 // SQLResults results = MultiSQLServer.getInstance()
282 // .execute(bookmark,
283 // connectionUtil.connect(bookmark, fWindow.getShell()),
286 // if (results != null && results.isResultSet()) {
287 // SQLResultSetCollection.getInstance().addSQLResultSet(
288 // (SQLResultSetResults) results);
290 // } catch (SQLException e) {
291 // ExceptionDisplayDialog.openError(fWindow.getShell(), null, null, e);
295 // // .loadTable(entityFactory
299 // // (String) locations[i],
300 // // Entity.TABLE_TYPE));
307 // } catch (NotConnectedException e) {
308 // // ignore this - not mission critical
309 // } catch (SQLException e) {
310 // e.printStackTrace();