Author: dazarov
Date: 2008-12-26 14:50:59 -0500 (Fri, 26 Dec 2008)
New Revision: 12832
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamDeclarationsHandler.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamHandler.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamReferencesHandler.java
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/OpenSeamComponentHandler.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-2509
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamDeclarationsHandler.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamDeclarationsHandler.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamDeclarationsHandler.java 2008-12-26
19:50:59 UTC (rev 12832)
@@ -0,0 +1,23 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.ui.handlers;
+
+import org.jboss.tools.seam.ui.search.SeamSearchScope;
+
+public class FindSeamDeclarationsHandler extends FindSeamHandler {
+
+ /**
+ * Returns the search limitation to declarations
+ */
+ protected int getLimitTo() {
+ return SeamSearchScope.SEARCH_FOR_DECLARATIONS;
+ }
+}
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamDeclarationsHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamHandler.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamHandler.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamHandler.java 2008-12-26
19:50:59 UTC (rev 12832)
@@ -0,0 +1,395 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.ui.handlers;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
+import org.eclipse.jdt.internal.ui.search.SearchMessages;
+import org.eclipse.jdt.internal.ui.search.SearchUtil;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.dialogs.ErrorDialog;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.ISelectionListener;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.MultiPageEditorPart;
+import org.eclipse.ui.progress.IProgressService;
+import org.eclipse.ui.texteditor.AbstractTextEditor;
+import org.jboss.tools.common.el.core.model.ELInvocationExpression;
+import org.jboss.tools.common.model.ui.editor.EditorPartWrapper;
+import org.jboss.tools.common.model.ui.texteditors.xmleditor.XMLTextEditor;
+import org.jboss.tools.jst.jsp.jspeditor.JSPMultiPageEditor;
+import org.jboss.tools.seam.core.ISeamContextVariable;
+import org.jboss.tools.seam.core.ISeamProject;
+import org.jboss.tools.seam.core.SeamCorePlugin;
+import org.jboss.tools.seam.ui.SeamGuiPlugin;
+import org.jboss.tools.seam.ui.search.SeamSearchQuery;
+import org.jboss.tools.seam.ui.search.SeamSearchScope;
+
+abstract public class FindSeamHandler extends AbstractHandler implements
ISelectionListener{
+
+ /**
+ * @Override
+ */
+ public void run() {
+
+ }
+ /*
+ * Returns editor from a given workbench window
+ *
+ * @param window
+ * @return
+ */
+ private IEditorPart getCurrentEditor(IWorkbenchWindow window) {
+ if (window == null || window.getActivePage() == null)
+ return null;
+
+ return window.getActivePage().getActiveEditor();
+ }
+
+ /*
+ * Returns viewer for a given editor
+ *
+ * @param editor
+ * @return
+ */
+ private ISourceViewer getEditorViewer(IWorkbenchPart editor) {
+ ISourceViewer viewer = null;
+
+ if (editor instanceof EditorPartWrapper) {
+ editor = ((EditorPartWrapper)editor).getEditor();
+ }
+ if (editor instanceof JSPMultiPageEditor) {
+ viewer = ((JSPMultiPageEditor)editor).getJspEditor().getTextViewer();
+ } else if (editor instanceof XMLTextEditor) {
+ viewer = ((XMLTextEditor)editor).getTextViewer();
+ } else if (editor instanceof MultiPageEditorPart) {
+ IEditorPart activeEditor = getActiveEditor((MultiPageEditorPart)editor);
+ if (activeEditor instanceof AbstractTextEditor) {
+ viewer = getSourceViewer((AbstractTextEditor)activeEditor);
+ }
+ } else if (editor instanceof AbstractTextEditor ) {
+ viewer = getSourceViewer((AbstractTextEditor)editor);
+ }
+ else if (editor instanceof CompilationUnitEditor) {
+ viewer = ((CompilationUnitEditor)editor).getViewer();
+ }
+ return viewer;
+ }
+
+ /*
+ * Returns {@link ITextSelection} for a given viewer
+ *
+ * @param viewer
+ * @return
+ */
+ private ITextSelection getTextSelection(ITextViewer viewer) {
+ if (viewer == null || viewer.getSelectionProvider() == null)
+ return null;
+
+ return getTextSelection(viewer.getSelectionProvider().getSelection());
+ }
+
+ /*
+ * Returns {@link ITextSelection} for a given {@link ISelection}
+ *
+ * @param selection
+ * @return
+ */
+ private ITextSelection getTextSelection(ISelection selection) {
+ if (selection == null || selection.isEmpty())
+ return null;
+
+ if (selection instanceof ITextSelection) {
+ return (ITextSelection)selection;
+ }
+ return null;
+ }
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ IWorkbenchPart activeWorkbenchPart = getCurrentEditor(getActiveWorkbenchWindow());
+ if (activeWorkbenchPart == null)
+ return null;
+
+ IEditorInput input = (activeWorkbenchPart instanceof IEditorPart ?
+ ((IEditorPart)activeWorkbenchPart).getEditorInput() : null);
+ ISourceViewer viewer = getEditorViewer(activeWorkbenchPart);
+ if (viewer == null)
+ return null;
+
+ ITextSelection selection = getTextSelection(viewer);
+ if (selection == null)
+ return null;
+
+ int selectionOffset = selection.getOffset();
+
+ IDocument document = viewer.getDocument();
+
+ IFile file = null;
+
+ if (input instanceof IFileEditorInput) {
+ file = ((IFileEditorInput)input).getFile();
+ }
+
+ IProject project = (file == null ? null : file.getProject());
+
+ ISeamProject seamProject = SeamCorePlugin.getSeamProject(project, true);
+ if (seamProject == null)
+ return null;
+
+// List<ELOperandToken> tokens =
SeamELCompletionEngine.findTokensAtOffset(document, selectionOffset);
+
+ ELInvocationExpression tokens = null; //TODO
+
+ if (tokens == null)
+ return null; // No EL Operand found
+
+ try {
+ performNewSearch(tokens, file);
+ } catch (JavaModelException jme) {
+ SeamGuiPlugin.getPluginLog().logError(jme);
+ } catch (InterruptedException ie) {
+ SeamGuiPlugin.getPluginLog().logError(ie);
+ }
+ return null;
+
+ }
+ /**
+ * Finds the variable names for the selected ELOperandToken tokens
+ *
+ * @param seamProject
+ * @param document
+ * @param tokens
+ * @return
+ */
+ public static String[] findVariableNames(ISeamProject seamProject, IDocument document,
ELInvocationExpression tokens) {
+ String[] varNames = null;
+ if(tokens == null) return varNames;
+
+ // Define the Seam project variables to search for declarations
+ List<ISeamContextVariable> variables = new
ArrayList<ISeamContextVariable>();
+
+ while(tokens != null) {
+ try {
+ int start = tokens.getStartPosition();
+ int end = tokens.getEndPosition();
+ String variationText = document.get(start, end - start);
+
+ Set<ISeamContextVariable> vars =
seamProject.getVariablesByName(variationText);
+ if (vars != null)
+ variables.addAll(vars);
+
+ } catch (BadLocationException e1) {
+ SeamGuiPlugin.getPluginLog().logError(e1);
+ }
+ tokens = tokens.getLeft();
+ }
+
+ if (variables.size() != 0) {
+ // Some variable/variables are found - perform search for their declarations
+ varNames = new String[variables.size()];
+ for (int i = 0; i < variables.size(); i++) {
+ varNames[i] = variables.get(i).getName();
+ }
+ }
+
+ return varNames;
+ }
+
+ /**
+ * @Override
+ */
+ public void selectionChanged(IAction action, ISelection selection) {
+ selectionChanged(getCurrentEditor(getActiveWorkbenchWindow()), selection);
+ }
+ private SeamSearchQuery createQuery(ELInvocationExpression tokens, IFile sourceFile)
throws JavaModelException, InterruptedException {
+
+ SeamSearchScope scope = new SeamSearchScope(new IProject[] {sourceFile.getProject()},
getLimitTo());
+
+ return new SeamSearchQuery(tokens, sourceFile, scope);
+ }
+
+ /**
+ * Returns the limitTo flag. The possible values are:
+ * - SeamSearchScope.SEARCH_FOR_DECLARATIONS
+ * - SeamSearchScope.SEARCH_FOR_REFERENCES
+ * @return
+ */
+ abstract protected int getLimitTo();
+
+ private void performNewSearch(ELInvocationExpression tokens, IFile sourceFile) throws
JavaModelException, InterruptedException {
+ SeamSearchQuery query= createQuery(tokens, sourceFile);
+ if (query.canRunInBackground()) {
+ /*
+ * This indirection with Object as parameter is needed to prevent the loading
+ * of the Search plug-in: the VM verifies the method call and hence loads the
+ * types used in the method signature, eventually triggering the loading of
+ * a plug-in (in this case ISearchQuery results in Search plug-in being loaded).
+ */
+ SearchUtil.runQueryInBackground(query);
+ } else {
+ IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
+ /*
+ * This indirection with Object as parameter is needed to prevent the loading
+ * of the Search plug-in: the VM verifies the method call and hence loads the
+ * types used in the method signature, eventually triggering the loading of
+ * a plug-in (in this case it would be ISearchQuery).
+ */
+ IStatus status= SearchUtil.runQueryInForeground(progressService, query);
+ if (status.matches(IStatus.ERROR | IStatus.INFO | IStatus.WARNING)) {
+ ErrorDialog.openError(getShell(), SearchMessages.Search_Error_search_title,
SearchMessages.Search_Error_search_message, status);
+ }
+ }
+ }
+
+ /**
+ * Returns the active editor from active MultipageEditor
+ *
+ * @param multiPageEditor
+ * @return
+ */
+ public static IEditorPart getActiveEditor(MultiPageEditorPart multiPageEditor) {
+ Class editorClass = multiPageEditor.getClass();
+ while (editorClass != null) {
+ try {
+ Method m = editorClass.getDeclaredMethod("getActiveEditor", new Class[]
{});
+
+ if(m != null) {
+ m.setAccessible(true);
+ Object result = m.invoke(multiPageEditor, new Object[]{});
+ return (result instanceof IEditorPart ? (IEditorPart)result : null);
+ }
+ } catch (NoSuchMethodException e) {
+ // ignore
+ } catch (IllegalArgumentException e) {
+ // ignore
+ } catch (IllegalAccessException e) {
+ // ignore
+ } catch (InvocationTargetException e) {
+ // ignore
+ }
+ editorClass = editorClass.getSuperclass();
+ }
+ return null;
+
+ }
+
+ /**
+ * Returns the source viewer from AbstractTextEditor
+ *
+ * @param multiPageEditor
+ * @return
+ */
+ public static ISourceViewer getSourceViewer(AbstractTextEditor editor) {
+ Class editorClass = editor.getClass();
+ while (editorClass != null) {
+ try {
+ Method m = editorClass.getDeclaredMethod("getSourceViewer", new Class[]
{});
+
+ if(m != null) {
+ m.setAccessible(true);
+ Object result = m.invoke(editor, new Object[]{});
+ return (result instanceof ISourceViewer ? (ISourceViewer)result : null);
+ }
+ } catch (NoSuchMethodException e) {
+ // ignore
+ } catch (IllegalArgumentException e) {
+ // ignore
+ } catch (IllegalAccessException e) {
+ // ignore
+ } catch (InvocationTargetException e) {
+ // ignore
+ }
+ editorClass = editorClass.getSuperclass();
+ }
+ return null;
+
+ }
+
+ /*
+ * Returns current Shell
+ *
+ * @return
+ */
+ private Shell getShell() {
+ return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+ }
+
+// IWorkbenchPart fActiveWorkbenchPart = null;
+
+ /*
+ * Returns the workbench
+ *
+ * @return
+ */
+ private IWorkbench getWorkbench() {
+ return PlatformUI.getWorkbench();
+ }
+
+ /*
+ * Returns active workbench window
+ * @return
+ */
+ private IWorkbenchWindow getActiveWorkbenchWindow() {
+ return (getWorkbench() == null ? null : getWorkbench().getActiveWorkbenchWindow());
+ }
+
+ /*
+ * Updates availability on the action delegate
+ *
+ * @param selection
+ */
+ private void update(ISelection selection) {
+ boolean enabled = false;
+ try {
+ IWorkbenchPart activeWorkbenchPart = getCurrentEditor(getActiveWorkbenchWindow());
+ if (!(activeWorkbenchPart instanceof IEditorPart))
+ return;
+
+ ISourceViewer viewer = getEditorViewer((IEditorPart)activeWorkbenchPart);
+ if (viewer == null)
+ return;
+
+ enabled = (getTextSelection(selection) != null);
+ } finally {
+ setBaseEnabled(enabled);
+ }
+ }
+
+ // ISelectionListener
+ public void selectionChanged(IWorkbenchPart part, ISelection selection) {
+ update(getTextSelection(getEditorViewer(part)));
+ }
+
+}
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamReferencesHandler.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamReferencesHandler.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamReferencesHandler.java 2008-12-26
19:50:59 UTC (rev 12832)
@@ -0,0 +1,24 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.ui.handlers;
+
+import org.jboss.tools.seam.ui.search.SeamSearchScope;
+
+
+public class FindSeamReferencesHandler extends FindSeamHandler{
+
+ /**
+ * Returns the search limitation to references
+ */
+ protected int getLimitTo() {
+ return SeamSearchScope.SEARCH_FOR_REFERENCES;
+ }
+}
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/FindSeamReferencesHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/OpenSeamComponentHandler.java
===================================================================
---
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/OpenSeamComponentHandler.java
(rev 0)
+++
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/OpenSeamComponentHandler.java 2008-12-26
19:50:59 UTC (rev 12832)
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.seam.ui.handlers;
+
+import java.util.Set;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.SelectionDialog;
+import org.jboss.tools.seam.core.ISeamComponentDeclaration;
+import org.jboss.tools.seam.internal.core.SeamComponentDeclaration;
+import org.jboss.tools.seam.internal.core.SeamJavaComponentDeclaration;
+import org.jboss.tools.seam.ui.SeamUIMessages;
+import org.jboss.tools.seam.ui.wizard.OpenSeamComponentDialog;
+import org.jboss.tools.seam.ui.wizard.OpenSeamComponentDialog.SeamComponentWrapper;
+/**
+ * @author Daniel Azarov
+ *
+ */
+public class OpenSeamComponentHandler extends AbstractHandler {
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ Shell parent= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+ SelectionDialog dialog;
+
+ dialog= new OpenSeamComponentDialog(parent);
+
+ dialog.setTitle(SeamUIMessages.OPEN_SEAM_COMPONENT_ACTION_ACTION_NAME);
+ dialog.setMessage(SeamUIMessages.OPEN_SEAM_COMPONENT_ACTION_MESSAGE);
+
+ int result= dialog.open();
+ if (result != IDialogConstants.OK_ID)
+ return null;
+
+ Object[] components= dialog.getResult();
+ if (components != null && components.length > 0) {
+ SeamComponentWrapper wrapper= null;
+ for (int i= 0; i < components.length; i++) {
+ wrapper= (SeamComponentWrapper) components[i];
+ SeamJavaComponentDeclaration javaDeclaration =
(SeamJavaComponentDeclaration)wrapper.getComponent().getJavaDeclaration();
+ if(javaDeclaration != null){
+ javaDeclaration.open();
+ }else{
+ Set<ISeamComponentDeclaration> declarations =
wrapper.getComponent().getAllDeclarations();
+ if(declarations.iterator().hasNext()){
+ ((SeamComponentDeclaration)declarations.iterator().next()).open();
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+
+}
Property changes on:
trunk/seam/plugins/org.jboss.tools.seam.ui/src/org/jboss/tools/seam/ui/handlers/OpenSeamComponentHandler.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain