]>
Commit | Line | Data |
---|---|---|
1 | /******************************************************************************* | |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.Comparator; | |
15 | import java.util.HashSet; | |
16 | import java.util.Set; | |
17 | ||
18 | import org.eclipse.core.runtime.Assert; | |
19 | ||
20 | import org.eclipse.jface.viewers.ITreeContentProvider; | |
21 | import org.eclipse.jface.viewers.Viewer; | |
22 | ||
23 | import org.eclipse.jdt.core.dom.ITypeBinding; | |
24 | ||
25 | import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring; | |
26 | import org.eclipse.jdt.internal.corext.util.CollectionsUtil; | |
27 | ||
28 | public class ChangeTypeContentProvider implements ITreeContentProvider { | |
29 | ||
30 | public ChangeTypeRefactoring fGeneralizeType; | |
31 | ||
32 | ChangeTypeContentProvider(ChangeTypeRefactoring gt){ | |
33 | fGeneralizeType= gt; | |
34 | } | |
35 | ||
36 | public Object[] getChildren(Object element) { | |
37 | if (element instanceof RootType){ | |
38 | return ((RootType)element).getChildren(); | |
39 | } | |
40 | ITypeBinding[] superTypes = CollectionsUtil.toArray(getDirectSuperTypes((ITypeBinding)element), ITypeBinding.class); | |
41 | Arrays.sort(superTypes, new Comparator<ITypeBinding>(){ | |
42 | public int compare(ITypeBinding o1, ITypeBinding o2) { | |
43 | String name1 = o1.getQualifiedName(); | |
44 | String name2 = o2.getQualifiedName(); | |
45 | return name1.compareTo(name2); | |
46 | } | |
47 | }); | |
48 | return superTypes; | |
49 | } | |
50 | ||
51 | /** | |
52 | * @param type a type | |
53 | * @return the direct superclass and direct superinterfaces. Class Object is | |
54 | * included in the result if the root of the hierarchy is a top-level | |
55 | * interface. | |
56 | */ | |
57 | public Set<ITypeBinding> getDirectSuperTypes(ITypeBinding type){ | |
58 | Set<ITypeBinding> result= new HashSet<ITypeBinding>(); | |
59 | if (type.getSuperclass() != null){ | |
60 | result.add(type.getSuperclass()); | |
61 | } | |
62 | ITypeBinding[] interfaces= type.getInterfaces(); | |
63 | for (int i=0; i < interfaces.length; i++){ | |
64 | result.add(interfaces[i]); | |
65 | } | |
66 | fGeneralizeType.generated_4646330632040955199(type, result); | |
67 | return result; | |
68 | } | |
69 | ||
70 | public Object[] getElements(Object element) { | |
71 | Assert.isTrue(element instanceof RootType); | |
72 | return ((RootType)element).getChildren(); | |
73 | } | |
74 | ||
75 | public boolean hasChildren(Object element) { | |
76 | return getChildren(element).length > 0; | |
77 | } | |
78 | ||
79 | public Object getParent(Object element) { | |
80 | return null; | |
81 | } | |
82 | ||
83 | public void dispose() { | |
84 | } | |
85 | ||
86 | public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
87 | } | |
88 | ||
89 | /** | |
90 | * Artificial "root node" of the tree view. This is needed to handle situations where the replacement | |
91 | * types do not have a single common supertype. Also, the tree view does not show the root node by | |
92 | * default. | |
93 | */ | |
94 | static class RootType { | |
95 | RootType(ITypeBinding root){ | |
96 | fRoot = root; | |
97 | } | |
98 | public ITypeBinding[] getChildren(){ | |
99 | return new ITypeBinding[]{ fRoot }; | |
100 | } | |
101 | private ITypeBinding fRoot; | |
102 | } | |
103 | } |