001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.viewers;
011:
012: import java.util.Arrays;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.jface.resource.JFaceResources;
017: import org.eclipse.core.runtime.Assert;
018:
019: /**
020: * A concrete implementation of the <code>IStructuredSelection</code> interface,
021: * suitable for instantiating.
022: * <p>
023: * This class is not intended to be subclassed.
024: * </p>
025: */
026: public class StructuredSelection implements IStructuredSelection {
027:
028: /**
029: * The element that make up this structured selection.
030: */
031: private Object[] elements;
032:
033: /**
034: * The canonical empty selection. This selection should be used instead of
035: * <code>null</code>.
036: */
037: public static final StructuredSelection EMPTY = new StructuredSelection();
038:
039: /**
040: * Creates a new empty selection.
041: * See also the static field <code>EMPTY</code> which contains an empty selection singleton.
042: *
043: * @see #EMPTY
044: */
045: public StructuredSelection() {
046: }
047:
048: /**
049: * Creates a structured selection from the given elements.
050: *
051: * @param elements an array of elements
052: */
053: public StructuredSelection(Object[] elements) {
054: this .elements = new Object[elements.length];
055: System
056: .arraycopy(elements, 0, this .elements, 0,
057: elements.length);
058: }
059:
060: /**
061: * Creates a structured selection containing a single object.
062: * The object must not be <code>null</code>.
063: *
064: * @param element the element
065: */
066: public StructuredSelection(Object element) {
067: Assert.isNotNull(element);
068: elements = new Object[] { element };
069: }
070:
071: /**
072: * Creates a structured selection from the given <code>List</code>.
073: * @param elements list of selected elements
074: */
075: public StructuredSelection(List elements) {
076: Assert.isNotNull(elements);
077: this .elements = elements.toArray();
078: }
079:
080: /**
081: * Returns whether this structured selection is equal to the given object.
082: * Two structured selections are equal if they contain the same elements
083: * in the same order.
084: *
085: * @param o the other object
086: * @return <code>true</code> if they are equal, and <code>false</code> otherwise
087: */
088: public boolean equals(Object o) {
089: if (this == o) {
090: return true;
091: }
092: //null and other classes
093: if (!(o instanceof StructuredSelection)) {
094: return false;
095: }
096: StructuredSelection s2 = (StructuredSelection) o;
097:
098: // either or both empty?
099: if (isEmpty()) {
100: return s2.isEmpty();
101: }
102: if (s2.isEmpty()) {
103: return false;
104: }
105:
106: //size
107: int myLen = elements.length;
108: if (myLen != s2.elements.length) {
109: return false;
110: }
111: //element comparison
112: for (int i = 0; i < myLen; i++) {
113: if (!elements[i].equals(s2.elements[i])) {
114: return false;
115: }
116: }
117: return true;
118: }
119:
120: /* (non-Javadoc)
121: * Method declared in IStructuredSelection.
122: */
123: public Object getFirstElement() {
124: return isEmpty() ? null : elements[0];
125: }
126:
127: /* (non-Javadoc)
128: * Method declared in ISelection.
129: */
130: public boolean isEmpty() {
131: return elements == null || elements.length == 0;
132: }
133:
134: /* (non-Javadoc)
135: * Method declared in IStructuredSelection.
136: */
137: public Iterator iterator() {
138: return Arrays.asList(
139: elements == null ? new Object[0] : elements).iterator();
140: }
141:
142: /* (non-Javadoc)
143: * Method declared in IStructuredSelection.
144: */
145: public int size() {
146: return elements == null ? 0 : elements.length;
147: }
148:
149: /* (non-Javadoc)
150: * Method declared in IStructuredSelection.
151: */
152: public Object[] toArray() {
153: return elements == null ? new Object[0] : (Object[]) elements
154: .clone();
155: }
156:
157: /* (non-Javadoc)
158: * Method declared in IStructuredSelection.
159: */
160: public List toList() {
161: return Arrays.asList(elements == null ? new Object[0]
162: : elements);
163: }
164:
165: /**
166: * Internal method which returns a string representation of this
167: * selection suitable for debug purposes only.
168: *
169: * @return debug string
170: */
171: public String toString() {
172: return isEmpty() ? JFaceResources
173: .getString("<empty_selection>") : toList().toString(); //$NON-NLS-1$
174: }
175: }
|