/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * 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()); } }