KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > typehierarchy > TraditionalHierarchyViewer


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.typehierarchy;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.widgets.Composite;
17
18 import org.eclipse.ui.IWorkbenchPart;
19
20 import org.eclipse.jdt.core.Flags;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.ITypeHierarchy;
23
24 /**
25  * A TypeHierarchyViewer that looks like the type hierarchy view of VA/Java:
26  * Starting form Object down to the element in focus, then all subclasses from
27  * this element.
28  * Used by the TypeHierarchyViewPart which has to provide a TypeHierarchyLifeCycle
29  * on construction (shared type hierarchy)
30  */

31 public class TraditionalHierarchyViewer extends TypeHierarchyViewer {
32     
33     public TraditionalHierarchyViewer(Composite parent, TypeHierarchyLifeCycle lifeCycle, IWorkbenchPart part) {
34         super(parent, new TraditionalHierarchyContentProvider(lifeCycle), lifeCycle, part);
35     }
36     
37     /*
38      * @see TypeHierarchyViewer#getTitle
39      */

40     public String JavaDoc getTitle() {
41         if (isMethodFiltering()) {
42             return TypeHierarchyMessages.TraditionalHierarchyViewer_filtered_title;
43         } else {
44             return TypeHierarchyMessages.TraditionalHierarchyViewer_title;
45         }
46     }
47
48     /*
49      * @see TypeHierarchyViewer#updateContent
50      */

51     public void updateContent(boolean expand) {
52         getTree().setRedraw(false);
53         refresh();
54         
55         if (expand) {
56             TraditionalHierarchyContentProvider contentProvider= (TraditionalHierarchyContentProvider) getContentProvider();
57             int expandLevel= contentProvider.getExpandLevel();
58             if (isMethodFiltering()) {
59                 expandLevel++;
60             }
61             expandToLevel(expandLevel);
62         }
63         getTree().setRedraw(true);
64     }
65
66     /**
67      * Content provider for the 'traditional' type hierarchy.
68      */

69     public static class TraditionalHierarchyContentProvider extends TypeHierarchyContentProvider {
70         
71             
72         public TraditionalHierarchyContentProvider(TypeHierarchyLifeCycle provider) {
73             super(provider);
74         }
75         
76         public int getExpandLevel() {
77             ITypeHierarchy hierarchy= getHierarchy();
78             if (hierarchy != null) {
79                 IType input= hierarchy.getType();
80                 if (input != null) {
81                     return getDepth(hierarchy, input) + 2;
82                 } else {
83                     return 5;
84                 }
85             }
86             return 2;
87         }
88         
89         private int getDepth(ITypeHierarchy hierarchy, IType input) {
90             int count= 0;
91             IType superType= hierarchy.getSuperclass(input);
92             while (superType != null) {
93                 count++;
94                 superType= hierarchy.getSuperclass(superType);
95             }
96             return count;
97         }
98         
99         /* (non-Javadoc)
100          * @see org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyContentProvider#getRootTypes(java.util.List)
101          */

102         protected final void getRootTypes(List JavaDoc res) {
103             ITypeHierarchy hierarchy= getHierarchy();
104             if (hierarchy != null) {
105                 IType input= hierarchy.getType();
106                 if (input == null) {
107                     IType[] classes= hierarchy.getRootClasses();
108                     for (int i= 0; i < classes.length; i++) {
109                         res.add(classes[i]);
110                     }
111                     IType[] interfaces= hierarchy.getRootInterfaces();
112                     for (int i= 0; i < interfaces.length; i++) {
113                         res.add(interfaces[i]);
114                     }
115                 } else {
116                     if (Flags.isInterface(hierarchy.getCachedFlags(input))) {
117                         res.add(input);
118                     } else if (isAnonymousFromInterface(input)) {
119                         res.add(hierarchy.getSuperInterfaces(input)[0]);
120                     } else {
121                         IType[] roots= hierarchy.getRootClasses();
122                         for (int i= 0; i < roots.length; i++) {
123                             if (isObject(roots[i])) {
124                                 res.add(roots[i]);
125                                 return;
126                             }
127                         }
128                         res.addAll(Arrays.asList(roots)); // something wrong with the hierarchy
129
}
130                 }
131             }
132         }
133                 
134         /*
135          * @see TypeHierarchyContentProvider.getTypesInHierarchy
136          */

137         protected final void getTypesInHierarchy(IType type, List JavaDoc res) {
138             ITypeHierarchy hierarchy= getHierarchy();
139             if (hierarchy != null) {
140                 IType[] types= hierarchy.getSubtypes(type);
141                 if (isObject(type)) {
142                     for (int i= 0; i < types.length; i++) {
143                         IType curr= types[i];
144                         if (!isAnonymousFromInterface(curr)) { // no anonymous classes on 'Object' -> will be children of interface
145
res.add(curr);
146                         }
147                     }
148                 } else {
149                     boolean isHierarchyOnType= (hierarchy.getType() != null);
150                     boolean isClass= !Flags.isInterface(hierarchy.getCachedFlags(type));
151                     if (isClass || isHierarchyOnType) {
152                         for (int i= 0; i < types.length; i++) {
153                             res.add(types[i]);
154                         }
155                     } else {
156                         for (int i= 0; i < types.length; i++) {
157                             IType curr= types[i];
158                             // no classes implementing interfaces, only if anonymous
159
if (Flags.isInterface(hierarchy.getCachedFlags(curr)) || isAnonymous(curr)) {
160                                 res.add(curr);
161                             }
162                         }
163                     }
164                 }
165             }
166         }
167
168         protected IType getParentType(IType type) {
169             ITypeHierarchy hierarchy= getHierarchy();
170             if (hierarchy != null) {
171                 return hierarchy.getSuperclass(type);
172                 // don't handle interfaces
173
}
174             return null;
175         }
176             
177     }
178 }
179
Popular Tags