--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.common;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.simantics.db.Resource;\r
+\r
+\r
+/**\r
+ * StructuredSelection that contains ResourceSelections\r
+ * \r
+ * @author Marko Luukkainen\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class StructuredResourceSelection implements IStructuredSelection {\r
+ \r
+ public static final StructuredResourceSelection EMPTY = new StructuredResourceSelection() {\r
+ // Create an empty fixed size List to ensure the list is not modified.\r
+ private List<Resource> empty = Arrays.asList(); \r
+ \r
+ public void add(Resource rs) {\r
+ throw new UnsupportedOperationException("BUG: attempted to modify StructuredResourceSelection.EMPTY");\r
+ }\r
+ public List<Resource> getSelectionList() {\r
+ return empty;\r
+ }\r
+ };\r
+\r
+ private List<Resource> selections;\r
+ \r
+ /**\r
+ * Creates a new selection that doesn't contain any items\r
+ */\r
+ public StructuredResourceSelection() {\r
+ }\r
+\r
+ public StructuredResourceSelection(Resource rs) {\r
+ getSelectionList().add(rs);\r
+ }\r
+\r
+ public StructuredResourceSelection(Resource... rss) {\r
+ List<Resource> s = getSelectionList();\r
+ for (Resource rs : rss)\r
+ s.add(rs);\r
+ }\r
+\r
+ public void add(Resource rs) {\r
+ getSelectionList().add(rs);\r
+ }\r
+\r
+ public List<Resource> getSelectionList() {\r
+ if (selections == null) {\r
+ selections = new ArrayList<Resource>();\r
+ }\r
+ return selections;\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.ISelection#isEmpty()\r
+ */\r
+ public boolean isEmpty() {\r
+ return selections == null || selections.isEmpty();\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.IStructuredSelection#getFirstElement()\r
+ */\r
+ public Object getFirstElement() {\r
+ if (!isEmpty())\r
+ return selections.get(0);\r
+ return null;\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.IStructuredSelection#iterator()\r
+ */\r
+ public Iterator<Resource> iterator() {\r
+ return getSelectionList().iterator();\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.IStructuredSelection#size()\r
+ */\r
+ public int size() {\r
+ return selections == null ? 0 : selections.size();\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.IStructuredSelection#toArray()\r
+ */\r
+ public Object[] toArray() {\r
+ return selections == null ? new Object[0] : selections.toArray();\r
+ }\r
+\r
+ /*\r
+ * (non-Javadoc)\r
+ * \r
+ * @see org.eclipse.jface.viewers.IStructuredSelection#toList()\r
+ */\r
+ public List<Resource> toList() {\r
+ return selections == null ? Arrays.asList(new Resource[0]) : selections;\r
+ }\r
+\r
+ /**\r
+ * Returns whether this structured selection is equal to the given object.\r
+ * Two structured selections are equal iff they contain the same elements\r
+ * in the same order.\r
+ *\r
+ * @param o the other object\r
+ * @return <code>true</code> if they are equal, and <code>false</code> otherwise\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (this == o) {\r
+ return true;\r
+ }\r
+ // null and other classes\r
+ if (!(o instanceof StructuredResourceSelection)) {\r
+ return false;\r
+ }\r
+ StructuredResourceSelection other = (StructuredResourceSelection) o;\r
+\r
+ // either or both empty?\r
+ if (isEmpty()) {\r
+ return other.isEmpty();\r
+ }\r
+ if (other.isEmpty()) {\r
+ return false;\r
+ }\r
+\r
+ // check size\r
+ if (size() != other.size())\r
+ return false;\r
+ \r
+ // element comparison\r
+ Iterator<Resource> it = iterator();\r
+ Iterator<Resource> otherIt = other.iterator();\r
+ while (it.hasNext()) {\r
+ if (!it.next().equals(otherIt.next()))\r
+ return false;\r
+ }\r
+ \r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ return Arrays.toString(getSelectionList().toArray());\r
+ }\r
+ \r
+}\r