1 11 package org.eclipse.pde.internal.ui.wizards; 12 13 import java.util.Comparator ; 14 15 import org.eclipse.jface.util.Policy; 16 import org.eclipse.jface.viewers.IBasicPropertyConstants; 17 import org.eclipse.jface.viewers.ILabelProvider; 18 import org.eclipse.jface.viewers.ITableLabelProvider; 19 import org.eclipse.jface.viewers.Viewer; 20 import org.eclipse.jface.viewers.ViewerComparator; 21 import org.eclipse.pde.core.plugin.IPluginBase; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.core.plugin.ModelEntry; 24 import org.eclipse.pde.internal.core.ifeature.IFeature; 25 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 26 import org.eclipse.pde.internal.ui.PDEPlugin; 27 import org.eclipse.pde.internal.ui.elements.ElementLabelProvider; 28 import org.eclipse.pde.internal.ui.nls.ModelChange; 29 import org.eclipse.swt.graphics.Image; 30 31 32 public class ListUtil { 33 34 private static final Comparator stringComparator = new Comparator () { 35 36 public int compare(Object arg0, Object arg1) { 37 if (arg0 instanceof String && arg1 instanceof String ) 38 return ((String )arg0).compareToIgnoreCase((String )arg1); 39 return Policy.getComparator().compare(arg0, arg1); 41 } 42 43 }; 44 45 static class NameComparator extends ViewerComparator { 46 public NameComparator() { 47 super(stringComparator); 49 } 50 51 public boolean isSorterProperty(Object element, Object propertyId) { 52 return propertyId.equals(IBasicPropertyConstants.P_TEXT); 53 } 54 } 55 static class FeatureComparator extends NameComparator { 56 public int compare(Viewer viewer, Object e1, Object e2) { 57 if (e1 instanceof IFeatureModel && e2 instanceof IFeatureModel) { 58 IFeature feature1 = ((IFeatureModel)e1).getFeature(); 59 IFeature feature2 = ((IFeatureModel)e2).getFeature(); 60 int result = getComparator().compare(feature1.getId(),feature2.getId()); 61 if (result != 0) { 62 return result; 63 } 64 } 65 return super.compare(viewer,e1,e2); 66 } 67 } 68 public static class PluginComparator extends NameComparator { 69 public int compare(Viewer viewer, Object e1, Object e2) { 70 int result = 0; 71 String name1 = getName(e1); 72 String name2 = getName(e2); 73 if (name1 != null && name2 != null) 74 result = getComparator().compare(name1, name2); 75 return (result != 0) ? result : super.compare(viewer, e1, e2); 76 } 77 78 private String getName(Object object) { 79 80 if (object instanceof IPluginBase) 81 return getPluginName((IPluginBase) object); 82 if (object instanceof IPluginModelBase) 83 return getPluginName( 84 ((IPluginModelBase) object).getPluginBase()); 85 if (object instanceof ModelEntry) { 86 return getPluginName( 87 ((ModelEntry) object).getModel().getPluginBase()); 88 } 89 if (object instanceof ModelChange) 90 return getPluginName( 91 ((ModelChange)object).getParentModel().getPluginBase()); 92 return null; 93 } 94 95 private String getPluginName(IPluginBase pluginBase) { 96 return PDEPlugin.isFullNameModeEnabled() 97 ? pluginBase.getTranslatedName() 98 : pluginBase.getId(); 99 } 100 } 101 102 103 public static final ViewerComparator NAME_COMPARATOR = new NameComparator(); 104 105 public static final ViewerComparator PLUGIN_COMPARATOR = new PluginComparator(); 106 107 public static final ViewerComparator FEATURE_COMPARATOR = new FeatureComparator(); 108 109 static class TableLabelProvider extends ElementLabelProvider implements ITableLabelProvider { 110 public String getColumnText(Object o, int index) { 111 return getText(o); 112 } 113 public Image getColumnImage(Object o, int index) { 114 return getImage(o); 115 } 116 } 117 118 public static final ILabelProvider TABLE_LABEL_PROVIDER = new TableLabelProvider(); 119 120 public ListUtil() { 121 super(); 122 } 123 } 124 | Popular Tags |