Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3348f6e4a685e61f9c45b1e2cb59bc73a86da394 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
 *  Copyright (c) 2000, 2010 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.viewers;

import org.eclipse.equinox.internal.p2.ui.ProvUI;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;

public class IUComparator extends ViewerComparator {
	public static final int IU_NAME = 0;
	public static final int IU_ID = 1;
	private int key;
	private boolean showingId = false;

	public IUComparator(int sortKey) {
		this.key = sortKey;
		showingId = sortKey == IU_ID;
	}

	/**
	 * Use the specified column config to determine
	 * whether the id should be used in lieu of an empty name
	 * when sorting.
	 * 
	 * @param columnConfig
	 */
	public void useColumnConfig(IUColumnConfig[] columnConfig) {
		for (int i = 0; i < columnConfig.length; i++)
			if (columnConfig[i].getColumnType() == IUColumnConfig.COLUMN_ID) {
				showingId = true;
				break;
			}
	}

	@Override
	public int compare(Viewer viewer, Object obj1, Object obj2) {
		IInstallableUnit iu1 = ProvUI.getAdapter(obj1, IInstallableUnit.class);
		IInstallableUnit iu2 = ProvUI.getAdapter(obj2, IInstallableUnit.class);
		if (iu1 == null || iu2 == null)
			// If these are not iu's use the super class comparator.
			return super.compare(viewer, obj1, obj2);

		String key1, key2;
		if (key == IU_NAME) {
			// Compare the iu names in the default locale.
			// If a name is not defined, we use blank if we know the id is shown in another
			// column.  If the id is not shown elsewhere, then we are displaying it, so use
			// the id instead.
			key1 = iu1.getProperty(IInstallableUnit.PROP_NAME, null);
			if (key1 == null)
				if (showingId)
					key1 = ""; //$NON-NLS-1$
				else
					key1 = iu1.getId();
			key2 = iu2.getProperty(IInstallableUnit.PROP_NAME, null);
			if (key2 == null)
				if (showingId)
					key2 = ""; //$NON-NLS-1$
				else
					key2 = iu2.getId();
		} else {
			key1 = iu1.getId();
			key2 = iu2.getId();
		}

		int result = 0;
		result = key1.compareToIgnoreCase(key2);
		if (result == 0) {
			// We want to show later versions first so compare backwards.
			result = iu2.getVersion().compareTo(iu1.getVersion());
		}
		return result;
	}
}

Back to the top