"Open SQL Table View" actiun (right mouse click in editor on table fragment-string)
authorkhartlage <khartlage>
Tue, 8 Jun 2004 20:00:06 +0000 (20:00 +0000)
committerkhartlage <khartlage>
Tue, 8 Jun 2004 20:00:06 +0000 (20:00 +0000)
net.sourceforge.phpeclipse/plugin.xml
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/template/php/PHPUnitContext.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenIncludeEditorAction.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenSQLTableEditorAction.java [new file with mode: 0644]

index da75286..0eb9dd0 100644 (file)
                id="net.sourceforge.phpeclipse.actions.openinclude">
          </action>
       </viewerContribution>
+      <viewerContribution
+            targetID="#PHPEditorContext"
+            id="net.sourceforge.phpeclipse.actions.popupOpenSQLTable">
+         <action
+               label="Open Quantum SQL Table View"
+               class="net.sourceforge.phpeclipse.actions.PHPOpenSQLTableEditorAction"
+               menubarPath="additions"
+               id="net.sourceforge.phpeclipse.actions.opensqltable">
+         </action>
+      </viewerContribution>
    
       <viewerContribution
             targetID="#PHPRulerContext"
index 8f9a0be..3f27a31 100644 (file)
@@ -6,6 +6,7 @@ package net.sourceforge.phpdt.internal.corext.template.php;
 
 import net.sourceforge.phpdt.internal.corext.template.ContextType;
 import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
+import net.sourceforge.phpdt.internal.corext.template.ITemplateEditor;
 import net.sourceforge.phpdt.internal.corext.template.Template;
 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
@@ -90,8 +91,8 @@ public class PHPUnitContext extends DocumentTemplateContext {
     if (lineDelimiter == null)
       lineDelimiter = PLATFORM_LINE_DELIMITER;
 
-    //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
-    //    formatter.edit(buffer, this);
+//    ITemplateEditor formatter= new PHPFormatter(lineDelimiter);
+//    formatter.edit(buffer, this);
 
     return buffer;
   }
index 1dcf6eb..b388f2f 100644 (file)
@@ -8,7 +8,7 @@
  ******************************************************************************/
 package net.sourceforge.phpeclipse.actions;
 
-import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
 
 import org.eclipse.core.resources.IContainer;
@@ -67,7 +67,7 @@ public class PHPOpenIncludeEditorAction extends ActionDelegate
                }
                return editorInput.getFile().getParent();
        }
-       private IFile getWikiFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
+       private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
 //             IContainer container = getWorkingLocation(editorInput);
 //             String fullPath  = project.getLocation().toString();
                Path path = new Path(relativeFilename);
