X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fcommon%2FStructuredResourceSelection.java;fp=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Fcommon%2FStructuredResourceSelection.java;h=80f92d5fb28c00775bdb5857ab48a4e1d434aed8;hb=10f144a2bb2d7bec98b812b83acecb333fd098ea;hp=0000000000000000000000000000000000000000;hpb=3055b543aa5afc0cca4bb3b341704e7c5103fa6a;p=simantics%2F3d.git diff --git a/org.simantics.g3d/src/org/simantics/proconf/g3d/common/StructuredResourceSelection.java b/org.simantics.g3d/src/org/simantics/proconf/g3d/common/StructuredResourceSelection.java new file mode 100644 index 00000000..80f92d5f --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/proconf/g3d/common/StructuredResourceSelection.java @@ -0,0 +1,173 @@ +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.proconf.g3d.common; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.jface.viewers.IStructuredSelection; +import org.simantics.db.Resource; + + +/** + * StructuredSelection that contains ResourceSelections + * + * @author Marko Luukkainen + * @author Tuukka Lehtonen + */ +public class StructuredResourceSelection implements IStructuredSelection { + + public static final StructuredResourceSelection EMPTY = new StructuredResourceSelection() { + // Create an empty fixed size List to ensure the list is not modified. + private List empty = Arrays.asList(); + + public void add(Resource rs) { + throw new UnsupportedOperationException("BUG: attempted to modify StructuredResourceSelection.EMPTY"); + } + public List getSelectionList() { + return empty; + } + }; + + private List selections; + + /** + * Creates a new selection that doesn't contain any items + */ + public StructuredResourceSelection() { + } + + public StructuredResourceSelection(Resource rs) { + getSelectionList().add(rs); + } + + public StructuredResourceSelection(Resource... rss) { + List s = getSelectionList(); + for (Resource rs : rss) + s.add(rs); + } + + public void add(Resource rs) { + getSelectionList().add(rs); + } + + public List getSelectionList() { + if (selections == null) { + selections = new ArrayList(); + } + return selections; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.ISelection#isEmpty() + */ + public boolean isEmpty() { + return selections == null || selections.isEmpty(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement() + */ + public Object getFirstElement() { + if (!isEmpty()) + return selections.get(0); + return null; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredSelection#iterator() + */ + public Iterator iterator() { + return getSelectionList().iterator(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredSelection#size() + */ + public int size() { + return selections == null ? 0 : selections.size(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredSelection#toArray() + */ + public Object[] toArray() { + return selections == null ? new Object[0] : selections.toArray(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredSelection#toList() + */ + public List toList() { + return selections == null ? Arrays.asList(new Resource[0]) : selections; + } + + /** + * Returns whether this structured selection is equal to the given object. + * Two structured selections are equal iff they contain the same elements + * in the same order. + * + * @param o the other object + * @return true if they are equal, and false otherwise + */ + public boolean equals(Object o) { + if (this == o) { + return true; + } + // null and other classes + if (!(o instanceof StructuredResourceSelection)) { + return false; + } + StructuredResourceSelection other = (StructuredResourceSelection) o; + + // either or both empty? + if (isEmpty()) { + return other.isEmpty(); + } + if (other.isEmpty()) { + return false; + } + + // check size + if (size() != other.size()) + return false; + + // element comparison + Iterator it = iterator(); + Iterator otherIt = other.iterator(); + while (it.hasNext()) { + if (!it.next().equals(otherIt.next())) + return false; + } + + return true; + } + + @Override + public String toString() { + return Arrays.toString(getSelectionList().toArray()); + } + +}