01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
11:
12: import org.eclipse.jface.viewers.Viewer;
13: import org.eclipse.jface.viewers.ViewerComparator;
14: import org.eclipse.ui.internal.registry.Category;
15: import org.eclipse.ui.internal.registry.ViewRegistry;
16: import org.eclipse.ui.internal.registry.ViewRegistryReader;
17: import org.eclipse.ui.views.IViewCategory;
18: import org.eclipse.ui.views.IViewDescriptor;
19:
20: /**
21: * This is used to sort views in a ShowViewDialog.
22: */
23: public class ViewComparator extends ViewerComparator {
24: private ViewRegistry viewReg;
25:
26: /**
27: * ViewSorter constructor comment.
28: *
29: * @param reg an IViewRegistry
30: */
31: public ViewComparator(ViewRegistry reg) {
32: super ();
33: viewReg = reg;
34: }
35:
36: /**
37: * Returns a negative, zero, or positive number depending on whether
38: * the first element is less than, equal to, or greater than
39: * the second element.
40: */
41: public int compare(Viewer viewer, Object e1, Object e2) {
42: if (e1 instanceof IViewDescriptor) {
43: String str1 = DialogUtil.removeAccel(((IViewDescriptor) e1)
44: .getLabel());
45: String str2 = DialogUtil.removeAccel(((IViewDescriptor) e2)
46: .getLabel());
47: return getComparator().compare(str1, str2);
48: } else if (e1 instanceof IViewCategory) {
49: IViewCategory generalCategory = viewReg
50: .findCategory(ViewRegistryReader.GENERAL_VIEW_ID);
51: if (generalCategory != null) {
52: if (((IViewCategory) e1).getId().equals(
53: generalCategory.getId())) {
54: return -1;
55: }
56: if (((IViewCategory) e2).getId().equals(
57: generalCategory.getId())) {
58: return 1;
59: }
60: }
61: Category miscCategory = viewReg.getMiscCategory();
62: if (miscCategory != null) {
63: if (((IViewCategory) e1).getId().equals(
64: miscCategory.getId())) {
65: return 1;
66: }
67: if (((IViewCategory) e2).getId().equals(
68: miscCategory.getId())) {
69: return -1;
70: }
71: }
72: String str1 = DialogUtil.removeAccel(((IViewCategory) e1)
73: .getLabel());
74: String str2 = DialogUtil.removeAccel(((IViewCategory) e2)
75: .getLabel());
76: return getComparator().compare(str1, str2);
77: }
78: return 0;
79: }
80: }
|