initial plugin manifest addition: declares view and action in popup menu
[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
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
6  * 
7  * Contributors: Klaus Hartlage - www.eclipseproject.de
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
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;
16
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;
21
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;
46
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;
55
56 public class PHPOpenSQLTableEditorAction extends ActionDelegate
57                 implements
58                         IEditorActionDelegate {
59
60         private IWorkbenchWindow fWindow;
61         private PHPEditor fEditor;
62         private IProject fProject;
63
64         public void dispose() {
65         }
66
67         public void init(IWorkbenchWindow window) {
68                 this.fWindow = window;
69         }
70
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) {
77                                 //
78                         }
79                 }
80         }
81         private IWorkbenchPage getActivePage() {
82                 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
83                                 .getWorkbenchWindow();
84                 IWorkbenchPage page = workbenchWindow.getActivePage();
85                 return page;
86         }
87         public IContainer getWorkingLocation(IFileEditorInput editorInput) {
88                 if (editorInput == null || editorInput.getFile() == null) {
89                         return null;
90                 }
91                 return editorInput.getFile().getParent();
92         }
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);
99                 return file;
100         }
101
102         public void run(IAction action) {
103                 if (fEditor == null) {
104                         IEditorPart targetEditor = fWindow.getActivePage()
105                                         .getActiveEditor();
106                         if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
107                                 fEditor = (PHPEditor) targetEditor;
108                         }
109                 }
110                 if (fEditor != null) {
111                         //                      TableView view = TableView.getInstance();
112
113                         //                      determine the current Project from a (file-based) Editor
114                         IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
115                         fProject = f.getProject();
116
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);
124
125                         IViewPart viewPart = null;
126                         String view = "com.quantum.view.tableview.TableView";
127                         try {
128                                 IWorkbenchPage page = QuantumPlugin.getDefault()
129                                                 .getActivePage();
130                                 viewPart = page.findView(view);
131                                 if (viewPart == null) {
132                                         viewPart = page.showView(view);
133                                 }
134                                 page.bringToTop(viewPart);
135                                 getTables((TableView) viewPart, fProject, tableName);
136                         } catch (PartInitException e) {
137                                 e.printStackTrace();
138                         }
139
140                 }
141         }
142
143         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
144                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
145                         fEditor = (PHPEditor) targetEditor;
146                 }
147         }
148
149         private String getSQLTableName(IDocument doc, int pos) {
150                 Point word = null;
151                 int start = -1;
152                 int end = -1;
153
154                 try {
155
156                         int position = pos;
157                         char character;
158
159                         while (position >= 0) {
160                                 character = doc.getChar(position);
161                                 if (Character.isWhitespace(character) || (character == '\"')
162                                                 || (character == '\'') || (character == '\r')
163                                                 || (character == '\n'))
164                                         break;
165                                 --position;
166                         }
167
168                         start = position;
169
170                         position = pos;
171                         int length = doc.getLength();
172
173                         while (position < length) {
174                                 character = doc.getChar(position);
175                                 if (Character.isWhitespace(character) || (character == '\"')
176                                                 || (character == '\'') || (character == '\r')
177                                                 || (character == '\n'))
178                                         break;
179                                 ++position;
180                         }
181
182                         start++;
183                         end = position;
184
185                         if (end > start)
186                                 word = new Point(start, end - start);
187
188                 } catch (BadLocationException x) {
189                 }
190
191                 if (word != null) {
192                         try {
193                                 return doc.get(word.x, word.y);
194                         } catch (BadLocationException e) {
195                         }
196                 }
197                 return "";
198         }
199
200         public void getTables(TableView tableView, IProject project,
201                         String tableName) {
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);
212                                 }
213                                 if (bookmark != null && bookmark.isConnected()) {
214                                         try {
215                                                 Connection connection = bookmark.getConnection();
216                                                 DatabaseMetaData metaData = connection.getMetaData();
217
218                                                 if (metaData != null) {
219                                                         String columnName;
220                                                         String prefixWithoutDollar = tableName;
221                                                         if (prefixWithoutDollar.charAt(0) == '$') {
222                                                                 prefixWithoutDollar = prefixWithoutDollar
223                                                                                 .substring(1);
224                                                         }
225                                                         ResultSet set;
226                                                         set = metaData.getTables(null, null, "%"
227                                                                         + prefixWithoutDollar+"%", null);
228                                                         while (set.next()) {
229                                                                 tableName = set.getString("TABLE_NAME");
230                                                                 tableName = (tableName == null)
231                                                                                 ? ""
232                                                                                 : tableName.trim();
233                                                                 if (tableName != null && tableName.length() > 0) {
234                                                                         sqlList.add(tableName);
235                                                                 }
236                                                         }
237                                                         set.close();
238                                                         EntityFactory entityFactory = EntityFactory
239                                                                         .getInstance();
240                                                         if (sqlList.size() == 1) {
241                                                                 tableView.loadTable(entityFactory.create(
242                                                                                 bookmark, null,
243                                                                                 (String) sqlList.get(0),
244                                                                                 Entity.TABLE_TYPE));
245                                                         } else if (sqlList.size() > 1) {
246                                                                 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
247                                                                                 PHPeclipsePlugin.getDefault()
248                                                                                                 .getWorkbench()
249                                                                                                 .getActiveWorkbenchWindow()
250                                                                                                 .getShell(), sqlList,
251                                                                                 new ListContentProvider(),
252                                                                                 new LabelProvider(),
253                                                                                 "Select the SQL to open.");
254                                                                 listSelectionDialog
255                                                                                 .setTitle("Multiple tablenames found");
256                                                                 if (listSelectionDialog.open() == Window.OK) {
257                                                                         Object[] locations = listSelectionDialog
258                                                                                         .getResult();
259                                                                         if (locations != null) {
260                                                                                 for (int i = 0; i < locations.length; i++) {
261                                                                                         tableView
262                                                                                                         .loadTable(entityFactory
263                                                                                                                         .create(
264                                                                                                                                         bookmark,
265                                                                                                                                         null,
266                                                                                                                                         (String) locations[i],
267                                                                                                                                         Entity.TABLE_TYPE));    
268                                                                                 }
269
270                                                                         }
271                                                                 }
272                                                         }
273                                                 }
274                                         } catch (NotConnectedException e) {
275                                                 // ignore this - not mission critical
276                                         } catch (SQLException e) {
277                                                 e.printStackTrace();
278                                         }
279                                 }
280                         }
281                 }
282         }
283
284 }