KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > actions > RefactoringActions


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.refactoring.actions;
12
13 import org.eclipse.jdt.core.IClassFile;
14 import org.eclipse.jdt.core.ICompilationUnit;
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jdt.core.IType;
17 import org.eclipse.jdt.core.JavaModelException;
18
19 import org.eclipse.jface.text.ITextSelection;
20
21 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
22 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
23 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
24
25 /**
26  * Helper class for refactoring actions
27  */

28 public class RefactoringActions {
29
30     /**
31      * Converts the given selection into a type using the following rules:
32      * <ul>
33      * <li>if the selection is enclosed by a type than that type is returned.</li>
34      * <li>if the selection is inside a compilation unit or class file than the
35      * primary type is returned.</li>
36      * <li>otherwise <code>null</code> is returned.
37      * </ul>
38      */

39     public static IType getEnclosingOrPrimaryType(JavaTextSelection selection) throws JavaModelException {
40         final IJavaElement element= selection.resolveEnclosingElement();
41         if (element != null)
42             return convertToEnclosingOrPrimaryType(element);
43         return null;
44     }
45     public static IType getEnclosingOrPrimaryType(JavaEditor editor) throws JavaModelException {
46         return convertToEnclosingOrPrimaryType(SelectionConverter.resolveEnclosingElement(
47             editor, (ITextSelection)editor.getSelectionProvider().getSelection()));
48     }
49
50     private static IType convertToEnclosingOrPrimaryType(IJavaElement element) throws JavaModelException {
51         if (element instanceof IType)
52             return (IType)element;
53         IType result= (IType)element.getAncestor(IJavaElement.TYPE);
54         if (result != null)
55             return result;
56         if (element instanceof ICompilationUnit)
57             return ((ICompilationUnit)element).findPrimaryType();
58         if (element instanceof IClassFile)
59             return ((IClassFile)element).getType();
60         return null;
61     }
62     
63     /**
64      * Converts the given selection into a type using the following rules:
65      * <ul>
66      * <li>if the selection is enclosed by a type than that type is returned.</li>
67      * <li>otherwise <code>null</code> is returned.
68      * </ul>
69      */

70     public static IType getEnclosingType(JavaTextSelection selection) throws JavaModelException {
71         return convertToEnclosingType(selection.resolveEnclosingElement());
72     }
73     public static IType getEnclosingType(JavaEditor editor) throws JavaModelException {
74         return convertToEnclosingType(SelectionConverter.resolveEnclosingElement(
75             editor, (ITextSelection)editor.getSelectionProvider().getSelection()));
76     }
77     
78     private static IType convertToEnclosingType(IJavaElement element) {
79         if (element == null)
80             return null;
81         if (! (element instanceof IType))
82             element= element.getAncestor(IJavaElement.TYPE);
83         return (IType)element;
84     }
85 }
86
Popular Tags