Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/StructuredSelection.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | 5df4896124c7 |
children | 04b47443bb01 |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
10 | 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.StructuredSelection; | |
14 | |
15 import dwtx.jface.viewers.IStructuredSelection; | |
71 | 16 import dwtx.jface.viewers.IElementComparer; |
10 | 17 |
18 import tango.util.collection.ArraySeq; | |
19 import tango.util.collection.model.Seq; | |
20 import tango.util.collection.model.SeqView; | |
21 import tango.util.collection.model.Iterator; | |
22 | |
23 import dwtx.core.runtime.Assert; | |
24 import dwtx.jface.resource.JFaceResources; | |
25 | |
26 import dwt.dwthelper.utils; | |
27 | |
28 /** | |
29 * A concrete implementation of the <code>IStructuredSelection</code> interface, | |
30 * suitable for instantiating. | |
31 * <p> | |
32 * This class is not intended to be subclassed. | |
33 * </p> | |
90 | 34 * @noextend This class is not intended to be subclassed by clients. |
10 | 35 */ |
36 public class StructuredSelection : IStructuredSelection { | |
37 | |
38 /** | |
39 * The element that make up this structured selection. | |
40 */ | |
41 private Object[] elements; | |
42 | |
43 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
44 * The element comparer, or <code>null</code> |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
45 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
46 private IElementComparer comparer; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
47 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
48 /** |
10 | 49 * The canonical empty selection. This selection should be used instead of |
50 * <code>null</code>. | |
51 */ | |
52 public static const StructuredSelection EMPTY; | |
53 static this(){ | |
54 EMPTY = new StructuredSelection(); | |
55 } | |
56 /** | |
57 * Creates a new empty selection. | |
58 * See also the static field <code>EMPTY</code> which contains an empty selection singleton. | |
59 * | |
60 * @see #EMPTY | |
61 */ | |
62 public this() { | |
63 } | |
64 | |
65 /** | |
66 * Creates a structured selection from the given elements. | |
67 * | |
68 * @param elements an array of elements | |
69 */ | |
70 public this(Object[] elements) { | |
71 this.elements = new Object[elements.length]; | |
72 System.arraycopy(elements, 0, this.elements, 0, elements.length); | |
73 } | |
74 | |
75 /** | |
76 * Creates a structured selection containing a single object. | |
77 * The object must not be <code>null</code>. | |
78 * | |
79 * @param element the element | |
80 */ | |
81 public this(Object element) { | |
82 Assert.isNotNull(element); | |
83 elements = [ element ]; | |
84 } | |
85 | |
86 /** | |
87 * Creates a structured selection from the given <code>List</code>. | |
88 * @param elements list of selected elements | |
89 */ | |
90 public this(SeqView!(Object) elements) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
91 this(elements, null); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
92 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
93 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
94 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
95 * Creates a structured selection from the given <code>List</code> and |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
96 * element comparer. If an element comparer is provided, it will be used to |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
97 * determine equality between structured selection objects provided that |
71 | 98 * they both are based on the same (identical) comparer. See bug |
99 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
100 * @param elements |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
101 * list of selected elements |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
102 * @param comparer |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
103 * the comparer, or null |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
104 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
105 */ |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
106 public this(SeqView!(Object) elements, IElementComparer comparer) { |
10 | 107 this.elements = elements.toArray(); |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
108 this.comparer = comparer; |
10 | 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 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
119 public override int opEquals(Object o) { |
10 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
137 bool useComparer = comparer !is null && comparer is s2.comparer; |
71 | 138 |
10 | 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++) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
146 if (useComparer) { |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
147 if (!comparer.opEquals(elements[i], s2.elements[i])) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
148 return false; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
149 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
150 } else { |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
151 if (!elements[i].opEquals(s2.elements[i])) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
152 return false; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
153 } |
10 | 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!(Object) iterator() { | |
177 auto res = new ArraySeq!(Object); | |
178 res.capacity( elements.length ); | |
179 foreach( o; elements ){ | |
180 res.append( o ); | |
181 } | |
182 return res.elements; | |
183 } | |
184 | |
185 /* (non-Javadoc) | |
186 * Method declared in IStructuredSelection. | |
187 */ | |
188 public int size() { | |
189 return elements is null ? 0 : elements.length; | |
190 } | |
191 | |
192 /* (non-Javadoc) | |
193 * Method declared in IStructuredSelection. | |
194 */ | |
195 public Object[] toArray() { | |
196 return elements is null ? new Object[0] : elements.dup; | |
197 } | |
198 | |
199 /* (non-Javadoc) | |
200 * Method declared in IStructuredSelection. | |
201 */ | |
202 public SeqView!(Object) toList() { | |
203 auto res = new ArraySeq!(Object); | |
204 res.capacity( elements.length ); | |
205 foreach( o; elements ){ | |
206 res.append( o ); | |
207 } | |
208 return res; | |
209 } | |
210 | |
211 /** | |
212 * Internal method which returns a string representation of this | |
213 * selection suitable for debug purposes only. | |
214 * | |
215 * @return debug string | |
216 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
217 public override String toString() { |
10 | 218 return isEmpty() ? JFaceResources.getString("<empty_selection>") : (cast(Object)toList()).toString(); //$NON-NLS-1$ |
219 } | |
220 } |