KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > CategoryComparator


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.views.markers.internal;
12
13 import java.util.Comparator JavaDoc;
14
15 import org.eclipse.jface.dialogs.IDialogSettings;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerComparator;
18
19 /**
20  * CategorySorter is the sorter that takes categories and the viewer into
21  * account.
22  *
23  */

24 public class CategoryComparator extends ViewerComparator implements Comparator JavaDoc {
25     TableComparator innerSorter;
26
27     IField categoryField;
28
29     private final String JavaDoc TAG_FIELD = "categoryField"; //$NON-NLS-1$
30

31     /**
32      * Create a new instance of the receiver wrapping a sorter.
33      *
34      * @param sorter
35      */

36     CategoryComparator(TableComparator sorter) {
37         innerSorter = sorter;
38     }
39
40     /**
41      * Compare obj1 and obj starting with field depth.
42      *
43      * @param obj1
44      * @param obj2
45      * @param depth
46      * @param continueSearching
47      * @return int
48      * @see ViewerComparator#compare(Viewer, Object, Object)
49      */

50     int compare(Object JavaDoc obj1, Object JavaDoc obj2, int depth, boolean continueSearching) {
51
52         if (obj1 == null || obj2 == null || !(obj1 instanceof MarkerNode)
53                 || !(obj2 instanceof MarkerNode)) {
54             return 0;
55         }
56
57         MarkerNode marker1;
58         MarkerNode marker2;
59
60         marker1 = (MarkerNode) obj1;
61         marker2 = (MarkerNode) obj2;
62
63         if (categoryField == null) {
64             return innerSorter.compare(marker1, marker2, depth,
65                     continueSearching);
66         }
67
68         int result = categoryField.compare(marker1, marker2);
69         if (continueSearching && result == 0) {
70             return innerSorter.compare(marker1, marker2, 0, continueSearching);
71         }
72         return result;
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer,
79      * java.lang.Object, java.lang.Object)
80      */

81     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
82         return compare(e1, e2, 0, true);
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
89      */

90     public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
91         return compare(arg0, arg1, 0, true);
92     }
93
94     /**
95      * Get the category field.
96      *
97      * @return IField
98      */

99     public IField getCategoryField() {
100         return categoryField;
101     }
102
103     /**
104      * Set the field that we are categorizing by.
105      *
106      * @param field
107      */

108     public void setCategoryField(IField field) {
109         this.categoryField = field;
110     }
111
112     /**
113      * Set the inner sorter to the new sorter.
114      *
115      * @param sorter2
116      */

117     public void setTableSorter(TableComparator sorter2) {
118         innerSorter = sorter2;
119
120     }
121
122     /**
123      * Save the state of the receiver.
124      *
125      * @param dialogSettings
126      */

127     public void saveState(IDialogSettings dialogSettings) {
128         if (dialogSettings == null) {
129             return;
130         }
131
132         IDialogSettings settings = dialogSettings
133                 .getSection(TableComparator.TAG_DIALOG_SECTION);
134         if (settings == null) {
135             settings = dialogSettings
136                     .addNewSection(TableComparator.TAG_DIALOG_SECTION);
137         }
138
139         String JavaDoc description = Util.EMPTY_STRING;
140         if (categoryField != null) {
141             description = categoryField.getDescription();
142         }
143
144         settings.put(TAG_FIELD, description);
145
146     }
147
148     /**
149      * Restore the state of the receiver from the dialog settings.
150      *
151      * @param dialogSettings
152      * @param view
153      */

154     public void restoreState(IDialogSettings dialogSettings, ProblemView view) {
155         if (dialogSettings == null) {
156             selectDefaultGrouping(view);
157             return;
158         }
159
160         IDialogSettings settings = dialogSettings
161                 .getSection(TableComparator.TAG_DIALOG_SECTION);
162         if (settings == null) {
163             selectDefaultGrouping(view);
164             return;
165         }
166
167         String JavaDoc description = settings.get(TAG_FIELD);
168         view.selectCategory(description, this);
169     }
170
171     /**
172      * Select the default grouping in the problem view
173      * @param view
174      */

175     private void selectDefaultGrouping(ProblemView view) {
176         view.selectCategoryField(MarkerSupportRegistry.getInstance()
177                 .getDefaultGroup(), this);
178     }
179 }
180
Popular Tags