@@ -99,7 +99,7 @@ public class PHPOpenIncludeEditorAction extends ActionDelegate
                        //      System.out.println(word);
                        if (relativeFilename != null && !relativeFilename.equals("")) {
 
-                               IFile file = getWikiFile(fProject, (IFileEditorInput) fEditor
+                               IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor
                                                .getEditorInput(), relativeFilename);
                                try {
                                        //                      createNewFileIfNeeded(file);
@@ -110,7 +110,8 @@ public class PHPOpenIncludeEditorAction extends ActionDelegate
                                        //                              getActivePage().reuseEditor(reusableEditor, new
                                        // FileEditorInput(file));
                                        //                      } else {
-                                       IDE.openEditor(getActivePage(), file, true);
+//                                     IDE.openEditor(getActivePage(), file, true);
+                                       PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
                                        //                              redrawText();
                                        //                      }
                                } catch (Exception e) {
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenSQLTableEditorAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPOpenSQLTableEditorAction.java
new file mode 100644 (file)
index 0000000..848a1ca
--- /dev/null
@@ -0,0 +1,284 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
+ * program and the accompanying materials are made available under the terms of
+ * the Common Public License v1.0 which accompanies this distribution, and is
+ * available at http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors: Klaus Hartlage - www.eclipseproject.de
+ ******************************************************************************/
+package net.sourceforge.phpeclipse.actions;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+import net.sourceforge.phpeclipse.IPreferenceConstants;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import net.sourceforge.phpeclipse.overlaypages.Util;
+import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.actions.ActionDelegate;
+import org.eclipse.ui.dialogs.ListSelectionDialog;
+import org.eclipse.ui.internal.dialogs.ListContentProvider;
+
+import com.quantum.QuantumPlugin;
+import com.quantum.model.Bookmark;
+import com.quantum.model.BookmarkCollection;
+import com.quantum.model.Entity;
+import com.quantum.model.EntityFactory;
+import com.quantum.model.NotConnectedException;
+import com.quantum.util.connection.ConnectionUtil;
+import com.quantum.view.tableview.TableView;
+
+public class PHPOpenSQLTableEditorAction extends ActionDelegate
+               implements
+                       IEditorActionDelegate {
+
+       private IWorkbenchWindow fWindow;
+       private PHPEditor fEditor;
+       private IProject fProject;
+
+       public void dispose() {
+       }
+
+       public void init(IWorkbenchWindow window) {
+               this.fWindow = window;
+       }
+
+       public void selectionChanged(IAction action, ISelection selection) {
+               if (!selection.isEmpty()) {
+                       if (selection instanceof TextSelection) {
+                               action.setEnabled(true);
+                       } else if (fWindow.getActivePage() != null
+                                       && fWindow.getActivePage().getActivePart() != null) {
+                               //
+                       }
+               }
+       }
+       private IWorkbenchPage getActivePage() {
+               IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
+                               .getWorkbenchWindow();
+               IWorkbenchPage page = workbenchWindow.getActivePage();
+               return page;
+       }
+       public IContainer getWorkingLocation(IFileEditorInput editorInput) {
+               if (editorInput == null || editorInput.getFile() == null) {
+                       return null;
+               }
+               return editorInput.getFile().getParent();
+       }
+       private IFile getIncludeFile(IProject project,
+                       IFileEditorInput editorInput, String relativeFilename) {
+               //              IContainer container = getWorkingLocation(editorInput);
+               //              String fullPath = project.getLocation().toString();
+               Path path = new Path(relativeFilename);
+               IFile file = project.getFile(path);
+               return file;
+       }
+
+       public void run(IAction action) {
+               if (fEditor == null) {
+                       IEditorPart targetEditor = fWindow.getActivePage()
+                                       .getActiveEditor();
+                       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
+                               fEditor = (PHPEditor) targetEditor;
+                       }
+               }
+               if (fEditor != null) {
+                       //                      TableView view = TableView.getInstance();
+
+                       //                      determine the current Project from a (file-based) Editor
+                       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
+                       fProject = f.getProject();
+
+                       ITextSelection selection = (ITextSelection) fEditor
+                                       .getSelectionProvider().getSelection();
+                       IDocument doc = fEditor.getDocumentProvider().getDocument(
+                                       fEditor.getEditorInput());
+                       int pos = selection.getOffset();
+                       //  System.out.println(selection.getText());
+                       String tableName = getSQLTableName(doc, pos);
+
+                       IViewPart viewPart = null;
+                       String view = "com.quantum.view.tableview.TableView";
+                       try {
+                               IWorkbenchPage page = QuantumPlugin.getDefault()
+                                               .getActivePage();
+                               viewPart = page.findView(view);
+                               if (viewPart == null) {
+                                       viewPart = page.showView(view);
+                               }
+                               page.bringToTop(viewPart);
+                               getTables((TableView) viewPart, fProject, tableName);
+                       } catch (PartInitException e) {
+                               e.printStackTrace();
+                       }
+
+               }
+       }
+
+       public void setActiveEditor(IAction action, IEditorPart targetEditor) {
+               if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
+                       fEditor = (PHPEditor) targetEditor;
+               }
+       }
+
+       private String getSQLTableName(IDocument doc, int pos) {
+               Point word = null;
+               int start = -1;
+               int end = -1;
+
+               try {
+
+                       int position = pos;
+                       char character;
+
+                       while (position >= 0) {
+                               character = doc.getChar(position);
+                               if (Character.isWhitespace(character) || (character == '\"')
+                                               || (character == '\'') || (character == '\r')
+                                               || (character == '\n'))
+                                       break;
+                               --position;
+                       }
+
+                       start = position;
+
+                       position = pos;
+                       int length = doc.getLength();
+
+                       while (position < length) {
+                               character = doc.getChar(position);
+                               if (Character.isWhitespace(character) || (character == '\"')
+                                               || (character == '\'') || (character == '\r')
+                                               || (character == '\n'))
+                                       break;
+                               ++position;
+                       }
+
+                       start++;
+                       end = position;
+
+                       if (end > start)
+                               word = new Point(start, end - start);
+
+               } catch (BadLocationException x) {
+               }
+
+               if (word != null) {
+                       try {
+                               return doc.get(word.x, word.y);
+                       } catch (BadLocationException e) {
+                       }
+               }
+               return "";
+       }
+
+       public void getTables(TableView tableView, IProject project,
+                       String tableName) {
+               // Get The Database bookmark from the Quantum SQL plugin:
+               BookmarkCollection sqlBookMarks = BookmarkCollection.getInstance();
+               if (sqlBookMarks != null) {
+                       String bookmarkString = Util.getMiscProjectsPreferenceValue(
+                                       project, IPreferenceConstants.PHP_BOOKMARK_DEFAULT);
+                       if (bookmarkString != null && !bookmarkString.equals("")) {
+                               Bookmark bookmark = sqlBookMarks.find(bookmarkString);
+                               ArrayList sqlList = new ArrayList();
+                               if (bookmark != null && !bookmark.isConnected()) {
+                                       new ConnectionUtil().connect(bookmark, null);
+                               }
+                               if (bookmark != null && bookmark.isConnected()) {
+                                       try {
+                                               Connection connection = bookmark.getConnection();
+                                               DatabaseMetaData metaData = connection.getMetaData();
+
+                                               if (metaData != null) {
+                                                       String columnName;
+                                                       String prefixWithoutDollar = tableName;
+                                                       if (prefixWithoutDollar.charAt(0) == '$') {
+                                                               prefixWithoutDollar = prefixWithoutDollar
+                                                                               .substring(1);
+                                                       }
+                                                       ResultSet set;
+                                                       set = metaData.getTables(null, null, "%"
+                                                                       + prefixWithoutDollar+"%", null);
+                                                       while (set.next()) {
+                                                               tableName = set.getString("TABLE_NAME");
+                                                               tableName = (tableName == null)
+                                                                               ? ""
+                                                                               : tableName.trim();
+                                                               if (tableName != null && tableName.length() > 0) {
+                                                                       sqlList.add(tableName);
+                                                               }
+                                                       }
+                                                       set.close();
+                                                       EntityFactory entityFactory = EntityFactory
+                                                                       .getInstance();
+                                                       if (sqlList.size() == 1) {
+                                                               tableView.loadTable(entityFactory.create(
+                                                                               bookmark, null,
+                                                                               (String) sqlList.get(0),
+                                                                               Entity.TABLE_TYPE));
+                                                       } else if (sqlList.size() > 1) {
+                                                               ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
+                                                                               PHPeclipsePlugin.getDefault()
+                                                                                               .getWorkbench()
+                                                                                               .getActiveWorkbenchWindow()
+                                                                                               .getShell(), sqlList,
+                                                                               new ListContentProvider(),
+                                                                               new LabelProvider(),
+                                                                               "Select the SQL to open.");
+                                                               listSelectionDialog
+                                                                               .setTitle("Multiple tablenames found");
+                                                               if (listSelectionDialog.open() == Window.OK) {
+                                                                       Object[] locations = listSelectionDialog
+                                                                                       .getResult();
+                                                                       if (locations != null) {
+                                                                               for (int i = 0; i < locations.length; i++) {
+                                                                                       tableView
+                                                                                                       .loadTable(entityFactory
+                                                                                                                       .create(
+                                                                                                                                       bookmark,
+                                                                                                                                       null,
+                                                                                                                                       (String) locations[i],
+                                                                                                                                       Entity.TABLE_TYPE));    
+                                                                               }
+
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       } catch (NotConnectedException e) {
+                                               // ignore this - not mission critical
+                                       } catch (SQLException e) {
+                                               e.printStackTrace();
+                                       }
+                               }
+                       }
+               }
+       }
+
+}
\ No newline at end of file