KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > ViewComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.internal.dialogs;
12
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.viewers.ViewerComparator;
15 import org.eclipse.ui.internal.registry.Category;
16 import org.eclipse.ui.internal.registry.ViewRegistry;
17 import org.eclipse.ui.internal.registry.ViewRegistryReader;
18 import org.eclipse.ui.views.IViewCategory;
19 import org.eclipse.ui.views.IViewDescriptor;
20
21 /**
22  * This is used to sort views in a ShowViewDialog.
23  */

24 public class ViewComparator extends ViewerComparator {
25     private ViewRegistry viewReg;
26
27     /**
28      * ViewSorter constructor comment.
29      *
30      * @param reg an IViewRegistry
31      */

32     public ViewComparator(ViewRegistry reg) {
33         super();
34         viewReg = reg;
35     }
36
37     /**
38      * Returns a negative, zero, or positive number depending on whether
39      * the first element is less than, equal to, or greater than
40      * the second element.
41      */

42     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
43         if (e1 instanceof IViewDescriptor) {
44             String JavaDoc str1 = DialogUtil.removeAccel(((IViewDescriptor) e1)
45                     .getLabel());
46             String JavaDoc str2 = DialogUtil.removeAccel(((IViewDescriptor) e2)
47                     .getLabel());
48             return getComparator().compare(str1, str2);
49         } else if (e1 instanceof IViewCategory) {
50             IViewCategory generalCategory = viewReg.findCategory(ViewRegistryReader.GENERAL_VIEW_ID);
51             if (generalCategory != null){
52                 if (((IViewCategory)e1).getId().equals(generalCategory.getId())) {
53                     return -1;
54                 }
55                 if (((IViewCategory)e2).getId().equals(generalCategory.getId())) {
56                     return 1;
57                 }
58             }
59             Category miscCategory = viewReg.getMiscCategory();
60             if(miscCategory != null){
61                 if (((IViewCategory)e1).getId().equals(miscCategory.getId())) {
62                     return 1;
63                 }
64                 if (((IViewCategory)e2).getId().equals(miscCategory.getId())) {
65                     return -1;
66                 }
67             }
68             String JavaDoc str1 = DialogUtil.removeAccel(((IViewCategory) e1).getLabel());
69             String JavaDoc str2 = DialogUtil.removeAccel(((IViewCategory) e2).getLabel());
70             return getComparator().compare(str1, str2);
71         }
72         return 0;
73     }
74 }
75
Popular Tags