Mercurial > projects > dwt-addons
comparison dwtx/jface/viewers/TreePathViewerSorter.d @ 10:b6c35faf97c8
Viewers
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 31 Mar 2008 00:47:19 +0200 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
9:6c14e54dfc11 | 10:b6c35faf97c8 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006, 2007 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.TreePathViewerSorter; | |
15 | |
16 import dwtx.jface.viewers.ViewerSorter; | |
17 import dwtx.jface.viewers.TreePath; | |
18 import dwtx.jface.viewers.Viewer; | |
19 | |
20 // import java.util.Arrays; | |
21 // import java.util.Comparator; | |
22 | |
23 import dwt.dwthelper.utils; | |
24 import tango.core.Array; | |
25 | |
26 /** | |
27 * A viewer sorter that is provided extra context in the form of the path of the | |
28 * parent element of the elements being sorted. | |
29 * | |
30 * @since 3.2 | |
31 */ | |
32 public class TreePathViewerSorter : ViewerSorter { | |
33 public alias ViewerSorter.category category; | |
34 public alias ViewerSorter.compare compare; | |
35 public alias ViewerSorter.isSorterProperty isSorterProperty; | |
36 | |
37 /** | |
38 * Provide a category for the given element that will have the given parent | |
39 * path when it is added to the viewer. The provided path is | |
40 * relative to the viewer input. The parent path will | |
41 * be <code>null</code> when the elements are root elements. | |
42 * <p> | |
43 * By default, the this method calls | |
44 * {@link ViewerSorter#category(Object)}. Subclasses may override. | |
45 * | |
46 * @param parentPath | |
47 * the parent path for the element | |
48 * @param element | |
49 * the element | |
50 * @return the category of the element | |
51 */ | |
52 public int category(TreePath parentPath, Object element) { | |
53 return category(element); | |
54 } | |
55 | |
56 /** | |
57 * Compare the given elements that will have the given parent | |
58 * path when they are added to the viewer. The provided path is | |
59 * relative to the viewer input. The parent path will | |
60 * be <code>null</code> when the elements are root elements. | |
61 * <p> | |
62 * By default, the this method calls | |
63 * {@link ViewerSorter#sort(Viewer, Object[])}. Subclasses may override. | |
64 * @param viewer the viewer | |
65 * @param parentPath the parent path for the two elements | |
66 * @param e1 the first element | |
67 * @param e2 the second element | |
68 * @return a negative number if the first element is less than the | |
69 * second element; the value <code>0</code> if the first element is | |
70 * equal to the second element; and a positive | |
71 */ | |
72 public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) { | |
73 return compare(viewer, e1, e2); | |
74 } | |
75 | |
76 /** | |
77 * Returns whether this viewer sorter would be affected | |
78 * by a change to the given property of the given element. | |
79 * The provided path is | |
80 * relative to the viewer input. The parent path will | |
81 * be <code>null</code> when the elements are root elements. | |
82 * <p> | |
83 * The default implementation of this method calls | |
84 * {@link ViewerSorter#isSorterProperty(Object, String)}. | |
85 * Subclasses may reimplement. | |
86 * @param parentPath the parent path of the element | |
87 * @param element the element | |
88 * @param property the property | |
89 * @return <code>true</code> if the sorting would be affected, | |
90 * and <code>false</code> if it would be unaffected | |
91 */ | |
92 public bool isSorterProperty(TreePath parentPath, Object element, String property) { | |
93 return isSorterProperty(element, property); | |
94 } | |
95 | |
96 /** | |
97 * Sorts the given elements in-place, modifying the given array. | |
98 * The provided path is | |
99 * relative to the viewer input. The parent path will | |
100 * be <code>null</code> when the elements are root elements. | |
101 * <p> | |
102 * The default implementation of this method uses the | |
103 * java.util.Arrays#sort algorithm on the given array, | |
104 * calling {@link #compare(Viewer, TreePath, Object, Object)} to compare elements. | |
105 * </p> | |
106 * <p> | |
107 * Subclasses may reimplement this method to provide a more optimized implementation. | |
108 * </p> | |
109 * | |
110 * @param viewer the viewer | |
111 * @param parentPath the parent path of the given elements | |
112 * @param elements the elements to sort | |
113 */ | |
114 public void sort(Viewer viewer, TreePath parentPath, Object[] elements) { | |
115 tango.core.Array.sort(elements, delegate int (Object a, Object b) { | |
116 return compare(viewer, parentPath, a, b); | |
117 } | |
118 ); | |
119 } | |
120 } |