KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreateTypeHierarchyOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core;
12
13 import org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IJavaModelStatus;
16 import org.eclipse.jdt.core.IJavaModelStatusConstants;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.IRegion;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.ITypeHierarchy;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.core.search.IJavaSearchScope;
23 import org.eclipse.jdt.internal.core.hierarchy.RegionBasedTypeHierarchy;
24 import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy;
25
26 /**
27  * This operation creates an <code>ITypeHierarchy</code> for a specific type within
28  * a specified region, or for all types within a region. The specified
29  * region limits the number of resolved subtypes (to the subset of
30  * types in the specified region). The resolved supertypes may go outside
31  * of the specified region in order to reach the root(s) of the type
32  * hierarchy. A Java Project is required to provide a context (classpath)
33  * to use while resolving supertypes and subtypes.
34  *
35  * @see ITypeHierarchy
36  */

37
38 public class CreateTypeHierarchyOperation extends JavaModelOperation {
39     /**
40      * The generated type hierarchy
41      */

42     protected TypeHierarchy typeHierarchy;
43     
44 /**
45  * Constructs an operation to create a type hierarchy for the
46  * given type within the specified region, in the context of
47  * the given project.
48  */

49 public CreateTypeHierarchyOperation(IRegion region, ICompilationUnit[] workingCopies, IType element, boolean computeSubtypes) {
50     super(element);
51     this.typeHierarchy = new RegionBasedTypeHierarchy(region, workingCopies, element, computeSubtypes);
52 }
53 /**
54  * Constructs an operation to create a type hierarchy for the
55  * given type and working copies.
56  */

57 public CreateTypeHierarchyOperation(IType element, ICompilationUnit[] workingCopies, IJavaSearchScope scope, boolean computeSubtypes) {
58     super(element);
59     ICompilationUnit[] copies;
60     if (workingCopies != null) {
61         int length = workingCopies.length;
62         copies = new ICompilationUnit[length];
63         System.arraycopy(workingCopies, 0, copies, 0, length);
64     } else {
65         copies = null;
66     }
67     this.typeHierarchy = new TypeHierarchy(element, copies, scope, computeSubtypes);
68 }
69 /**
70  * Constructs an operation to create a type hierarchy for the
71  * given type and working copies.
72  */

73 public CreateTypeHierarchyOperation(IType element, ICompilationUnit[] workingCopies, IJavaProject project, boolean computeSubtypes) {
74     super(element);
75     ICompilationUnit[] copies;
76     if (workingCopies != null) {
77         int length = workingCopies.length;
78         copies = new ICompilationUnit[length];
79         System.arraycopy(workingCopies, 0, copies, 0, length);
80     } else {
81         copies = null;
82     }
83     this.typeHierarchy = new TypeHierarchy(element, copies, project, computeSubtypes);
84 }
85 /**
86  * Performs the operation - creates the type hierarchy
87  * @exception JavaModelException The operation has failed.
88  */

89 protected void executeOperation() throws JavaModelException {
90     this.typeHierarchy.refresh(this);
91 }
92 /**
93  * Returns the generated type hierarchy.
94  */

95 public ITypeHierarchy getResult() {
96     return this.typeHierarchy;
97 }
98 /**
99  * @see JavaModelOperation
100  */

101 public boolean isReadOnly() {
102     return true;
103 }
104 /**
105  * Possible failures: <ul>
106  * <li>NO_ELEMENTS_TO_PROCESS - at least one of a type or region must
107  * be provided to generate a type hierarchy.
108  * <li>ELEMENT_NOT_PRESENT - the provided type or type's project does not exist
109  * </ul>
110  */

111 public IJavaModelStatus verify() {
112     IJavaElement elementToProcess= getElementToProcess();
113     if (elementToProcess == null && !(this.typeHierarchy instanceof RegionBasedTypeHierarchy)) {
114         return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS);
115     }
116     if (elementToProcess != null && !elementToProcess.exists()) {
117         return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, elementToProcess);
118     }
119     IJavaProject project = this.typeHierarchy.javaProject();
120     if (project != null && !project.exists()) {
121         return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, project);
122     }
123     return JavaModelStatus.VERIFIED_OK;
124 }
125 }
126
Popular Tags