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