KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > ChangeTypeContentProvider


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;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 //import org.eclipse.jdt.core.IType;
21
import org.eclipse.jdt.core.dom.ITypeBinding;
22
23 import org.eclipse.jface.viewers.ITreeContentProvider;
24 import org.eclipse.jface.viewers.Viewer;
25
26 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
27
28 class ChangeTypeContentProvider implements ITreeContentProvider {
29     
30     private ChangeTypeRefactoring fGeneralizeType;
31     
32     ChangeTypeContentProvider(ChangeTypeRefactoring gt){
33         fGeneralizeType= gt;
34     }
35
36     public Object JavaDoc[] getChildren(Object JavaDoc element) {
37         if (element instanceof RootType){
38             return ((RootType)element).getChildren();
39         }
40         Object JavaDoc[] superTypes = getDirectSuperTypes((ITypeBinding)element).toArray();
41         Arrays.sort(superTypes, new Comparator JavaDoc(){
42             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
43                 String JavaDoc name1 = ((ITypeBinding)o1).getQualifiedName();
44                 String JavaDoc name2 = ((ITypeBinding)o2).getQualifiedName();
45                 return name1.compareTo(name2);
46             }
47         });
48         return superTypes;
49     }
50     
51     /**
52      * Returns the direct superclass and direct superinterfaces. Class Object is
53      * included in the result if the root of the hierarchy is a top-level
54      * interface.
55      */

56     public Set JavaDoc/*<ITypeBinding>*/ getDirectSuperTypes(ITypeBinding type){
57         Set JavaDoc/*<ITypeBinding>*/ result= new HashSet JavaDoc();
58         if (type.getSuperclass() != null){
59             result.add(type.getSuperclass());
60         }
61         ITypeBinding[] interfaces= type.getInterfaces();
62         for (int i=0; i < interfaces.length; i++){
63             result.add(interfaces[i]);
64         }
65         if (fGeneralizeType.getOriginalType().isInterface() && type != fGeneralizeType.getObject()){
66             result.add(fGeneralizeType.getObject());
67         }
68         return result;
69     }
70
71     public Object JavaDoc[] getElements(Object JavaDoc element) {
72         Assert.isTrue(element instanceof RootType);
73         return ((RootType)element).getChildren();
74     }
75
76     public boolean hasChildren(Object JavaDoc element) {
77         return getChildren(element).length > 0;
78     }
79
80     public Object JavaDoc getParent(Object JavaDoc element) {
81         return null;
82     }
83
84     public void dispose() {
85     }
86
87     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
88     }
89     
90     /**
91      * Artificial "root node" of the tree view. This is needed to handle situations where the replacement
92      * types do not have a single common supertype. Also, the tree view does not show the root node by
93      * default.
94      */

95     static class RootType {
96         RootType(ITypeBinding root){
97             fRoot = root;
98         }
99         public ITypeBinding[] getChildren(){
100             return new ITypeBinding[]{ fRoot };
101         }
102         private ITypeBinding fRoot;
103     }
104 }
105
Popular Tags