comparison dwtx/jface/viewers/StructuredSelection.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children ea8ff534f622
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
1 /*******************************************************************************
2 * Copyright (c) 2000, 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.StructuredSelection;
14
15 import dwtx.jface.viewers.IStructuredSelection;
16
17 // import java.util.Arrays;
18 // import java.util.Iterator;
19 import tango.util.collection.ArraySeq;
20 import tango.util.collection.model.Seq;
21 import tango.util.collection.model.SeqView;
22 import tango.util.collection.model.Iterator;
23
24 import dwtx.core.runtime.Assert;
25 import dwtx.jface.resource.JFaceResources;
26
27 import dwt.dwthelper.utils;
28
29 /**
30 * A concrete implementation of the <code>IStructuredSelection</code> interface,
31 * suitable for instantiating.
32 * <p>
33 * This class is not intended to be subclassed.
34 * </p>
35 */
36 public class StructuredSelection : IStructuredSelection {
37
38 /**
39 * The element that make up this structured selection.
40 */
41 private Object[] elements;
42
43 /**
44 * The canonical empty selection. This selection should be used instead of
45 * <code>null</code>.
46 */
47 public static const StructuredSelection EMPTY;
48 static this(){
49 EMPTY = new StructuredSelection();
50 }
51 /**
52 * Creates a new empty selection.
53 * See also the static field <code>EMPTY</code> which contains an empty selection singleton.
54 *
55 * @see #EMPTY
56 */
57 public this() {
58 }
59
60 /**
61 * Creates a structured selection from the given elements.
62 *
63 * @param elements an array of elements
64 */
65 public this(Object[] elements) {
66 this.elements = new Object[elements.length];
67 System.arraycopy(elements, 0, this.elements, 0, elements.length);
68 }
69
70 /**
71 * Creates a structured selection containing a single object.
72 * The object must not be <code>null</code>.
73 *
74 * @param element the element
75 */
76 public this(Object element) {
77 Assert.isNotNull(element);
78 elements = [ element ];
79 }
80
81 /**
82 * Creates a structured selection from the given <code>List</code>.
83 * @param elements list of selected elements
84 */
85 public this(SeqView!(Object) elements) {
86 Assert.isNotNull(cast(Object)elements);
87 this.elements = elements.toArray();
88 }
89
90 /**
91 * Returns whether this structured selection is equal to the given object.
92 * Two structured selections are equal if they contain the same elements
93 * in the same order.
94 *
95 * @param o the other object
96 * @return <code>true</code> if they are equal, and <code>false</code> otherwise
97 */
98 public bool equals(Object o) {
99 if (this is o) {
100 return true;
101 }
102 //null and other classes
103 if (!(cast(StructuredSelection)o )) {
104 return false;
105 }
106 StructuredSelection s2 = cast(StructuredSelection) o;
107
108 // either or both empty?
109 if (isEmpty()) {
110 return s2.isEmpty();
111 }
112 if (s2.isEmpty()) {
113 return false;
114 }
115
116 //size
117 int myLen = elements.length;
118 if (myLen !is s2.elements.length) {
119 return false;
120 }
121 //element comparison
122 for (int i = 0; i < myLen; i++) {
123 if (!elements[i].opEquals(s2.elements[i])) {
124 return false;
125 }
126 }
127 return true;
128 }
129
130 /* (non-Javadoc)
131 * Method declared in IStructuredSelection.
132 */
133 public Object getFirstElement() {
134 return isEmpty() ? null : elements[0];
135 }
136
137 /* (non-Javadoc)
138 * Method declared in ISelection.
139 */
140 public bool isEmpty() {
141 return elements is null || elements.length is 0;
142 }
143
144 /* (non-Javadoc)
145 * Method declared in IStructuredSelection.
146 */
147 public Iterator!(Object) iterator() {
148 auto res = new ArraySeq!(Object);
149 res.capacity( elements.length );
150 foreach( o; elements ){
151 res.append( o );
152 }
153 return res.elements;
154 }
155
156 /* (non-Javadoc)
157 * Method declared in IStructuredSelection.
158 */
159 public int size() {
160 return elements is null ? 0 : elements.length;
161 }
162
163 /* (non-Javadoc)
164 * Method declared in IStructuredSelection.
165 */
166 public Object[] toArray() {
167 return elements is null ? new Object[0] : elements.dup;
168 }
169
170 /* (non-Javadoc)
171 * Method declared in IStructuredSelection.
172 */
173 public SeqView!(Object) toList() {
174 auto res = new ArraySeq!(Object);
175 res.capacity( elements.length );
176 foreach( o; elements ){
177 res.append( o );
178 }
179 return res;
180 }
181
182 /**
183 * Internal method which returns a string representation of this
184 * selection suitable for debug purposes only.
185 *
186 * @return debug string
187 */
188 public String toString() {
189 return isEmpty() ? JFaceResources.getString("<empty_selection>") : (cast(Object)toList()).toString(); //$NON-NLS-1$
190 }
191 }