/******************************************************************************* * Copyright (c) 2000, 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.rubypeople.rdt.internal.ui.typehierarchy; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.widgets.Display; import org.rubypeople.rdt.core.IMember; import org.rubypeople.rdt.core.IMethod; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IType; import org.rubypeople.rdt.core.ITypeHierarchy; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.internal.corext.util.MethodOverrideTester; import org.rubypeople.rdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; import org.rubypeople.rdt.ui.RubyElementLabels; /** * Label provider for the hierarchy method viewers. */ public class MethodsLabelProvider extends AppearanceAwareLabelProvider { private Color fResolvedBackground; private boolean fShowDefiningType; private TypeHierarchyLifeCycle fHierarchy; private MethodsViewer fMethodsViewer; public MethodsLabelProvider(TypeHierarchyLifeCycle lifeCycle, MethodsViewer methodsViewer) { super(DEFAULT_TEXTFLAGS, DEFAULT_IMAGEFLAGS); fHierarchy= lifeCycle; fShowDefiningType= false; fMethodsViewer= methodsViewer; fResolvedBackground= null; } public void setShowDefiningType(boolean showDefiningType) { fShowDefiningType= showDefiningType; } public boolean isShowDefiningType() { return fShowDefiningType; } private IType getDefiningType(Object element) throws RubyModelException { int kind= ((IRubyElement) element).getElementType(); if (kind != IRubyElement.METHOD && kind != IRubyElement.FIELD && kind != IRubyElement.CONSTANT && kind != IRubyElement.INSTANCE_VAR && kind != IRubyElement.CLASS_VAR) { return null; } IType declaringType= ((IMember) element).getDeclaringType(); if (kind != IRubyElement.METHOD) { return declaringType; } ITypeHierarchy hierarchy= fHierarchy.getHierarchy(); if (hierarchy == null) { return declaringType; } IMethod method= (IMethod) element; MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy); IMethod res= tester.findDeclaringMethod(method, true); if (res == null || method.equals(res)) { return declaringType; } return res.getDeclaringType(); } /* (non-Javadoc) * @see ILabelProvider#getText */ public String getText(Object element) { String text= super.getText(element); if (fShowDefiningType) { try { IType type= getDefiningType(element); if (type != null) { StringBuffer buf= new StringBuffer(super.getText(type)); buf.append(RubyElementLabels.CONCAT_STRING); buf.append(text); return buf.toString(); } } catch (RubyModelException e) { } } return text; } /* (non-Javadoc) * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object) */ public Color getForeground(Object element) { if (fMethodsViewer.isShowInheritedMethods() && element instanceof IMethod) { IMethod curr= (IMethod) element; IMember declaringType= curr.getDeclaringType(); if (declaringType.equals(fMethodsViewer.getInput())) { if (fResolvedBackground == null) { Display display= Display.getCurrent(); fResolvedBackground= display.getSystemColor(SWT.COLOR_DARK_BLUE); } return fResolvedBackground; } } return null; } }