1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.common;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Arrays;
\r
15 import java.util.Iterator;
\r
16 import java.util.List;
\r
18 import org.eclipse.jface.viewers.IStructuredSelection;
\r
19 import org.simantics.db.Resource;
\r
23 * StructuredSelection that contains ResourceSelections
\r
25 * @author Marko Luukkainen
\r
26 * @author Tuukka Lehtonen
\r
28 public class StructuredResourceSelection implements IStructuredSelection {
\r
30 public static final StructuredResourceSelection EMPTY = new StructuredResourceSelection() {
\r
31 // Create an empty fixed size List to ensure the list is not modified.
\r
32 private List<Resource> empty = Arrays.asList();
\r
34 public void add(Resource rs) {
\r
35 throw new UnsupportedOperationException("BUG: attempted to modify StructuredResourceSelection.EMPTY");
\r
37 public List<Resource> getSelectionList() {
\r
42 private List<Resource> selections;
\r
45 * Creates a new selection that doesn't contain any items
\r
47 public StructuredResourceSelection() {
\r
50 public StructuredResourceSelection(Resource rs) {
\r
51 getSelectionList().add(rs);
\r
54 public StructuredResourceSelection(Resource... rss) {
\r
55 List<Resource> s = getSelectionList();
\r
56 for (Resource rs : rss)
\r
60 public void add(Resource rs) {
\r
61 getSelectionList().add(rs);
\r
64 public List<Resource> getSelectionList() {
\r
65 if (selections == null) {
\r
66 selections = new ArrayList<Resource>();
\r
74 * @see org.eclipse.jface.viewers.ISelection#isEmpty()
\r
76 public boolean isEmpty() {
\r
77 return selections == null || selections.isEmpty();
\r
83 * @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()
\r
85 public Object getFirstElement() {
\r
87 return selections.get(0);
\r
94 * @see org.eclipse.jface.viewers.IStructuredSelection#iterator()
\r
96 public Iterator<Resource> iterator() {
\r
97 return getSelectionList().iterator();
\r
103 * @see org.eclipse.jface.viewers.IStructuredSelection#size()
\r
105 public int size() {
\r
106 return selections == null ? 0 : selections.size();
\r
112 * @see org.eclipse.jface.viewers.IStructuredSelection#toArray()
\r
114 public Object[] toArray() {
\r
115 return selections == null ? new Object[0] : selections.toArray();
\r
121 * @see org.eclipse.jface.viewers.IStructuredSelection#toList()
\r
123 public List<Resource> toList() {
\r
124 return selections == null ? Arrays.asList(new Resource[0]) : selections;
\r
128 * Returns whether this structured selection is equal to the given object.
\r
129 * Two structured selections are equal iff they contain the same elements
\r
130 * in the same order.
\r
132 * @param o the other object
\r
133 * @return <code>true</code> if they are equal, and <code>false</code> otherwise
\r
135 public boolean equals(Object o) {
\r
139 // null and other classes
\r
140 if (!(o instanceof StructuredResourceSelection)) {
\r
143 StructuredResourceSelection other = (StructuredResourceSelection) o;
\r
145 // either or both empty?
\r
147 return other.isEmpty();
\r
149 if (other.isEmpty()) {
\r
154 if (size() != other.size())
\r
157 // element comparison
\r
158 Iterator<Resource> it = iterator();
\r
159 Iterator<Resource> otherIt = other.iterator();
\r
160 while (it.hasNext()) {
\r
161 if (!it.next().equals(otherIt.next()))
\r
169 public String toString() {
\r
170 return Arrays.toString(getSelectionList().toArray());
\r