Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/TreeSelection.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | ea8ff534f622 |
children |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
2 * Copyright (c) 2005, 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 module dwtx.jface.viewers.TreeSelection; | |
14 | |
15 import dwtx.jface.viewers.StructuredSelection; | |
16 import dwtx.jface.viewers.ITreeSelection; | |
17 import dwtx.jface.viewers.CustomHashtable; | |
18 import dwtx.jface.viewers.TreePath; | |
19 import dwtx.jface.viewers.IElementComparer; | |
20 | |
21 | |
22 import dwtx.core.runtime.Assert; | |
23 | |
24 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
25 import dwtx.dwtxhelper.Collection; |
10 | 26 |
27 /** | |
28 * A concrete implementation of the <code>ITreeSelection</code> interface, | |
29 * suitable for instantiating. | |
30 * <p> | |
31 * This class is not intended to be subclassed. | |
32 * </p> | |
33 * | |
34 * @since 3.2 | |
35 */ | |
36 public class TreeSelection : StructuredSelection, ITreeSelection { | |
37 | |
38 /* Implementation note. This class extends StructuredSelection because many pre-existing | |
39 * JFace viewer clients assumed that the only implementation of IStructuredSelection | |
40 * was StructuredSelection. By extending StructuredSelection rather than implementing | |
41 * ITreeSelection directly, we avoid this problem. | |
42 * For more details, see Bug 121939 [Viewers] TreeSelection should subclass StructuredSelection. | |
43 */ | |
44 | |
45 private TreePath[] paths = null; | |
46 private CustomHashtable element2TreePaths = null; | |
47 | |
48 /** | |
49 * The canonical empty selection. This selection should be used instead of | |
50 * <code>null</code>. | |
51 */ | |
52 public static const TreeSelection EMPTY; | |
53 | |
54 private static const TreePath[] EMPTY_TREE_PATHS = null; | |
55 static this(){ | |
56 EMPTY = new TreeSelection(); | |
57 } | |
58 | |
59 private static class InitializeData { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
60 List selection; |
10 | 61 TreePath[] paths; |
62 CustomHashtable element2TreePaths; | |
63 | |
64 private this(TreePath[] paths, IElementComparer comparer) { | |
65 this.paths= new TreePath[paths.length]; | |
66 System.arraycopy(paths, 0, this.paths, 0, paths.length); | |
67 element2TreePaths = new CustomHashtable(comparer); | |
68 int size = paths.length; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
69 selection = new ArrayList(size); |
10 | 70 for (int i = 0; i < size; i++) { |
71 Object lastSegment= paths[i].getLastSegment(); | |
72 Object mapped= element2TreePaths.get(lastSegment); | |
73 if (mapped is null) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
74 selection.add(lastSegment); |
10 | 75 element2TreePaths.put(lastSegment, paths[i]); |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
76 } else if ( cast(List)mapped ) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
77 (cast(List)mapped).add( cast(Object)paths[i]); |
10 | 78 } else { |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
79 List newMapped= new ArrayList(); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
80 newMapped.add(mapped); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
81 newMapped.add(paths[i]); |
10 | 82 element2TreePaths.put(lastSegment, cast(Object) newMapped); |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 /** | |
89 * Constructs a selection based on the elements identified by the given tree | |
90 * paths. | |
91 * | |
92 * @param paths | |
93 * tree paths | |
94 */ | |
95 public this(TreePath[] paths) { | |
96 this(new InitializeData(paths, null)); | |
97 } | |
98 | |
99 /** | |
100 * Constructs a selection based on the elements identified by the given tree | |
101 * paths. | |
102 * | |
103 * @param paths | |
104 * tree paths | |
105 * @param comparer | |
106 * the comparer, or <code>null</code> if default equals is to be used | |
107 */ | |
108 public this(TreePath[] paths, IElementComparer comparer) { | |
109 this(new InitializeData(paths, comparer)); | |
110 } | |
111 | |
112 /** | |
113 * Constructs a selection based on the elements identified by the given tree | |
114 * path. | |
115 * | |
116 * @param treePath | |
117 * tree path, or <code>null</code> for an empty selection | |
118 */ | |
119 public this(TreePath treePath) { | |
120 this(treePath !is null ? [ treePath ] : EMPTY_TREE_PATHS, null); | |
121 } | |
122 | |
123 /** | |
124 * Constructs a selection based on the elements identified by the given tree | |
125 * path. | |
126 * | |
127 * @param treePath | |
128 * tree path, or <code>null</code> for an empty selection | |
129 * @param comparer | |
130 * the comparer, or <code>null</code> if default equals is to be used | |
131 */ | |
132 public this(TreePath treePath, IElementComparer comparer) { | |
133 this(treePath !is null ? [ treePath ] : EMPTY_TREE_PATHS, comparer); | |
134 } | |
135 | |
136 /** | |
137 * Creates a new tree selection based on the initialization data. | |
138 * | |
139 * @param data the data | |
140 */ | |
141 private this(InitializeData data) { | |
142 super(data.selection); | |
143 paths= data.paths; | |
144 element2TreePaths= data.element2TreePaths; | |
145 } | |
146 | |
147 /** | |
148 * Creates a new empty selection. See also the static field | |
149 * <code>EMPTY</code> which contains an empty selection singleton. | |
150 * <p> | |
151 * Note that TreeSelection.EMPTY is not equals() to StructuredViewer.EMPTY. | |
152 * </p> | |
153 * | |
154 * @see #EMPTY | |
155 */ | |
156 public this() { | |
157 super(); | |
158 } | |
159 | |
160 /** | |
161 * Returns the element comparer passed in when the tree selection | |
162 * has been created or <code>null</code> if no comparer has been | |
163 * provided. | |
164 * | |
165 * @return the element comparer or <code>null</code> | |
166 * | |
167 * @since 3.2 | |
168 */ | |
169 public IElementComparer getElementComparer() { | |
170 if (element2TreePaths is null) | |
171 return null; | |
172 return element2TreePaths.getComparer(); | |
173 } | |
174 | |
175 public override int opEquals(Object obj) { | |
176 if (!(cast(TreeSelection)obj)) { | |
177 // Fall back to super implementation, see bug 135837. | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
178 return super.opEquals(obj); |
10 | 179 } |
180 TreeSelection selection = cast(TreeSelection) obj; | |
181 int size = getPaths().length; | |
182 if (selection.getPaths().length is size) { | |
183 IElementComparer comparerOrNull = (getElementComparer() is selection | |
184 .getElementComparer()) ? getElementComparer() : null; | |
185 if (size > 0) { | |
186 for (int i = 0; i < paths.length; i++) { | |
187 if (!paths[i].opEquals(selection.paths[i], comparerOrNull)) { | |
188 return false; | |
189 } | |
190 } | |
191 } | |
192 return true; | |
193 } | |
194 return false; | |
195 } | |
196 | |
197 public override hash_t toHash() { | |
198 int code = this.classinfo.toHash(); | |
199 if (paths !is null) { | |
200 for (int i = 0; i < paths.length; i++) { | |
201 code = code * 17 + paths[i].toHash(getElementComparer()); | |
202 } | |
203 } | |
204 return code; | |
205 } | |
206 | |
207 public TreePath[] getPaths() { | |
208 return paths is null ? EMPTY_TREE_PATHS : paths.dup; | |
209 } | |
210 | |
211 public TreePath[] getPathsFor(Object element) { | |
212 Object value= element2TreePaths is null ? null : element2TreePaths.get(element); | |
213 if (value is null) { | |
214 return EMPTY_TREE_PATHS; | |
215 } else if (cast(TreePath)value ) { | |
216 return [ cast(TreePath)value ]; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
217 } else if (cast(List)value ) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
218 auto l= cast(List)value; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
219 return arraycast!(TreePath)( l.toArray() ); |
10 | 220 } else { |
221 // should not happen: | |
222 Assert.isTrue(false, "Unhandled case"); //$NON-NLS-1$ | |
223 return null; | |
224 } | |
225 } | |
226 } |