KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > StepIntoSelectionUtils


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.jdt.core.ICodeAssist;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IMethod;
16 import org.eclipse.jdt.core.JavaModelException;
17 import org.eclipse.jdt.ui.JavaUI;
18 import org.eclipse.jface.text.ITextSelection;
19 import org.eclipse.ui.IEditorInput;
20
21 /**
22  * Utility class for aiding step into selection actions and hyper-linking
23  *
24  * @see StepIntoSelectionActionDelegate
25  * @see StepIntoSelectionHyperlinkDetector
26  *
27  * @since 3.3
28  */

29 public class StepIntoSelectionUtils {
30
31     
32     /**
33      * gets the <code>IJavaElement</code> from the editor input
34      * @param input the current editor input
35      * @return the corresponding <code>IJavaElement</code>
36      */

37     public static IJavaElement getJavaElement(IEditorInput input) {
38         IJavaElement je = JavaUI.getEditorInputJavaElement(input);
39         if(je != null) {
40             return je;
41         }
42         return JavaUI.getWorkingCopyManager().getWorkingCopy(input);
43     }
44     
45     /**
46      * Returns the <code>IMethod</code> from the given selection within the given <code>IJavaElement</code>,
47      * or <code>null</code> if the selection does not container or is not an <code>IMethod</code>
48      * @param selection
49      * @param element
50      * @return the corresponding <code>IMethod</code> from the selection within the provided <code>IJavaElement</code>
51      * @throws JavaModelException
52      */

53     public static IMethod getMethod(ITextSelection selection, IJavaElement element) throws JavaModelException {
54         if(element != null && element instanceof ICodeAssist) {
55             IJavaElement[] elements = ((ICodeAssist)element).codeSelect(selection.getOffset(), selection.getLength());
56             for (int i = 0; i < elements.length; i++) {
57                 if (elements[i] instanceof IMethod) {
58                     return (IMethod)elements[i];
59                 }
60             }
61         }
62         return null;
63     }
64 }
65
Popular Tags