|
From: Christopher W. <caw...@us...> - 2006-02-10 19:55:05
|
Update of /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11297/src/org/rubypeople/rdt/ui Modified Files: RubyElementLabelProvider.java RubyElementSorter.java Added Files: RubyElementLabels.java RubyElementImageDescriptor.java Log Message: new UI classes for element labels and images Index: RubyElementLabelProvider.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/RubyElementLabelProvider.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** RubyElementLabelProvider.java 7 Sep 2005 22:02:33 -0000 1.7 --- RubyElementLabelProvider.java 10 Feb 2006 19:54:54 -0000 1.8 *************** *** 1,119 **** package org.rubypeople.rdt.ui; ! import org.eclipse.jface.viewers.ILabelProvider; ! import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.swt.graphics.Image; ! import org.jruby.lexer.yacc.SyntaxException; ! import org.rubypeople.rdt.core.IRubyElement; ! import org.rubypeople.rdt.core.IMethod; ! import org.rubypeople.rdt.core.IType; ! import org.rubypeople.rdt.core.RubyModelException; ! import org.rubypeople.rdt.internal.ui.RubyPluginImages; ! import org.rubypeople.rdt.internal.ui.RubyPlugin; ! public class RubyElementLabelProvider implements ILabelProvider { ! /** ! * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object) ! */ ! public Image getImage(Object object) { ! if (object == null) { ! log("Attempting to get Image for null object"); ! return RubyPluginImages.get(RubyPluginImages.IMG_OBJS_ERROR); ! } ! if (object instanceof IRubyElement) { ! IRubyElement rubyElement = (IRubyElement) object; ! if (rubyElement.isType(IRubyElement.IMPORT_CONTAINER)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_IMPORT_CONTAINER); ! if (rubyElement.isType(IRubyElement.GLOBAL)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_GLOBAL); ! if (rubyElement.isType(IRubyElement.CONSTANT)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_CONSTANT); ! if (rubyElement.isType(IRubyElement.CLASS_VAR)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_CLASS_VAR); ! if (rubyElement.isType(IRubyElement.LOCAL_VAR)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_LOCAL_VAR); ! if (rubyElement.isType(IRubyElement.IMPORT)) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_IMPORT); ! if (rubyElement.isType(IRubyElement.SINGLETON_METHOD)) { ! IMethod method = (IMethod) rubyElement; ! try { ! if (method.getVisibility() == IMethod.PUBLIC) { return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_SINGLETONMETHOD_PUB); } ! if (method.getVisibility() == IMethod.PROTECTED) { return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_SINGLETONMETHOD_PRO); } ! } catch (RubyModelException e) { ! RubyPlugin.log(e); ! } ! // assume it's private ! return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_SINGLETONMETHOD); ! } ! if (rubyElement.isType(IRubyElement.METHOD)) { ! IMethod method = (IMethod) rubyElement; ! try { ! if (method.getVisibility() == IMethod.PUBLIC) { return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBYMETHOD_PUB); } ! if (method.getVisibility() == IMethod.PROTECTED) { return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBYMETHOD_PRO); } ! } catch (RubyModelException e) { ! RubyPlugin.log(e); ! } ! // assume it's private ! return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_METHOD); ! } ! if (rubyElement.isType(IRubyElement.INSTANCE_VAR)) { ! // FIXME Transition to showing the methods that attr adds! Rather than saying these variables are read/write ! return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_INSTANCE_VAR); ! } ! if (rubyElement.isType(IRubyElement.TYPE)) { ! IType rubyType = (IType) rubyElement; ! if(rubyType.isClass()) return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_CLASS); ! return RubyPluginImages.get(RubyPluginImages.IMG_CTOOLS_RUBY_MODULE); ! } ! } ! if (object instanceof SyntaxException) { ! return RubyPluginImages.get(RubyPluginImages.IMG_OBJS_ERROR); ! } ! log("Attempting to get Image for unknown object: " + object); ! return RubyPluginImages.get(RubyPluginImages.IMG_OBJS_ERROR); ! } ! /** ! * @param string ! */ ! protected void log(String string) { ! RubyPlugin.log(string); ! } ! /** ! * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object) ! */ ! public String getText(Object obj) { ! if (obj instanceof IRubyElement) return ((IRubyElement) obj).getElementName(); ! if (obj instanceof SyntaxException) { ! SyntaxException syntaxException = (SyntaxException) obj ; ! StringBuffer sb = new StringBuffer() ; ! sb.append("Line ") ; ! sb.append(syntaxException.getPosition().getStartLine()) ; ! sb.append(": ") ; ! sb.append(syntaxException.getMessage()) ; ! return sb.toString() ; ! } ! return "Invalid object: " + obj.getClass().getName(); ! } ! /** ! * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) ! */ ! public void addListener(ILabelProviderListener arg0) {} ! /** ! * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose() ! */ ! public void dispose() {} ! /** ! * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, ! * java.lang.String) ! */ ! public boolean isLabelProperty(Object arg0, String arg1) { ! return false; ! } ! /** ! * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) ! */ ! public void removeListener(ILabelProviderListener arg0) {} } --- 1,197 ---- package org.rubypeople.rdt.ui; ! import org.eclipse.core.resources.IStorage; ! import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; ! import org.rubypeople.rdt.internal.ui.viewsupport.RubyElementImageProvider; ! import org.rubypeople.rdt.internal.ui.viewsupport.StorageLabelProvider; ! public class RubyElementLabelProvider extends LabelProvider { ! /** ! * Flag (bit mask) indicating that the label should include overlay icons ! * for element type and modifiers. ! */ ! public final static int SHOW_OVERLAY_ICONS = 0x002; ! /** ! * Flag (bit mask) indicating that the label should include the name of the ! * package fragment root (appended). ! */ ! public final static int SHOW_ROOT = 0x004; ! /** ! * Flag (bit mask) indicating that the label should show the icons with no ! * space reserved for overlays. ! */ ! public final static int SHOW_SMALL_ICONS = 0x008; ! /** ! * Flag (bit mask) indicating that the package fragment roots from class ! * path variables should be rendered with the variable in the name ! */ ! public final static int SHOW_VARIABLE = 0x010; ! /** ! * Flag (bit mask) indicating that compilation units, class files, types, ! * declarations and members should be rendered qualified. Examples: ! * <code>java.lang.String</code>, <code>java.util.Vector.size()</code> ! * ! * @since 2.0 ! */ ! public final static int SHOW_QUALIFIED = 0x020; ! /** ! * Flag (bit mask) indicating that compilation units, class files, types, ! * declarations and members should be rendered qualified.The qualification ! * is appended. Examples: <code>String - java.lang</code>, ! * <code>size() - java.util.Vector</code> ! * ! * @since 2.0 ! */ ! public final static int SHOW_POST_QUALIFIED = 0x040; ! /** ! * Constant (value <code>0</code>) indicating that the label should show ! * the basic images only. ! */ ! public final static int SHOW_BASICS = 0x000; ! /** ! * Constant indicating the default label rendering. Currently the default is ! * equivalent to <code>SHOW_OVERLAY_ICONS</code>. ! */ ! public final static int SHOW_DEFAULT = new Integer(SHOW_OVERLAY_ICONS) ! .intValue(); ! ! private RubyElementImageProvider fImageLabelProvider; ! ! private StorageLabelProvider fStorageLabelProvider; ! private int fFlags; ! private int fImageFlags; ! private long fTextFlags; ! ! /** ! * Creates a new label provider with <code>SHOW_DEFAULT</code> flag. ! * ! * @see #SHOW_DEFAULT ! * @since 0.8.0 ! */ ! public RubyElementLabelProvider() { ! this(SHOW_DEFAULT); ! } ! ! /** ! * Creates a new label provider. ! * ! * @param flags ! * the initial options; a bitwise OR of <code>SHOW_* </code> ! * constants ! */ ! public RubyElementLabelProvider(int flags) { ! fImageLabelProvider = new RubyElementImageProvider(); ! fStorageLabelProvider = new StorageLabelProvider(); ! fFlags = flags; ! updateImageProviderFlags(); ! updateTextProviderFlags(); ! } ! ! private boolean getFlag(int flag) { ! return (fFlags & flag) != 0; ! } ! ! /** ! * Turns on the rendering options specified in the given flags. ! * ! * @param flags ! * the options; a bitwise OR of <code>SHOW_* </code> constants ! */ ! public void turnOn(int flags) { ! fFlags |= flags; ! updateImageProviderFlags(); ! updateTextProviderFlags(); ! } ! ! /** ! * Turns off the rendering options specified in the given flags. ! * ! * @param flags ! * the initial options; a bitwise OR of <code>SHOW_* </code> ! * constants ! */ ! public void turnOff(int flags) { ! fFlags &= (~flags); ! updateImageProviderFlags(); ! updateTextProviderFlags(); ! } ! ! private void updateImageProviderFlags() { ! fImageFlags = 0; ! if (getFlag(SHOW_OVERLAY_ICONS)) { ! fImageFlags |= RubyElementImageProvider.OVERLAY_ICONS; ! } ! if (getFlag(SHOW_SMALL_ICONS)) { ! fImageFlags |= RubyElementImageProvider.SMALL_ICONS; ! } ! } ! ! private void updateTextProviderFlags() { ! fTextFlags = RubyElementLabels.M_PARAMETER_NAMES; ! if (getFlag(SHOW_ROOT)) { ! fTextFlags |= RubyElementLabels.APPEND_ROOT_PATH; ! } ! if (getFlag(SHOW_VARIABLE)) { ! fTextFlags |= RubyElementLabels.ROOT_VARIABLE; ! } ! if (getFlag(SHOW_QUALIFIED)) { ! fTextFlags |= (RubyElementLabels.F_FULLY_QUALIFIED ! | RubyElementLabels.M_FULLY_QUALIFIED | RubyElementLabels.I_FULLY_QUALIFIED ! | RubyElementLabels.T_FULLY_QUALIFIED | RubyElementLabels.D_QUALIFIED ! | RubyElementLabels.CF_QUALIFIED | RubyElementLabels.CU_QUALIFIED); ! } ! if (getFlag(SHOW_POST_QUALIFIED)) { ! fTextFlags |= (RubyElementLabels.F_POST_QUALIFIED | RubyElementLabels.M_POST_QUALIFIED ! | RubyElementLabels.I_POST_QUALIFIED | RubyElementLabels.T_POST_QUALIFIED ! | RubyElementLabels.D_POST_QUALIFIED | RubyElementLabels.CF_POST_QUALIFIED | RubyElementLabels.CU_POST_QUALIFIED); ! } ! } ! ! /* ! * (non-Javadoc) ! * ! * @see ILabelProvider#getImage ! */ ! public Image getImage(Object element) { ! Image result = fImageLabelProvider.getImageLabel(element, fImageFlags); ! if (result != null) { return result; } ! ! if (element instanceof IStorage) return fStorageLabelProvider.getImage(element); ! ! return result; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see ILabelProvider#getText ! */ ! public String getText(Object element) { ! String text = RubyElementLabels.getTextLabel(element, fTextFlags); ! if (text.length() > 0) { return text; } ! ! if (element instanceof IStorage) return fStorageLabelProvider.getText(element); ! ! return text; ! } ! ! /* ! * (non-Javadoc) ! * ! * @see IBaseLabelProvider#dispose ! */ ! public void dispose() { ! fStorageLabelProvider.dispose(); ! fImageLabelProvider.dispose(); ! } } --- NEW FILE: RubyElementImageDescriptor.java --- /******************************************************************************* * 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.ui; import org.eclipse.jface.resource.CompositeImageDescriptor; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.util.Assert; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Point; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.RubyPluginImages; /** * A RubyImageDescriptor consists of a base image and several adornments. The adornments * are computed according to the flags either passed during creation or set via the method * <code>setAdornments</code>. * * <p> * This class may be instantiated; it is not intended to be subclassed. * </p> * * @since 2.0 */ public class RubyElementImageDescriptor extends CompositeImageDescriptor { /** Flag to render the static adornment. */ public final static int STATIC= 0x008; /** Flag to render the warning adornment. */ public final static int WARNING= 0x020; /** Flag to render the error adornment. */ public final static int ERROR= 0x040; /** Flag to render the 'override' adornment. */ public final static int OVERRIDES= 0x080; /** Flag to render the 'implements' adornment. */ public final static int IMPLEMENTS= 0x100; /** Flag to render the 'constructor' adornment. */ public final static int CONSTRUCTOR= 0x200; /** * Flag to render the 'deprecated' adornment. * @since 0.8.0 */ public final static int DEPRECATED= 0x400; private ImageDescriptor fBaseImage; private int fFlags; private Point fSize; /** * Creates a new RubyElementImageDescriptor. * * @param baseImage an image descriptor used as the base image * @param flags flags indicating which adornments are to be rendered. See <code>setAdornments</code> * for valid values. * @param size the size of the resulting image * @see #setAdornments(int) */ public RubyElementImageDescriptor(ImageDescriptor baseImage, int flags, Point size) { fBaseImage= baseImage; Assert.isNotNull(fBaseImage); fFlags= flags; Assert.isTrue(fFlags >= 0); fSize= size; Assert.isNotNull(fSize); } /** * Sets the descriptors adornments. Valid values are: <code>ABSTRACT</code>, <code>FINAL</code>, * <code>SYNCHRONIZED</code>, </code>STATIC<code>, </code>RUNNABLE<code>, </code>WARNING<code>, * </code>ERROR<code>, </code>OVERRIDDES<code>, <code>IMPLEMENTS</code>, <code>CONSTRUCTOR</code>, * <code>DEPRECATED</code>, or any combination of those. * * @param adornments the image descriptors adornments */ public void setAdornments(int adornments) { Assert.isTrue(adornments >= 0); fFlags= adornments; } /** * Returns the current adornments. * * @return the current adornments */ public int getAdronments() { return fFlags; } /** * Sets the size of the image created by calling <code>createImage()</code>. * * @param size the size of the image returned from calling <code>createImage()</code> * @see ImageDescriptor#createImage() */ public void setImageSize(Point size) { Assert.isNotNull(size); Assert.isTrue(size.x >= 0 && size.y >= 0); fSize= size; } /** * Returns the size of the image created by calling <code>createImage()</code>. * * @return the size of the image created by calling <code>createImage()</code> * @see ImageDescriptor#createImage() */ public Point getImageSize() { return new Point(fSize.x, fSize.y); } /* (non-Rubydoc) * Method declared in CompositeImageDescriptor */ protected Point getSize() { return fSize; } /* (non-Rubydoc) * Method declared on Object. */ public boolean equals(Object object) { if (object == null || !RubyElementImageDescriptor.class.equals(object.getClass())) return false; RubyElementImageDescriptor other= (RubyElementImageDescriptor)object; return (fBaseImage.equals(other.fBaseImage) && fFlags == other.fFlags && fSize.equals(other.fSize)); } /* (non-Rubydoc) * Method declared on Object. */ public int hashCode() { return fBaseImage.hashCode() | fFlags | fSize.hashCode(); } /* (non-Rubydoc) * Method declared in CompositeImageDescriptor */ protected void drawCompositeImage(int width, int height) { ImageData bg= getImageData(fBaseImage); if ((fFlags & DEPRECATED) != 0) { // over the full image Point size= getSize(); ImageData data= getImageData(RubyPluginImages.DESC_OVR_DEPRECATED); drawImage(data, 0, size.y - data.height); } drawImage(bg, 0, 0); drawTopRight(); drawBottomRight(); drawBottomLeft(); } private ImageData getImageData(ImageDescriptor descriptor) { ImageData data= descriptor.getImageData(); // see bug 51965: getImageData can return null if (data == null) { data= DEFAULT_IMAGE_DATA; RubyPlugin.logErrorMessage("Image data not available: " + descriptor.toString()); //$NON-NLS-1$ } return data; } private void drawTopRight() { int x= getSize().x; if ((fFlags & CONSTRUCTOR) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_CONSTRUCTOR); x-= data.width; drawImage(data, x, 0); } if ((fFlags & STATIC) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_STATIC); x-= data.width; drawImage(data, x, 0); } } private void drawBottomRight() { Point size= getSize(); int x= size.x; int flags= fFlags; if ((flags & OVERRIDES) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_OVERRIDES); x-= data.width; drawImage(data, x, size.y - data.height); } if ((flags & IMPLEMENTS) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_IMPLEMENTS); x-= data.width; drawImage(data, x, size.y - data.height); } } private void drawBottomLeft() { Point size= getSize(); int x= 0; if ((fFlags & ERROR) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_ERROR); drawImage(data, x, size.y - data.height); x+= data.width; } if ((fFlags & WARNING) != 0) { ImageData data= getImageData(RubyPluginImages.DESC_OVR_WARNING); drawImage(data, x, size.y - data.height); x+= data.width; } } } Index: RubyElementSorter.java =================================================================== RCS file: /cvsroot/rubyeclipse/org.rubypeople.rdt.ui/src/org/rubypeople/rdt/ui/RubyElementSorter.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** RubyElementSorter.java 12 Mar 2005 15:32:34 -0000 1.3 --- RubyElementSorter.java 10 Feb 2006 19:54:54 -0000 1.4 *************** *** 7,13 **** --- 7,30 ---- package org.rubypeople.rdt.ui; + import java.text.Collator; + + import org.eclipse.core.resources.IContainer; + import org.eclipse.core.resources.IFile; + import org.eclipse.core.resources.IProject; + import org.eclipse.core.resources.IStorage; + import org.eclipse.core.runtime.IAdaptable; + import org.eclipse.jface.viewers.ContentViewer; + import org.eclipse.jface.viewers.IBaseLabelProvider; + import org.eclipse.jface.viewers.ILabelProvider; + import org.eclipse.jface.viewers.Viewer; import org.eclipse.jface.viewers.ViewerSorter; + import org.eclipse.ui.model.IWorkbenchAdapter; + 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.RubyModelException; + import org.rubypeople.rdt.internal.ui.RubyPlugin; + import org.rubypeople.rdt.internal.ui.preferences.MembersOrderPreferenceCache; /** *************** *** 19,51 **** public class RubyElementSorter extends ViewerSorter { ! private static final int RUBY_REQUIRE = 1; ! private static final int RUBY_GLOBAL = 2; ! private static final int RUBY_CLASS_VARIABLE = 3; ! private static final int RUBY_INSTANCE_VARIABLE = 4; ! private static final int RUBY_METHOD = 5; ! private static final int RUBY_MODULE = 6; ! private static final int RUBY_CLASS = 7; ! /* ! * (non-Javadoc) ! * ! * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object) ! */ ! public int category(Object element) { ! if (element instanceof IRubyElement) { ! IRubyElement rubyElement = (IRubyElement) element; ! if (rubyElement.isType(IRubyElement.INSTANCE_VAR)) return RUBY_INSTANCE_VARIABLE; ! if (rubyElement.isType(IRubyElement.GLOBAL)) return RUBY_GLOBAL; ! if (rubyElement.isType(IRubyElement.METHOD)) return RUBY_METHOD; ! if (rubyElement.isType(IRubyElement.TYPE)) { ! IType rubyType = (IType) rubyElement; ! if(rubyType.isClass()) return RUBY_CLASS; ! return RUBY_MODULE; ! } ! if (rubyElement.isType(IRubyElement.IMPORT)) return RUBY_REQUIRE; ! if (rubyElement.isType(IRubyElement.CLASS_VAR)) return RUBY_CLASS_VARIABLE; ! } ! return 0; ! } } \ No newline at end of file --- 36,225 ---- public class RubyElementSorter extends ViewerSorter { ! private static final int PROJECTS = 1; ! private static final int RUBYSCRIPTS = 4; ! ! private static final int RESOURCEFOLDERS = 7; ! private static final int RESOURCES = 8; ! private static final int STORAGE = 9; ! ! private static final int IMPORT_CONTAINER = 11; ! private static final int IMPORT_DECLARATION = 12; ! ! // Includes all categories ordered using the OutlineSortOrderPage: ! // types, initializers, methods & fields ! private static final int MEMBERSOFFSET = 15; ! ! private static final int RUBYELEMENTS = 50; ! private static final int OTHERS = 51; ! ! private MembersOrderPreferenceCache fMemberOrderCache; ! ! /** ! * Constructor. ! */ ! public RubyElementSorter() { ! super(null); // delay initialization of collator ! fMemberOrderCache= RubyPlugin.getDefault().getMemberOrderPreferenceCache(); ! } ! ! /* (non-Javadoc) ! * @see org.eclipse.jface.viewers.ViewerSorter#getCollator() ! */ ! public final Collator getCollator() { ! if (collator == null) { ! collator= Collator.getInstance(); ! } ! return collator; ! } ! ! /* ! * @see ViewerSorter#category ! */ ! public int category(Object element) { ! if (element instanceof IRubyElement) { ! ! IRubyElement je = (IRubyElement) element; ! ! switch (je.getElementType()) { ! case IRubyElement.METHOD: { ! IMethod method = (IMethod) je; ! if (method.isConstructor()) { return getMemberCategory(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX); } ! if (method.isSingleton()) ! return getMemberCategory(MembersOrderPreferenceCache.STATIC_METHODS_INDEX); ! else ! return getMemberCategory(MembersOrderPreferenceCache.METHOD_INDEX); ! } ! case IRubyElement.FIELD: ! case IRubyElement.CLASS_VAR: ! case IRubyElement.INSTANCE_VAR: ! case IRubyElement.CONSTANT: { ! ! return getMemberCategory(MembersOrderPreferenceCache.FIELDS_INDEX); ! } ! case IRubyElement.TYPE: ! return getMemberCategory(MembersOrderPreferenceCache.TYPE_INDEX); ! case IRubyElement.IMPORT_CONTAINER: ! return IMPORT_CONTAINER; ! case IRubyElement.IMPORT: ! return IMPORT_DECLARATION; ! case IRubyElement.PROJECT: ! return PROJECTS; ! case IRubyElement.SCRIPT: ! return RUBYSCRIPTS; ! } ! ! return RUBYELEMENTS; ! } else if (element instanceof IFile) { ! return RESOURCES; ! } else if (element instanceof IProject) { ! return PROJECTS; ! } else if (element instanceof IContainer) { ! return RESOURCEFOLDERS; ! } else if (element instanceof IStorage) { return STORAGE; } ! return OTHERS; ! } ! ! private int getMemberCategory(int kind) { ! int offset = fMemberOrderCache.getCategoryIndex(kind); ! return offset + MEMBERSOFFSET; ! } ! ! /* ! * @see ViewerSorter#compare ! */ ! public int compare(Viewer viewer, Object e1, Object e2) { ! int cat1 = category(e1); ! int cat2 = category(e2); ! ! if (cat1 != cat2) return cat1 - cat2; ! ! if (cat1 == PROJECTS || cat1 == RESOURCES || cat1 == RESOURCEFOLDERS || cat1 == STORAGE ! || cat1 == OTHERS) { ! String name1 = getNonRubyElementLabel(viewer, e1); ! String name2 = getNonRubyElementLabel(viewer, e2); ! if (name1 != null && name2 != null) { return getCollator().compare(name1, name2); } ! return 0; // can't compare ! } ! // only ruby elements from this point ! ! if (e1 instanceof IMethod) { ! if (fMemberOrderCache.isSortByVisibility()) { ! try { ! int flags1 = ((IMethod) e1).getVisibility(); ! int flags2 = ((IMethod) e2).getVisibility(); ! int vis = fMemberOrderCache.getVisibilityIndex(flags1) ! - fMemberOrderCache.getVisibilityIndex(flags2); ! if (vis != 0) { return vis; } ! } catch (RubyModelException ignore) { ! } ! } ! } ! ! if (e1 instanceof IMember) { ! // FIXME Sort members differently! (Constants, Class vars, instance ! // vars) ! } ! ! String name1 = getElementName(e1); ! String name2 = getElementName(e2); ! ! if (e1 instanceof IType) { // handle anonymous types ! if (name1.length() == 0) { ! if (name2.length() == 0) { ! try { ! return getCollator().compare(((IType) e1).getSuperclassName(), ! ((IType) e2).getSuperclassName()); ! } catch (RubyModelException e) { ! return 0; ! } ! } else { ! return 1; ! } ! } else if (name2.length() == 0) { return -1; } ! } ! ! int cmp = getCollator().compare(name1, name2); ! if (cmp != 0) { return cmp; } ! try { ! if (e1 instanceof IMethod) { ! String[] params1 = ((IMethod) e1).getParameterNames(); ! String[] params2 = ((IMethod) e2).getParameterNames(); ! int len = Math.min(params1.length, params2.length); ! for (int i = 0; i < len; i++) { ! cmp = getCollator().compare(params1[i], params2[i]); ! if (cmp != 0) { return cmp; } ! } ! return params1.length - params2.length; ! } ! return 0; ! } catch (RubyModelException e) { ! return 0; ! } ! } ! ! private String getElementName(Object element) { ! if (element instanceof IRubyElement) { ! return ((IRubyElement) element).getElementName(); ! } else { ! return element.toString(); ! } ! } ! ! private String getNonRubyElementLabel(Viewer viewer, Object element) { ! // try to use the workbench adapter for non - ruby resources or if not ! // available, use the viewers label provider ! ! if (element instanceof IAdaptable) { ! IWorkbenchAdapter adapter = (IWorkbenchAdapter) ((IAdaptable) element) ! .getAdapter(IWorkbenchAdapter.class); ! if (adapter != null) { return adapter.getLabel(element); } ! } ! if (viewer instanceof ContentViewer) { ! IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider(); ! if (prov instanceof ILabelProvider) { return ((ILabelProvider) prov).getText(element); } ! } ! return null; ! } } \ No newline at end of file --- NEW FILE: RubyElementLabels.java --- /******************************************************************************* * 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.ui; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.ui.model.IWorkbenchAdapter; import org.rubypeople.rdt.core.IField; import org.rubypeople.rdt.core.IMethod; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IRubyScript; import org.rubypeople.rdt.core.IType; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.internal.corext.util.Messages; import org.rubypeople.rdt.internal.ui.RubyPlugin; import org.rubypeople.rdt.internal.ui.RubyUIMessages; /** * <code>RubyElementLabels</code> provides helper methods to render names of * Ruby elements. * * @since 3.1 */ public class RubyElementLabels { /** * Method names contain parameter names. e.g. <code>foo(index)</code> */ public final static long M_PARAMETER_NAMES = 1L << 1; /** * Method names are fully qualified. e.g. <code>java.util.Vector.size</code> */ public final static long M_FULLY_QUALIFIED = 1L << 7; /** * Method names are post qualified. e.g. * <code>size - java.util.Vector</code> */ public final static long M_POST_QUALIFIED = 1L << 8; /** * Initializer names are fully qualified. e.g. * <code>java.util.Vector.{ ... }</code> */ public final static long I_FULLY_QUALIFIED = 1L << 10; /** * Type names are post qualified. e.g. <code>{ ... } - java.util.Map</code> */ public final static long I_POST_QUALIFIED = 1L << 11; /** * Fields names are fully qualified. e.g. <code>java.lang.System.out</code> */ public final static long F_FULLY_QUALIFIED = 1L << 16; /** * Fields names are post qualified. e.g. <code>out - java.lang.System</code> */ public final static long F_POST_QUALIFIED = 1L << 17; /** * Type names are fully qualified. e.g. <code>java.util.Map.MapEntry</code> */ public final static long T_FULLY_QUALIFIED = 1L << 18; /** * Type names are type container qualified. e.g. <code>Map.MapEntry</code> */ public final static long T_CONTAINER_QUALIFIED = 1L << 19; /** * Type names are post qualified. e.g. <code>MapEntry - java.util.Map</code> */ public final static long T_POST_QUALIFIED = 1L << 20; /** * Declarations (import container / declaration, package declaration) are * qualified. e.g. <code>java.util.Vector.class/import container</code> */ public final static long D_QUALIFIED = 1L << 24; /** * Declarations (import container / declaration, package declaration) are * post qualified. e.g. * <code>import container - java.util.Vector.class</code> */ public final static long D_POST_QUALIFIED = 1L << 25; /** * Class file names are fully qualified. e.g. * <code>java.util.Vector.class</code> */ public final static long CF_QUALIFIED = 1L << 27; /** * Class file names are post qualified. e.g. * <code>Vector.class - java.util</code> */ public final static long CF_POST_QUALIFIED = 1L << 28; /** * Compilation unit names are fully qualified. e.g. * <code>java.util.Vector.java</code> */ public final static long CU_QUALIFIED = 1L << 31; /** * Compilation unit names are post qualified. e.g. * <code>Vector.java - java.util</code> */ public final static long CU_POST_QUALIFIED = 1L << 32; /** * Package names are qualified. e.g. <code>MyProject/src/java.util</code> */ public final static long P_QUALIFIED = 1L << 35; /** * Package names are post qualified. e.g. * <code>java.util - MyProject/src</code> */ public final static long P_POST_QUALIFIED = 1L << 36; /** * Package names are compressed. e.g. <code>o*.e*.search</code> */ public final static long P_COMPRESSED = 1L << 37; /** * Package Fragment Roots contain variable name if from a variable. e.g. * <code>JRE_LIB - c:\java\lib\rt.jar</code> */ public final static long ROOT_VARIABLE = 1L << 40; /** * Package Fragment Roots contain the project name if not an archive * (prepended). e.g. <code>MyProject/src</code> */ public final static long ROOT_QUALIFIED = 1L << 41; /** * Package Fragment Roots contain the project name if not an archive * (appended). e.g. <code>src - MyProject</code> */ public final static long ROOT_POST_QUALIFIED = 1L << 42; /** * Add root path to all elements except Package Fragment Roots and Ruby * projects. e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code> * Option only applies to getElementLabel */ public final static long APPEND_ROOT_PATH = 1L << 43; /** * Add root path to all elements except Package Fragment Roots and Ruby * projects. e.g. <code>java.lang.Vector - c:\java\lib\rt.jar</code> * Option only applies to getElementLabel */ public final static long PREPEND_ROOT_PATH = 1L << 44; /** * Post qualify referenced package fragment roots. For example * <code>jdt.jar - org.eclipse.jdt.ui</code> if the jar is referenced from * another project. */ public final static long REFERENCED_ROOT_POST_QUALIFIED = 1L << 45; /** * Specified to use the resolved information of a IType, IMethod or IField. * See {@link IType#isResolved()}. If resolved information is available, * types will be rendered with type parameters of the instantiated type. * Resolved method render with the parameter types of the method instance. * <code>Vector<String>.get(String)</code> */ public final static long USE_RESOLVED = 1L << 48; /** * Qualify all elements */ public final static long ALL_FULLY_QUALIFIED = new Long(F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED | P_QUALIFIED | ROOT_QUALIFIED).longValue(); /** * Post qualify all elements */ public final static long ALL_POST_QUALIFIED = new Long(F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED | P_POST_QUALIFIED | ROOT_POST_QUALIFIED).longValue(); /** * Default options (M_PARAMETER_NAMES enabled) */ public final static long ALL_DEFAULT = new Long(M_PARAMETER_NAMES).longValue(); /** * Default qualify options (All except Root and Package) */ public final static long DEFAULT_QUALIFIED = new Long(F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED) .longValue(); /** * Default post qualify options (All except Root and Package) */ public final static long DEFAULT_POST_QUALIFIED = new Long(F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED).longValue(); /** * User-readable string for separating post qualified names (e.g. " - "). */ public final static String CONCAT_STRING = RubyUIMessages.RubyElementLabels_concat_string; /** * User-readable string for separating list items (e.g. ", "). */ public final static String COMMA_STRING = RubyUIMessages.RubyElementLabels_comma_string; /** * User-readable string for separating the return type (e.g. " : "). */ public final static String DECL_STRING = RubyUIMessages.RubyElementLabels_declseparator_string; /** * User-readable string for ellipsis ("..."). */ public final static String ELLIPSIS_STRING = "..."; //$NON-NLS-1$ /** * User-readable string for the default package name (e.g. "(default * package)"). */ public final static String DEFAULT_PACKAGE = RubyUIMessages.RubyElementLabels_default_package; private final static long QUALIFIER_FLAGS = P_COMPRESSED | USE_RESOLVED; private RubyElementLabels() { } private static final boolean getFlag(long flags, long flag) { return (flags & flag) != 0; } /** * Returns the label of the given object. The object must be of type * {@link IRubyElement} or adapt to {@link IWorkbenchAdapter}. The empty * string is returned if the element type is not known. * * @param obj * Object to get the label from. * @param flags * The rendering flags * @return Returns the label or the empty string if the object type is not * supported. */ public static String getTextLabel(Object obj, long flags) { if (obj instanceof IRubyElement) { return getElementLabel((IRubyElement) obj, flags); } else if (obj instanceof IAdaptable) { IWorkbenchAdapter wbadapter = (IWorkbenchAdapter) ((IAdaptable) obj) .getAdapter(IWorkbenchAdapter.class); if (wbadapter != null) { return wbadapter.getLabel(obj); } } return ""; //$NON-NLS-1$ } /** * Returns the label for a Ruby element with the flags as defined by this * class. * * @param element * The element to render. * @param flags * The rendering flags. * @return the label of the Ruby element */ public static String getElementLabel(IRubyElement element, long flags) { StringBuffer buf = new StringBuffer(60); getElementLabel(element, flags, buf); return buf.toString(); } /** * Returns the label for a Ruby element with the flags as defined by this * class. * * @param element * The element to render. * @param flags * The rendering flags. * @param buf * The buffer to append the resulting label to. */ public static void getElementLabel(IRubyElement element, long flags, StringBuffer buf) { int type = element.getElementType(); switch (type) { case IRubyElement.METHOD: getMethodLabel((IMethod) element, flags, buf); break; case IRubyElement.FIELD: getFieldLabel((IField) element, flags, buf); break; case IRubyElement.LOCAL_VARIABLE: getLocalVariableLabel((IField) element, flags, buf); break; case IRubyElement.TYPE: getTypeLabel((IType) element, flags, buf); break; case IRubyElement.SCRIPT: getCompilationUnitLabel((IRubyScript) element, flags, buf); break; case IRubyElement.IMPORT_CONTAINER: case IRubyElement.IMPORT: getDeclarationLabel(element, flags, buf); break; case IRubyElement.PROJECT: case IRubyElement.RUBY_MODEL: buf.append(element.getElementName()); break; default: buf.append(element.getElementName()); } } /** * Appends the label for a method to a {@link StringBuffer}. Considers the * M_* flags. * * @param method * The element to render. * @param flags * The rendering flags. Flags with names starting with 'M_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getMethodLabel(IMethod method, long flags, StringBuffer buf) { try { // qualification if (getFlag(flags, M_FULLY_QUALIFIED)) { if (method.getDeclaringType() != null) { getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); buf.append('.'); } } buf.append(method.getElementName()); // parameters buf.append('('); if (getFlag(flags, M_PARAMETER_NAMES)) { String[] types = null; int nParams = 0; boolean renderVarargs = false; String[] names = null; if (getFlag(flags, M_PARAMETER_NAMES) && method.exists()) { names = method.getParameterNames(); if (types == null) { nParams = names.length; } else { if (nParams != names.length) { // see // https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137 // and // https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029 // RubyPlugin.logErrorMessage("RubyElementLabels: // Number of param types(" + nParams + ") != number // of names(" + names.length + "): " + // method.getElementName()); // //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ names = null; // no names rendered } } } for (int i = 0; i < nParams; i++) { if (i > 0) { buf.append(COMMA_STRING); } if (names != null) { buf.append(names[i]); } } } buf.append(')'); // post qualification if (getFlag(flags, M_POST_QUALIFIED)) { if (method.getDeclaringType() != null) { buf.append(CONCAT_STRING); getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); } } } catch (RubyModelException e) { RubyPlugin.log(e); // NotExistsException will not reach this point } } /** * Appends the label for a field to a {@link StringBuffer}. Considers the * F_* flags. * * @param field * The element to render. * @param flags * The rendering flags. Flags with names starting with 'F_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getFieldLabel(IField field, long flags, StringBuffer buf) { // qualification if (getFlag(flags, F_FULLY_QUALIFIED)) { getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); buf.append('.'); } buf.append(field.getElementName()); // post qualification if (getFlag(flags, F_POST_QUALIFIED)) { buf.append(CONCAT_STRING); getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); } } /** * Appends the label for a local variable to a {@link StringBuffer}. * * @param localVariable * The element to render. * @param flags * The rendering flags. Flags with names starting with 'F_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getLocalVariableLabel(IField localVariable, long flags, StringBuffer buf) { if (getFlag(flags, F_FULLY_QUALIFIED)) { getElementLabel(localVariable.getParent(), M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); buf.append('.'); } buf.append(localVariable.getElementName()); // post qualification if (getFlag(flags, F_POST_QUALIFIED)) { buf.append(CONCAT_STRING); getElementLabel(localVariable.getParent(), M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); } } /** * Appends the label for a type to a {@link StringBuffer}. Considers the * T_* flags. * * @param type * The element to render. * @param flags * The rendering flags. Flags with names starting with 'T_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getTypeLabel(IType type, long flags, StringBuffer buf) { if (getFlag(flags, T_FULLY_QUALIFIED)) { // IPackageFragment pack = type.getPackageFragment(); // if (!pack.isDefaultPackage()) { // getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf); // buf.append('.'); // } } if (getFlag(flags, T_FULLY_QUALIFIED | T_CONTAINER_QUALIFIED)) { IType declaringType = type.getDeclaringType(); if (declaringType != null) { getTypeLabel(declaringType, T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); buf.append('.'); } int parentType = type.getParent().getElementType(); if (parentType == IRubyElement.METHOD || parentType == IRubyElement.FIELD) { // anonymous // or local getElementLabel(type.getParent(), 0, buf); buf.append('.'); } } String typeName = type.getElementName(); if (typeName.length() == 0) { // anonymous try { String supertypeName = type.getSuperclassName(); // String[] superInterfaceNames = type.getSuperInterfaceNames(); // if (superInterfaceNames.length > 0) { // supertypeName = Signature.getSimpleName(superInterfaceNames[0]); // } else { // supertypeName = Signature.getSimpleName(type.getSuperclassName()); // } typeName = Messages.format(RubyUIMessages.RubyElementLabels_anonym_type, supertypeName); } catch (RubyModelException e) { // ignore typeName = RubyUIMessages.RubyElementLabels_anonym; } } buf.append(typeName); // post qualification if (getFlag(flags, T_POST_QUALIFIED)) { buf.append(CONCAT_STRING); IType declaringType = type.getDeclaringType(); if (declaringType != null) { getTypeLabel(declaringType, T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); int parentType = type.getParent().getElementType(); if (parentType == IRubyElement.METHOD || parentType == IRubyElement.FIELD) { // anonymous // or // local buf.append('.'); getElementLabel(type.getParent(), 0, buf); } } else { //getPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS, buf); } } } /** * Appends the label for a import container, import or package declaration * to a {@link StringBuffer}. Considers the D_* flags. * * @param declaration * The element to render. * @param flags * The rendering flags. Flags with names starting with 'D_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getDeclarationLabel(IRubyElement declaration, long flags, StringBuffer buf) { if (getFlag(flags, D_QUALIFIED)) { IRubyElement openable = (IRubyElement) declaration.getOpenable(); if (openable != null) { buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS))); buf.append('/'); } } if (declaration.getElementType() == IRubyElement.IMPORT_CONTAINER) { buf.append(RubyUIMessages.RubyElementLabels_import_container); } else { buf.append(declaration.getElementName()); } // post qualification if (getFlag(flags, D_POST_QUALIFIED)) { IRubyElement openable = (IRubyElement) declaration.getOpenable(); if (openable != null) { buf.append(CONCAT_STRING); buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS))); } } } /** * Appends the label for a compilation unit to a {@link StringBuffer}. * Considers the CU_* flags. * * @param cu * The element to render. * @param flags * The rendering flags. Flags with names starting with 'CU_' are * considered. * @param buf * The buffer to append the resulting label to. */ public static void getCompilationUnitLabel(IRubyScript cu, long flags, StringBuffer buf) { if (getFlag(flags, CU_QUALIFIED)) { // IPackageFragment pack = (IPackageFragment) cu.getParent(); // if (!pack.isDefaultPackage()) { // getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf); // buf.append('.'); // } } buf.append(cu.getElementName()); if (getFlag(flags, CU_POST_QUALIFIED)) { buf.append(CONCAT_STRING); // getPackageFragmentLabel((IPackageFragment) cu.getParent(), flags & QUALIFIER_FLAGS, buf); } } } |