Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/ViewerComparator.d @ 10:b6c35faf97c8
Viewers
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 31 Mar 2008 00:47:19 +0200 |
parents | |
children | bade933d6ef6 |
comparison
equal
deleted
inserted
replaced
9:6c14e54dfc11 | 10:b6c35faf97c8 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 ******************************************************************************/ | |
13 | |
14 module dwtx.jface.viewers.ViewerComparator; | |
15 | |
16 import dwtx.jface.viewers.Viewer; | |
17 import dwtx.jface.viewers.ContentViewer; | |
18 import dwtx.jface.viewers.IBaseLabelProvider; | |
19 import dwtx.jface.viewers.ILabelProvider; | |
20 | |
21 // import java.util.Arrays; | |
22 // import java.util.Comparator; | |
23 | |
24 import dwtx.jface.util.Policy; | |
25 | |
26 import dwt.dwthelper.utils; | |
27 import tango.core.Array; | |
28 | |
29 /** | |
30 * A viewer comparator is used by a {@link StructuredViewer} to | |
31 * reorder the elements provided by its content provider. | |
32 * <p> | |
33 * The default <code>compare</code> method compares elements using two steps. | |
34 * The first step uses the values returned from <code>category</code>. | |
35 * By default, all elements are in the same category. | |
36 * The second level is based on a case insensitive compare of the strings obtained | |
37 * from the content viewer's label provider via <code>ILabelProvider.getText</code>. | |
38 * </p> | |
39 * <p> | |
40 * Subclasses may implement the <code>isSorterProperty</code> method; | |
41 * they may reimplement the <code>category</code> method to provide | |
42 * categorization; and they may override the <code>compare</code> methods | |
43 * to provide a totally different way of sorting elements. | |
44 * </p> | |
45 * @see IStructuredContentProvider | |
46 * @see StructuredViewer | |
47 * | |
48 * @since 3.2 | |
49 */ | |
50 public class ViewerComparator { | |
51 /** | |
52 * The comparator to use to sort a viewer's contents. | |
53 */ | |
54 private Comparator comparator; | |
55 | |
56 /** | |
57 * Creates a new {@link ViewerComparator}, which uses the default comparator | |
58 * to sort strings. | |
59 * | |
60 */ | |
61 public this(){ | |
62 this(null); | |
63 } | |
64 | |
65 /** | |
66 * Creates a new {@link ViewerComparator}, which uses the given comparator | |
67 * to sort strings. | |
68 * | |
69 * @param comparator | |
70 */ | |
71 public this(Comparator comparator){ | |
72 this.comparator = comparator; | |
73 } | |
74 | |
75 /** | |
76 * Returns the comparator used to sort strings. | |
77 * | |
78 * @return the comparator used to sort strings | |
79 */ | |
80 protected Comparator getComparator() { | |
81 if (comparator is null){ | |
82 comparator = Policy.getComparator(); | |
83 } | |
84 return comparator; | |
85 } | |
86 | |
87 /** | |
88 * Returns the category of the given element. The category is a | |
89 * number used to allocate elements to bins; the bins are arranged | |
90 * in ascending numeric order. The elements within a bin are arranged | |
91 * via a second level sort criterion. | |
92 * <p> | |
93 * The default implementation of this framework method returns | |
94 * <code>0</code>. Subclasses may reimplement this method to provide | |
95 * non-trivial categorization. | |
96 * </p> | |
97 * | |
98 * @param element the element | |
99 * @return the category | |
100 */ | |
101 public int category(Object element) { | |
102 return 0; | |
103 } | |
104 | |
105 /** | |
106 * Returns a negative, zero, or positive number depending on whether | |
107 * the first element is less than, equal to, or greater than | |
108 * the second element. | |
109 * <p> | |
110 * The default implementation of this method is based on | |
111 * comparing the elements' categories as computed by the <code>category</code> | |
112 * framework method. Elements within the same category are further | |
113 * subjected to a case insensitive compare of their label strings, either | |
114 * as computed by the content viewer's label provider, or their | |
115 * <code>toString</code> values in other cases. Subclasses may override. | |
116 * </p> | |
117 * | |
118 * @param viewer the viewer | |
119 * @param e1 the first element | |
120 * @param e2 the second element | |
121 * @return a negative number if the first element is less than the | |
122 * second element; the value <code>0</code> if the first element is | |
123 * equal to the second element; and a positive number if the first | |
124 * element is greater than the second element | |
125 */ | |
126 public int compare(Viewer viewer, Object e1, Object e2) { | |
127 int cat1 = category(e1); | |
128 int cat2 = category(e2); | |
129 | |
130 if (cat1 !is cat2) { | |
131 return cat1 - cat2; | |
132 } | |
133 | |
134 String name1; | |
135 String name2; | |
136 | |
137 if (viewer is null || !(cast(ContentViewer)viewer )) { | |
138 name1 = e1.toString(); | |
139 name2 = e2.toString(); | |
140 } else { | |
141 IBaseLabelProvider prov = (cast(ContentViewer) viewer) | |
142 .getLabelProvider(); | |
143 if (auto lprov = cast(ILabelProvider)prov ) { | |
144 name1 = lprov.getText(e1); | |
145 name2 = lprov.getText(e2); | |
146 } else { | |
147 name1 = e1.toString(); | |
148 name2 = e2.toString(); | |
149 } | |
150 } | |
151 if (name1 is null) { | |
152 name1 = "";//$NON-NLS-1$ | |
153 } | |
154 if (name2 is null) { | |
155 name2 = "";//$NON-NLS-1$ | |
156 } | |
157 | |
158 // use the comparator to compare the strings | |
159 return getComparator().compare( new ArrayWrapperString(name1), new ArrayWrapperString(name2)); | |
160 } | |
161 | |
162 /** | |
163 * Returns whether this viewer sorter would be affected | |
164 * by a change to the given property of the given element. | |
165 * <p> | |
166 * The default implementation of this method returns <code>false</code>. | |
167 * Subclasses may reimplement. | |
168 * </p> | |
169 * | |
170 * @param element the element | |
171 * @param property the property | |
172 * @return <code>true</code> if the sorting would be affected, | |
173 * and <code>false</code> if it would be unaffected | |
174 */ | |
175 public bool isSorterProperty(Object element, String property) { | |
176 return false; | |
177 } | |
178 | |
179 /** | |
180 * Sorts the given elements in-place, modifying the given array. | |
181 * <p> | |
182 * The default implementation of this method uses the | |
183 * java.util.Arrays#sort algorithm on the given array, | |
184 * calling <code>compare</code> to compare elements. | |
185 * </p> | |
186 * <p> | |
187 * Subclasses may reimplement this method to provide a more optimized implementation. | |
188 * </p> | |
189 * | |
190 * @param viewer the viewer | |
191 * @param elements the elements to sort | |
192 */ | |
193 public void sort(Viewer viewer, Object[] elements) { | |
194 tango.core.Array.sort(elements, delegate int(Object a, Object b) { | |
195 return compare(viewer, a, b); | |
196 } | |
197 ); | |
198 } | |
199 } |