KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > StructuredSelection


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  *******************************************************************************/

11 package org.eclipse.jface.viewers;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.core.runtime.Assert;
19
20 /**
21  * A concrete implementation of the <code>IStructuredSelection</code> interface,
22  * suitable for instantiating.
23  * <p>
24  * This class is not intended to be subclassed.
25  * </p>
26  */

27 public class StructuredSelection implements IStructuredSelection {
28
29     /**
30      * The element that make up this structured selection.
31      */

32     private Object JavaDoc[] elements;
33
34     /**
35      * The canonical empty selection. This selection should be used instead of
36      * <code>null</code>.
37      */

38     public static final StructuredSelection EMPTY = new StructuredSelection();
39
40     /**
41      * Creates a new empty selection.
42      * See also the static field <code>EMPTY</code> which contains an empty selection singleton.
43      *
44      * @see #EMPTY
45      */

46     public StructuredSelection() {
47     }
48
49     /**
50      * Creates a structured selection from the given elements.
51      *
52      * @param elements an array of elements
53      */

54     public StructuredSelection(Object JavaDoc[] elements) {
55         this.elements = new Object JavaDoc[elements.length];
56         System.arraycopy(elements, 0, this.elements, 0, elements.length);
57     }
58
59     /**
60      * Creates a structured selection containing a single object.
61      * The object must not be <code>null</code>.
62      *
63      * @param element the element
64      */

65     public StructuredSelection(Object JavaDoc element) {
66         Assert.isNotNull(element);
67         elements = new Object JavaDoc[] { element };
68     }
69
70     /**
71      * Creates a structured selection from the given <code>List</code>.
72      * @param elements list of selected elements
73      */

74     public StructuredSelection(List JavaDoc elements) {
75         Assert.isNotNull(elements);
76         this.elements = elements.toArray();
77     }
78
79     /**
80      * Returns whether this structured selection is equal to the given object.
81      * Two structured selections are equal if they contain the same elements
82      * in the same order.
83      *
84      * @param o the other object
85      * @return <code>true</code> if they are equal, and <code>false</code> otherwise
86      */

87     public boolean equals(Object JavaDoc o) {
88         if (this == o) {
89             return true;
90         }
91         //null and other classes
92
if (!(o instanceof StructuredSelection)) {
93             return false;
94         }
95         StructuredSelection s2 = (StructuredSelection) o;
96
97         // either or both empty?
98
if (isEmpty()) {
99             return s2.isEmpty();
100         }
101         if (s2.isEmpty()) {
102             return false;
103         }
104
105         //size
106
int myLen = elements.length;
107         if (myLen != s2.elements.length) {
108             return false;
109         }
110         //element comparison
111
for (int i = 0; i < myLen; i++) {
112             if (!elements[i].equals(s2.elements[i])) {
113                 return false;
114             }
115         }
116         return true;
117     }
118
119     /* (non-Javadoc)
120      * Method declared in IStructuredSelection.
121      */

122     public Object JavaDoc getFirstElement() {
123         return isEmpty() ? null : elements[0];
124     }
125
126     /* (non-Javadoc)
127      * Method declared in ISelection.
128      */

129     public boolean isEmpty() {
130         return elements == null || elements.length == 0;
131     }
132
133     /* (non-Javadoc)
134      * Method declared in IStructuredSelection.
135      */

136     public Iterator JavaDoc iterator() {
137         return Arrays.asList(elements == null ? new Object JavaDoc[0] : elements)
138                 .iterator();
139     }
140
141     /* (non-Javadoc)
142      * Method declared in IStructuredSelection.
143      */

144     public int size() {
145         return elements == null ? 0 : elements.length;
146     }
147
148     /* (non-Javadoc)
149      * Method declared in IStructuredSelection.
150      */

151     public Object JavaDoc[] toArray() {
152         return elements == null ? new Object JavaDoc[0] : (Object JavaDoc[]) elements.clone();
153     }
154
155     /* (non-Javadoc)
156      * Method declared in IStructuredSelection.
157      */

158     public List JavaDoc toList() {
159         return Arrays.asList(elements == null ? new Object JavaDoc[0] : elements);
160     }
161
162     /**
163      * Internal method which returns a string representation of this
164      * selection suitable for debug purposes only.
165      *
166      * @return debug string
167      */

168     public String JavaDoc toString() {
169         return isEmpty() ? JFaceResources.getString("<empty_selection>") : toList().toString(); //$NON-NLS-1$
170
}
171 }
172
Popular Tags