001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.wizards;
011:
012: import java.util.Comparator;
013:
014: import org.eclipse.jface.util.Policy;
015: import org.eclipse.jface.viewers.IBasicPropertyConstants;
016: import org.eclipse.jface.viewers.ILabelProvider;
017: import org.eclipse.jface.viewers.ITableLabelProvider;
018: import org.eclipse.jface.viewers.Viewer;
019: import org.eclipse.jface.viewers.ViewerComparator;
020: import org.eclipse.pde.core.plugin.IPluginBase;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.core.plugin.ModelEntry;
023: import org.eclipse.pde.internal.core.ifeature.IFeature;
024: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
025: import org.eclipse.pde.internal.ui.PDEPlugin;
026: import org.eclipse.pde.internal.ui.elements.ElementLabelProvider;
027: import org.eclipse.pde.internal.ui.nls.ModelChange;
028: import org.eclipse.swt.graphics.Image;
029:
030: public class ListUtil {
031:
032: private static final Comparator stringComparator = new Comparator() {
033:
034: public int compare(Object arg0, Object arg1) {
035: if (arg0 instanceof String && arg1 instanceof String)
036: return ((String) arg0)
037: .compareToIgnoreCase((String) arg1);
038: // if not two Strings like we expect, then use default comparator
039: return Policy.getComparator().compare(arg0, arg1);
040: }
041:
042: };
043:
044: static class NameComparator extends ViewerComparator {
045: public NameComparator() {
046: // when comparing names, always use the comparator above to do a String comparison
047: super (stringComparator);
048: }
049:
050: public boolean isSorterProperty(Object element,
051: Object propertyId) {
052: return propertyId.equals(IBasicPropertyConstants.P_TEXT);
053: }
054: }
055:
056: static class FeatureComparator extends NameComparator {
057: public int compare(Viewer viewer, Object e1, Object e2) {
058: if (e1 instanceof IFeatureModel
059: && e2 instanceof IFeatureModel) {
060: IFeature feature1 = ((IFeatureModel) e1).getFeature();
061: IFeature feature2 = ((IFeatureModel) e2).getFeature();
062: int result = getComparator().compare(feature1.getId(),
063: feature2.getId());
064: if (result != 0) {
065: return result;
066: }
067: }
068: return super .compare(viewer, e1, e2);
069: }
070: }
071:
072: public static class PluginComparator extends NameComparator {
073: public int compare(Viewer viewer, Object e1, Object e2) {
074: int result = 0;
075: String name1 = getName(e1);
076: String name2 = getName(e2);
077: if (name1 != null && name2 != null)
078: result = getComparator().compare(name1, name2);
079: return (result != 0) ? result : super .compare(viewer, e1,
080: e2);
081: }
082:
083: private String getName(Object object) {
084:
085: if (object instanceof IPluginBase)
086: return getPluginName((IPluginBase) object);
087: if (object instanceof IPluginModelBase)
088: return getPluginName(((IPluginModelBase) object)
089: .getPluginBase());
090: if (object instanceof ModelEntry) {
091: return getPluginName(((ModelEntry) object).getModel()
092: .getPluginBase());
093: }
094: if (object instanceof ModelChange)
095: return getPluginName(((ModelChange) object)
096: .getParentModel().getPluginBase());
097: return null;
098: }
099:
100: private String getPluginName(IPluginBase pluginBase) {
101: return PDEPlugin.isFullNameModeEnabled() ? pluginBase
102: .getTranslatedName() : pluginBase.getId();
103: }
104: }
105:
106: public static final ViewerComparator NAME_COMPARATOR = new NameComparator();
107:
108: public static final ViewerComparator PLUGIN_COMPARATOR = new PluginComparator();
109:
110: public static final ViewerComparator FEATURE_COMPARATOR = new FeatureComparator();
111:
112: static class TableLabelProvider extends ElementLabelProvider
113: implements ITableLabelProvider {
114: public String getColumnText(Object o, int index) {
115: return getText(o);
116: }
117:
118: public Image getColumnImage(Object o, int index) {
119: return getImage(o);
120: }
121: }
122:
123: public static final ILabelProvider TABLE_LABEL_PROVIDER = new TableLabelProvider();
124:
125: public ListUtil() {
126: super();
127: }
128: }
|