01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: * Darrell Meyer <darrell@mygwt.net> - derived implementation
11: *******************************************************************************/package net.mygwt.ui.client.viewer;
12:
13: import java.util.ArrayList;
14: import java.util.Iterator;
15: import java.util.List;
16:
17: /**
18: * A concrete implementation of the Selection interface, suitable for
19: * instantiating.
20: *
21: * <p>
22: * This code is based on JFace API from the Eclipse Project.
23: * </p>
24: */
25: public class DefaultSelection implements ISelection {
26:
27: /**
28: * The canonical empty selection. This selection should be used instead of
29: * <code>null</code>.
30: */
31: public static final DefaultSelection EMPTY = new DefaultSelection();
32:
33: /**
34: * The element that make up this structured selection.
35: */
36: private List elements;
37:
38: /**
39: * Creates a new empty selection. See also the static field <code>EMPTY</code>
40: * which contains an empty selection singleton.
41: *
42: * @see #EMPTY
43: */
44: public DefaultSelection() {
45: elements = new ArrayList();
46: }
47:
48: /**
49: * Creates a structured selection containing a single object. The object must
50: * not be <code>null</code>.
51: *
52: * @param element the element
53: */
54: public DefaultSelection(Object element) {
55: elements = new ArrayList();
56: elements.add(element);
57: }
58:
59: /**
60: * Creates a structured selection from the list of objects. The list must not
61: * be <code>null</code>.
62: *
63: * @param elements the elements
64: */
65: public DefaultSelection(List elements) {
66: this .elements = elements;
67: }
68:
69: public Object[] toArray() {
70: return elements.toArray();
71: }
72:
73: public List toList() {
74: return elements;
75: }
76:
77: public Object getFirstElement() {
78: return isEmpty() ? null : elements.get(0);
79: }
80:
81: public boolean isEmpty() {
82: return elements == null || elements.size() == 0;
83: }
84:
85: public Iterator iterator() {
86: return elements.iterator();
87: }
88:
89: public int size() {
90: return elements.size();
91: }
92:
93: }
|