KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > FeatureSorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.update.internal.ui.wizards;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.jface.viewers.*;
15 import org.eclipse.update.core.*;
16 import org.eclipse.update.operations.*;
17
18 public class FeatureSorter extends ViewerSorter {
19     
20     public static final int FEATURE_VERSION = 1;
21     public static final int FEATURE_LABEL = 2;
22     public static final int FEATURE_PROVIDER = 3;
23     
24     public static final int ASCENDING = 1;
25     public static final int DESCENDING = -1;
26     
27     private int key = FEATURE_LABEL;
28     private int labelOrder;
29     private int versionOrder;
30     private int providerOrder;
31     
32     public FeatureSorter() {
33         this(FEATURE_LABEL, ASCENDING, ASCENDING, ASCENDING);
34     }
35     
36     public FeatureSorter(int key, int labelOrder, int versionOrder, int providerOrder) {
37         this.key = key;
38         this.labelOrder = labelOrder;
39         this.versionOrder = versionOrder;
40         this.providerOrder = providerOrder;
41     }
42
43     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
44         if (!(e1 instanceof IInstallFeatureOperation)
45             || !(e2 instanceof IInstallFeatureOperation))
46             return super.compare(viewer, e1, e2);
47             
48         IFeature f1 = ((IInstallFeatureOperation)e1).getFeature();
49         IFeature f2 = ((IInstallFeatureOperation)e2).getFeature();
50         
51         PluginVersionIdentifier v1 = f1.getVersionedIdentifier().getVersion();
52         PluginVersionIdentifier v2 = f2.getVersionedIdentifier().getVersion();
53         
54         String JavaDoc label1 = f1.getLabel() == null ? "" : f1.getLabel(); //$NON-NLS-1$
55
String JavaDoc label2 = f2.getLabel() == null ? "" : f2.getLabel(); //$NON-NLS-1$
56

57         String JavaDoc provider1 = f1.getProvider() == null ? "" : f1.getProvider(); //$NON-NLS-1$
58
String JavaDoc provider2 = f2.getProvider() == null ? "" : f2.getProvider(); //$NON-NLS-1$
59

60         int result = 0;
61         if (key == FEATURE_VERSION) {
62             result = compareVersions(v1, v2) * versionOrder;
63             if (result == 0) {
64                 result = collator.compare(label1, label2) * labelOrder;
65                 if (result == 0)
66                     result = collator.compare(provider1, provider2) * providerOrder;
67             }
68         } else if (key == FEATURE_LABEL) {
69             result = collator.compare(label1, label2) * labelOrder;
70             if (result == 0) {
71                 result = compareVersions(v1, v2) * versionOrder;
72                 if (result == 0)
73                     result = collator.compare(provider1, provider2) * providerOrder;
74             }
75         } else if (key == FEATURE_PROVIDER) {
76             result = collator.compare(provider1, provider2) * providerOrder;
77             if (result == 0) {
78                 result = collator.compare(label1, label2) * labelOrder;
79                 if (result == 0)
80                     result = compareVersions(v1, v2) * versionOrder;
81             }
82         }
83         return result;
84     }
85     
86     private int compareVersions(PluginVersionIdentifier v1, PluginVersionIdentifier v2) {
87         if (v1.equals(v2))
88             return 0;
89         return v2.isGreaterThan(v1) ? -1 : 1;
90     }
91     
92 }
93
Popular Tags