KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jface.viewers.IStructuredContentProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.ITypeHierarchy;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.ui.IWorkingCopyProvider;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29
30 /**
31  * Content provider used for the method view.
32  * Allows also seeing methods inherited from base classes.
33  */

34 public class MethodsContentProvider implements IStructuredContentProvider, IWorkingCopyProvider {
35     
36     private static final Object JavaDoc[] NO_ELEMENTS = new Object JavaDoc[0];
37         
38     private boolean fShowInheritedMethods;
39     private TypeHierarchyLifeCycle fHierarchyLifeCycle;
40     private TableViewer fViewer;
41     
42     public MethodsContentProvider(TypeHierarchyLifeCycle lifecycle) {
43         fHierarchyLifeCycle= lifecycle;
44         fShowInheritedMethods= false;
45         fViewer= null;
46     }
47     
48     /**
49      * Turn on / off showing of inherited methods
50      */

51     public void showInheritedMethods(boolean show) {
52         if (show != fShowInheritedMethods) {
53             fShowInheritedMethods= show;
54             if (fViewer != null) {
55                 fViewer.refresh();
56             }
57         }
58     }
59     
60     /* (non-Javadoc)
61      * @see IStructuredContentProvider#providesWorkingCopies()
62      */

63     public boolean providesWorkingCopies() {
64         return true;
65     }
66     
67     /**
68      * Returns true if inherited methods are shown
69      */

70     public boolean isShowInheritedMethods() {
71         return fShowInheritedMethods;
72     }
73     
74     
75     private void addAll(Object JavaDoc[] arr, List JavaDoc res) {
76         if (arr != null) {
77             for (int j= 0; j < arr.length; j++) {
78                 res.add(arr[j]);
79             }
80         }
81     }
82
83     /*
84      * @see IStructuredContentProvider#getElements
85      */

86     public Object JavaDoc[] getElements(Object JavaDoc element) {
87         if (element instanceof IType) {
88             IType type= (IType)element;
89
90             List JavaDoc res= new ArrayList JavaDoc();
91             try {
92                 ITypeHierarchy hierarchy= fHierarchyLifeCycle.getHierarchy();
93                 if (fShowInheritedMethods && hierarchy != null) {
94                     IType[] allSupertypes= hierarchy.getAllSupertypes(type);
95                     // sort in from last to first: elements with same name
96
// will show up in hierarchy order
97
for (int i= allSupertypes.length - 1; i >= 0; i--) {
98                         IType superType= allSupertypes[i];
99                         if (superType.exists()) {
100                             addAll(superType.getMethods(), res);
101                             addAll(superType.getInitializers(), res);
102                             addAll(superType.getFields(), res);
103                         }
104                     }
105                 }
106                 if (type.exists()) {
107                     addAll(type.getMethods(), res);
108                     addAll(type.getInitializers(), res);
109                     addAll(type.getFields(), res);
110                 }
111             } catch (JavaModelException e) {
112                 JavaPlugin.log(e);
113             }
114             return res.toArray();
115         }
116         return NO_ELEMENTS;
117     }
118     
119     
120     /*
121      * @see IContentProvider#inputChanged
122      */

123     public void inputChanged(Viewer input, Object JavaDoc oldInput, Object JavaDoc newInput) {
124         Assert.isTrue(input instanceof TableViewer);
125     
126         fViewer= (TableViewer) input;
127     }
128     
129     /*
130      * @see IContentProvider#dispose
131      */

132     public void dispose() {
133     }
134
135 }
136
Popular Tags