001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import java.util.Arrays;
013: import java.util.Comparator;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.Assert;
018:
019: //import org.eclipse.jdt.core.IType;
020: import org.eclipse.jdt.core.dom.ITypeBinding;
021:
022: import org.eclipse.jface.viewers.ITreeContentProvider;
023: import org.eclipse.jface.viewers.Viewer;
024:
025: import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
026:
027: class ChangeTypeContentProvider implements ITreeContentProvider {
028:
029: private ChangeTypeRefactoring fGeneralizeType;
030:
031: ChangeTypeContentProvider(ChangeTypeRefactoring gt) {
032: fGeneralizeType = gt;
033: }
034:
035: public Object[] getChildren(Object element) {
036: if (element instanceof RootType) {
037: return ((RootType) element).getChildren();
038: }
039: Object[] super Types = getDirectSuperTypes(
040: (ITypeBinding) element).toArray();
041: Arrays.sort(super Types, new Comparator() {
042: public int compare(Object o1, Object o2) {
043: String name1 = ((ITypeBinding) o1).getQualifiedName();
044: String name2 = ((ITypeBinding) o2).getQualifiedName();
045: return name1.compareTo(name2);
046: }
047: });
048: return super Types;
049: }
050:
051: /**
052: * Returns the direct superclass and direct superinterfaces. Class Object is
053: * included in the result if the root of the hierarchy is a top-level
054: * interface.
055: */
056: public Set/*<ITypeBinding>*/getDirectSuperTypes(ITypeBinding type) {
057: Set/*<ITypeBinding>*/result = new HashSet();
058: if (type.getSuperclass() != null) {
059: result.add(type.getSuperclass());
060: }
061: ITypeBinding[] interfaces = type.getInterfaces();
062: for (int i = 0; i < interfaces.length; i++) {
063: result.add(interfaces[i]);
064: }
065: if (fGeneralizeType.getOriginalType().isInterface()
066: && type != fGeneralizeType.getObject()) {
067: result.add(fGeneralizeType.getObject());
068: }
069: return result;
070: }
071:
072: public Object[] getElements(Object element) {
073: Assert.isTrue(element instanceof RootType);
074: return ((RootType) element).getChildren();
075: }
076:
077: public boolean hasChildren(Object element) {
078: return getChildren(element).length > 0;
079: }
080:
081: public Object getParent(Object element) {
082: return null;
083: }
084:
085: public void dispose() {
086: }
087:
088: public void inputChanged(Viewer viewer, Object oldInput,
089: Object newInput) {
090: }
091:
092: /**
093: * Artificial "root node" of the tree view. This is needed to handle situations where the replacement
094: * types do not have a single common supertype. Also, the tree view does not show the root node by
095: * default.
096: */
097: static class RootType {
098: RootType(ITypeBinding root) {
099: fRoot = root;
100: }
101:
102: public ITypeBinding[] getChildren() {
103: return new ITypeBinding[] { fRoot };
104: }
105:
106: private ITypeBinding fRoot;
107: }
108: }
|