Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredSelection.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) 2000, 2008 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.StructuredSelection; | |
14 | |
15 import org.eclipse.jface.viewers.IStructuredSelection; | |
16 import org.eclipse.jface.viewers.IElementComparer; | |
17 | |
18 | |
19 import org.eclipse.core.runtime.Assert; | |
20 import org.eclipse.jface.resource.JFaceResources; | |
21 | |
22 import java.lang.all; | |
23 import java.util.Arrays; | |
24 import java.util.List; | |
25 import java.util.Iterator; | |
26 | |
27 /** | |
28 * A concrete implementation of the <code>IStructuredSelection</code> interface, | |
29 * suitable for instantiating. | |
30 * <p> | |
31 * This class is not intended to be subclassed. | |
32 * </p> | |
33 * @noextend This class is not intended to be subclassed by clients. | |
34 */ | |
35 public class StructuredSelection : IStructuredSelection { | |
36 | |
37 /** | |
38 * The element that make up this structured selection. | |
39 */ | |
40 private Object[] elements; | |
41 | |
42 /** | |
43 * The element comparer, or <code>null</code> | |
44 */ | |
45 private IElementComparer comparer; | |
46 | |
47 /** | |
48 * The canonical empty selection. This selection should be used instead of | |
49 * <code>null</code>. | |
50 */ | |
51 public static const StructuredSelection EMPTY; | |
52 static this(){ | |
53 EMPTY = new StructuredSelection(); | |
54 } | |
55 /** | |
56 * Creates a new empty selection. | |
57 * See also the static field <code>EMPTY</code> which contains an empty selection singleton. | |
58 * | |
59 * @see #EMPTY | |
60 */ | |
61 public this() { | |
62 } | |
63 | |
64 /** | |
65 * Creates a structured selection from the given elements. | |
66 * | |
67 * @param elements an array of elements | |
68 */ | |
69 public this(Object[] elements) { | |
70 this.elements = new Object[elements.length]; | |
71 System.arraycopy(elements, 0, this.elements, 0, elements.length); | |
72 } | |
73 | |
74 /** | |
75 * Creates a structured selection containing a single object. | |
76 * The object must not be <code>null</code>. | |
77 * | |
78 * @param element the element | |
79 */ | |
80 public this(Object element) { | |
81 Assert.isNotNull(element); | |
82 elements = [ element ]; | |
83 } | |
84 | |
85 /** | |
86 * Creates a structured selection from the given <code>List</code>. | |
87 * @param elements list of selected elements | |
88 */ | |
89 public this(List elements) { | |
90 this(elements, null); | |
91 } | |
92 | |
93 /** | |
94 * Creates a structured selection from the given <code>List</code> and | |
95 * element comparer. If an element comparer is provided, it will be used to | |
96 * determine equality between structured selection objects provided that | |
97 * they both are based on the same (identical) comparer. See bug | |
98 * | |
99 * @param elements | |
100 * list of selected elements | |
101 * @param comparer | |
102 * the comparer, or null | |
103 * @since 3.4 | |
104 */ | |
105 public this(List elements, IElementComparer comparer) { | |
106 Assert.isNotNull(cast(Object)elements); | |
107 this.elements = elements.toArray(); | |
108 this.comparer = comparer; | |
109 } | |
110 | |
111 /** | |
112 * Returns whether this structured selection is equal to the given object. | |
113 * Two structured selections are equal if they contain the same elements | |
114 * in the same order. | |
115 * | |
116 * @param o the other object | |
117 * @return <code>true</code> if they are equal, and <code>false</code> otherwise | |
118 */ | |
119 public override int opEquals(Object o) { | |
120 if (this is o) { | |
121 return true; | |
122 } | |
123 //null and other classes | |
124 if (!(cast(StructuredSelection)o )) { | |
125 return false; | |
126 } | |
127 StructuredSelection s2 = cast(StructuredSelection) o; | |
128 | |
129 // either or both empty? | |
130 if (isEmpty()) { | |
131 return s2.isEmpty(); | |
132 } | |
133 if (s2.isEmpty()) { | |
134 return false; | |
135 } | |
136 | |
137 bool useComparer = comparer !is null && comparer is s2.comparer; | |
138 | |
139 //size | |
140 int myLen = elements.length; | |
141 if (myLen !is s2.elements.length) { | |
142 return false; | |
143 } | |
144 //element comparison | |
145 for (int i = 0; i < myLen; i++) { | |
146 if (useComparer) { | |
147 if (!comparer.opEquals(elements[i], s2.elements[i])) { | |
148 return false; | |
149 } | |
150 } else { | |
151 if (!elements[i].opEquals(s2.elements[i])) { | |
152 return false; | |
153 } | |
154 } | |
155 } | |
156 return true; | |
157 } | |
158 | |
159 /* (non-Javadoc) | |
160 * Method declared in IStructuredSelection. | |
161 */ | |
162 public Object getFirstElement() { | |
163 return isEmpty() ? null : elements[0]; | |
164 } | |
165 | |
166 /* (non-Javadoc) | |
167 * Method declared in ISelection. | |
168 */ | |
169 public bool isEmpty() { | |
170 return elements is null || elements.length is 0; | |
171 } | |
172 | |
173 /* (non-Javadoc) | |
174 * Method declared in IStructuredSelection. | |
175 */ | |
176 public Iterator iterator() { | |
177 return Arrays.asList(elements is null ? null : elements) | |
178 .iterator(); | |
179 } | |
180 | |
181 /* (non-Javadoc) | |
182 * Method declared in IStructuredSelection. | |
183 */ | |
184 public int size() { | |
185 return elements is null ? 0 : elements.length; | |
186 } | |
187 | |
188 /* (non-Javadoc) | |
189 * Method declared in IStructuredSelection. | |
190 */ | |
191 public Object[] toArray() { | |
192 return elements is null ? new Object[0] : elements.dup; | |
193 } | |
194 | |
195 /* (non-Javadoc) | |
196 * Method declared in IStructuredSelection. | |
197 */ | |
198 public List toList() { | |
199 return Arrays.asList(elements is null ? null : elements); | |
200 } | |
201 | |
202 /** | |
203 * Internal method which returns a string representation of this | |
204 * selection suitable for debug purposes only. | |
205 * | |
206 * @return debug string | |
207 */ | |
208 public override String toString() { | |
209 return isEmpty() ? JFaceResources.getString("<empty_selection>") : (cast(Object)toList()).toString(); //$NON-NLS-1$ | |
210 } | |
211 } |