001    /*
002     * file NoTypeSorter.java
003     * 
004     * Licensed Materials - Property of IBM
005     * Restricted Materials of IBM - you are allowed to copy, modify and 
006     * redistribute this file as part of any program that interfaces with 
007     * IBM Rational CM API.
008     *
009     * com.ibm.rational.teamapi.scout.NoTypeSorter
010     *
011     * © Copyright IBM Corporation 2004, 2008.  All Rights Reserved.
012     * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
013     * disclosure restricted by GSA ADP  Schedule Contract with IBM Corp.
014     */
015    package com.ibm.rational.teamapi.scout;
016    
017    import org.eclipse.jface.viewers.ContentViewer;
018    import org.eclipse.jface.viewers.IBaseLabelProvider;
019    import org.eclipse.jface.viewers.ILabelProvider;
020    import org.eclipse.jface.viewers.Viewer;
021    import org.eclipse.jface.viewers.ViewerSorter;
022    
023    /**
024     * A ViewSorter that sorts the bound member list on the simple name of the child
025     * (as opposed to sorting on the label displayed in the tree view, which includes
026     * resource type information).
027     */
028    public class NoTypeSorter extends ViewerSorter {        
029        /*
030             * @see ViewerSorter#compare(Viewer, Object, Object)
031             */
032            public int compare(Viewer viewer, Object e1, Object e2) {
033            int cat1 = category(e1);
034            int cat2 = category(e2);
035            
036            if (cat1 != cat2) return cat1 - cat2;
037    
038            String name1, name2;
039                    if (viewer == null || !(viewer instanceof ContentViewer)) {
040                            name1 = e1.toString();
041                            name2 = e2.toString();
042                    } else {
043                            IBaseLabelProvider prov = ((ContentViewer)viewer).getLabelProvider();
044                            if (prov instanceof ILabelProvider) {
045                                    ILabelProvider lprov = (ILabelProvider)prov;
046                                    name1 = lprov.getText(e1);
047                                    name2 = lprov.getText(e2);
048                            } else {
049                                    name1 = e1.toString();
050                                    name2 = e2.toString();
051                            }
052                    }
053                    return collator.compare(stripType(name1, e1), stripType(name2, e2));
054            }
055    
056        /**
057         * Strips the resource type from the head of the display name
058         * @param name The element display name
059         * @param obj The ProxyElement from which the name was derived
060         * @return The element display name stripped of its resource type.
061         */
062            protected String stripType(String name, Object obj) {
063            if (name == null)
064                return "";
065            
066            if (obj!=null && (obj instanceof ProxyElement)) {
067                int space = name.indexOf(" ");
068            
069                if (space > 0) {
070                    String type = name.substring(0,space);
071                    ProxyElement pe = (ProxyElement)obj;
072                    
073                    if (type.equals(pe.resourceType())) {
074                        return name.substring(space+1);
075                    }
076                }
077            }
078            
079                    return name;    
080            }
081    
082    }