Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/StructuredSelection.d @ 71:4878bef4a38e
Some fixing
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 04:03:58 +0200 |
parents | 46a6e0e6ccd4 |
children | 5df4896124c7 |
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> | |
34 */ | |
35 public class StructuredSelection : IStructuredSelection { | |
36 | |
37 /** | |
38 * The element that make up this structured selection. | |
39 */ | |
40 private Object[] elements; | |
41 | |
42 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
43 * 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
|
44 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
45 private IElementComparer comparer; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
46 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
47 /** |
10 | 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(SeqView!(Object) elements) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
90 this(elements, null); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
91 } |
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 * 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
|
95 * 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
|
96 * determine equality between structured selection objects provided that |
71 | 97 * they both are based on the same (identical) comparer. See bug |
98 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
99 * @param elements |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
100 * list of selected elements |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
101 * @param comparer |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
102 * the comparer, or null |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
103 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
104 */ |
71 | 105 public this(Seq!(Object) elements, IElementComparer comparer) { |
10 | 106 this.elements = elements.toArray(); |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
107 this.comparer = comparer; |
10 | 108 } |
109 | |
110 /** | |
111 * Returns whether this structured selection is equal to the given object. | |
112 * Two structured selections are equal if they contain the same elements | |
113 * in the same order. | |
114 * | |
115 * @param o the other object | |
116 * @return <code>true</code> if they are equal, and <code>false</code> otherwise | |
117 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
118 public override int opEquals(Object o) { |
10 | 119 if (this is o) { |
120 return true; | |
121 } | |
122 //null and other classes | |
123 if (!(cast(StructuredSelection)o )) { | |
124 return false; | |
125 } | |
126 StructuredSelection s2 = cast(StructuredSelection) o; | |
127 | |
128 // either or both empty? | |
129 if (isEmpty()) { | |
130 return s2.isEmpty(); | |
131 } | |
132 if (s2.isEmpty()) { | |
133 return false; | |
134 } | |
135 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
136 bool useComparer = comparer !is null && comparer is s2.comparer; |
71 | 137 |
10 | 138 //size |
139 int myLen = elements.length; | |
140 if (myLen !is s2.elements.length) { | |
141 return false; | |
142 } | |
143 //element comparison | |
144 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
|
145 if (useComparer) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
146 if (!comparer.equals(elements[i], s2.elements[i])) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
147 return false; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
148 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
149 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
150 if (!elements[i].equals(s2.elements[i])) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
151 return false; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
152 } |
10 | 153 } |
154 } | |
155 return true; | |
156 } | |
157 | |
158 /* (non-Javadoc) | |
159 * Method declared in IStructuredSelection. | |
160 */ | |
161 public Object getFirstElement() { | |
162 return isEmpty() ? null : elements[0]; | |
163 } | |
164 | |
165 /* (non-Javadoc) | |
166 * Method declared in ISelection. | |
167 */ | |
168 public bool isEmpty() { | |
169 return elements is null || elements.length is 0; | |
170 } | |
171 | |
172 /* (non-Javadoc) | |
173 * Method declared in IStructuredSelection. | |
174 */ | |
175 public Iterator!(Object) iterator() { | |
176 auto res = new ArraySeq!(Object); | |
177 res.capacity( elements.length ); | |
178 foreach( o; elements ){ | |
179 res.append( o ); | |
180 } | |
181 return res.elements; | |
182 } | |
183 | |
184 /* (non-Javadoc) | |
185 * Method declared in IStructuredSelection. | |
186 */ | |
187 public int size() { | |
188 return elements is null ? 0 : elements.length; | |
189 } | |
190 | |
191 /* (non-Javadoc) | |
192 * Method declared in IStructuredSelection. | |
193 */ | |
194 public Object[] toArray() { | |
195 return elements is null ? new Object[0] : elements.dup; | |
196 } | |
197 | |
198 /* (non-Javadoc) | |
199 * Method declared in IStructuredSelection. | |
200 */ | |
201 public SeqView!(Object) toList() { | |
202 auto res = new ArraySeq!(Object); | |
203 res.capacity( elements.length ); | |
204 foreach( o; elements ){ | |
205 res.append( o ); | |
206 } | |
207 return res; | |
208 } | |
209 | |
210 /** | |
211 * Internal method which returns a string representation of this | |
212 * selection suitable for debug purposes only. | |
213 * | |
214 * @return debug string | |
215 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
216 public override String toString() { |
10 | 217 return isEmpty() ? JFaceResources.getString("<empty_selection>") : (cast(Object)toList()).toString(); //$NON-NLS-1$ |
218 } | |
219 } |