KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > ListUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards;
12
13 import java.util.Comparator JavaDoc;
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 JavaDoc stringComparator = new Comparator JavaDoc() {
35
36         public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
37             if (arg0 instanceof String JavaDoc && arg1 instanceof String JavaDoc)
38                 return ((String JavaDoc)arg0).compareToIgnoreCase((String JavaDoc)arg1);
39             // if not two Strings like we expect, then use default comparator
40
return Policy.getComparator().compare(arg0, arg1);
41         }
42         
43     };
44     
45     static class NameComparator extends ViewerComparator {
46         public NameComparator() {
47             // when comparing names, always use the comparator above to do a String comparison
48
super(stringComparator);
49         }
50         
51         public boolean isSorterProperty(Object JavaDoc element, Object JavaDoc propertyId) {
52             return propertyId.equals(IBasicPropertyConstants.P_TEXT);
53         }
54     }
55     static class FeatureComparator extends NameComparator {
56         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc 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 JavaDoc e1, Object JavaDoc e2) {
70             int result = 0;
71             String JavaDoc name1 = getName(e1);
72             String JavaDoc 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 JavaDoc getName(Object JavaDoc 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 JavaDoc 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 JavaDoc getColumnText(Object JavaDoc o, int index) {
111             return getText(o);
112         }
113         public Image getColumnImage(Object JavaDoc 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