Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/TreeSelection.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
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 org.eclipse.jface.viewers.TreeSelection; | |
14 | |
15 import org.eclipse.jface.viewers.StructuredSelection; | |
16 import org.eclipse.jface.viewers.ITreeSelection; | |
17 import org.eclipse.jface.viewers.CustomHashtable; | |
18 import org.eclipse.jface.viewers.TreePath; | |
19 import org.eclipse.jface.viewers.IElementComparer; | |
20 | |
21 | |
22 import org.eclipse.core.runtime.Assert; | |
23 | |
24 import java.lang.all; | |
25 import java.util.List; | |
26 import java.util.ArrayList; | |
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 List 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 selection = new ArrayList(size); | |
71 for (int i = 0; i < size; i++) { | |
72 Object lastSegment= paths[i].getLastSegment(); | |
73 Object mapped= element2TreePaths.get(lastSegment); | |
74 if (mapped is null) { | |
75 selection.add(lastSegment); | |
76 element2TreePaths.put(lastSegment, paths[i]); | |
77 } else if ( cast(List)mapped ) { | |
78 (cast(List)mapped).add( cast(Object)paths[i]); | |
79 } else { | |
80 List newMapped= new ArrayList(); | |
81 newMapped.add(mapped); | |
82 newMapped.add(paths[i]); | |
83 element2TreePaths.put(lastSegment, cast(Object) newMapped); | |
84 } | |
85 } | |
86 } | |
87 } | |
88 | |
89 /** | |
90 * Constructs a selection based on the elements identified by the given tree | |
91 * paths. | |
92 * | |
93 * @param paths | |
94 * tree paths | |
95 */ | |
96 public this(TreePath[] paths) { | |
97 this(new InitializeData(paths, null)); | |
98 } | |
99 | |
100 /** | |
101 * Constructs a selection based on the elements identified by the given tree | |
102 * paths. | |
103 * | |
104 * @param paths | |
105 * tree paths | |
106 * @param comparer | |
107 * the comparer, or <code>null</code> if default equals is to be used | |
108 */ | |
109 public this(TreePath[] paths, IElementComparer comparer) { | |
110 this(new InitializeData(paths, comparer)); | |
111 } | |
112 | |
113 /** | |
114 * Constructs a selection based on the elements identified by the given tree | |
115 * path. | |
116 * | |
117 * @param treePath | |
118 * tree path, or <code>null</code> for an empty selection | |
119 */ | |
120 public this(TreePath treePath) { | |
121 this(treePath !is null ? [ treePath ] : EMPTY_TREE_PATHS, null); | |
122 } | |
123 | |
124 /** | |
125 * Constructs a selection based on the elements identified by the given tree | |
126 * path. | |
127 * | |
128 * @param treePath | |
129 * tree path, or <code>null</code> for an empty selection | |
130 * @param comparer | |
131 * the comparer, or <code>null</code> if default equals is to be used | |
132 */ | |
133 public this(TreePath treePath, IElementComparer comparer) { | |
134 this(treePath !is null ? [ treePath ] : EMPTY_TREE_PATHS, comparer); | |
135 } | |
136 | |
137 /** | |
138 * Creates a new tree selection based on the initialization data. | |
139 * | |
140 * @param data the data | |
141 */ | |
142 private this(InitializeData data) { | |
143 super(data.selection); | |
144 paths= data.paths; | |
145 element2TreePaths= data.element2TreePaths; | |
146 } | |
147 | |
148 /** | |
149 * Creates a new empty selection. See also the static field | |
150 * <code>EMPTY</code> which contains an empty selection singleton. | |
151 * <p> | |
152 * Note that TreeSelection.EMPTY is not equals() to StructuredViewer.EMPTY. | |
153 * </p> | |
154 * | |
155 * @see #EMPTY | |
156 */ | |
157 public this() { | |
158 super(); | |
159 } | |
160 | |
161 /** | |
162 * Returns the element comparer passed in when the tree selection | |
163 * has been created or <code>null</code> if no comparer has been | |
164 * provided. | |
165 * | |
166 * @return the element comparer or <code>null</code> | |
167 * | |
168 * @since 3.2 | |
169 */ | |
170 public IElementComparer getElementComparer() { | |
171 if (element2TreePaths is null) | |
172 return null; | |
173 return element2TreePaths.getComparer(); | |
174 } | |
175 | |
176 public override int opEquals(Object obj) { | |
177 if (!(cast(TreeSelection)obj)) { | |
178 // Fall back to super implementation, see bug 135837. | |
179 return super.opEquals(obj); | |
180 } | |
181 TreeSelection selection = cast(TreeSelection) obj; | |
182 int size = getPaths().length; | |
183 if (selection.getPaths().length is size) { | |
184 IElementComparer comparerOrNull = (getElementComparer() is selection | |
185 .getElementComparer()) ? getElementComparer() : null; | |
186 if (size > 0) { | |
187 for (int i = 0; i < paths.length; i++) { | |
188 if (!paths[i].opEquals(selection.paths[i], comparerOrNull)) { | |
189 return false; | |
190 } | |
191 } | |
192 } | |
193 return true; | |
194 } | |
195 return false; | |
196 } | |
197 | |
198 public override hash_t toHash() { | |
199 int code = this.classinfo.toHash(); | |
200 if (paths !is null) { | |
201 for (int i = 0; i < paths.length; i++) { | |
202 code = code * 17 + paths[i].toHash(getElementComparer()); | |
203 } | |
204 } | |
205 return code; | |
206 } | |
207 | |
208 public TreePath[] getPaths() { | |
209 return paths is null ? EMPTY_TREE_PATHS : paths.dup; | |
210 } | |
211 | |
212 public TreePath[] getPathsFor(Object element) { | |
213 Object value= element2TreePaths is null ? null : element2TreePaths.get(element); | |
214 if (value is null) { | |
215 return EMPTY_TREE_PATHS; | |
216 } else if (cast(TreePath)value ) { | |
217 return [ cast(TreePath)value ]; | |
218 } else if (cast(List)value ) { | |
219 auto l= cast(List)value; | |
220 return arraycast!(TreePath)( l.toArray() ); | |
221 } else { | |
222 // should not happen: | |
223 Assert.isTrue(false, "Unhandled case"); //$NON-NLS-1$ | |
224 return null; | |
225 } | |
226 } | |
227 } |