/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Apr 24, 2005 */ package ml.backend.selection; import java.util.ArrayList; import java.util.List; import ml.core.exceptions.Exception; import ml.core.helper.StringTools; /** * * This class manages a selection on top of the operator graph and * its contents. Selections are named subsets of the operator graph * and its children of a certain type. * * The objects in a selection are refered to by path names. For * example, a path to a scene graph element has the following format:
* * [operator graph node]/[slot name]/[scene graph node](/[scene graph node]...)
* * Theses paths may also refer to elements inside scene graph nodes. * Mesh elements are referred to by paths ending in:
* * [scene graph node]:faces[face id]
* * [scene graph node]:edges[edge id]
* * [scene graph node]:vertices[vertex id]
* * @author gregor */ public class Selection { /** * The name of this selection */ protected String name; /** * The current type of the objects within this collection */ protected SelectionType currentType; /** * The list of names of currently selected objects in this selection set */ protected ArrayList selectedObjects; /** * The name of the active object */ protected String activeObject; /** * The list of registered listeners */ protected ArrayList listeners; protected Manager selectionManager; /** * Creates a new selection with the provided name. The name cannot * be altered after creation. * * @param name Name of the selection */ public Selection(Manager manager, String name) { this.selectionManager=manager; this.name=name; selectedObjects=new ArrayList(); listeners=new ArrayList(); } /** * Register a listener for this selection. The listener will get * notified of any changes from that point onward. * * @param listener the listener to register */ public void registerListener(SelectionListener listener) { listeners.add(listener); } /** * Unregister a listener for this selection. The listener will no * longer be notified about changes to this selection. * * @param listener the listener to unregister */ public void unregisterListener(SelectionListener listener) { listeners.remove(listener); } /** * Clears the selection and resets the type of objects in this selection. * Every selection can only hold items of the same type. * * @param newType The new type for the objects in the selection */ public void clearSelection(SelectionType newType) { this.currentType=newType; selectedObjects.clear(); activeObject=null; for(SelectionListener listener : listeners) { listener.selectionCleared(); } } /** * This helper function returns a reference to the object that * a given path string points to. If the given string is invalid, * an exception is thrown. * * @param object name of object * @return reference of object with given name * @throws ml.core.exceptions.Exception */ public Object getObjectFromSelectionString(String object) throws ml.core.exceptions.Exception { // general plan: walk through path while trying to deduce where it points to String[] splitted=StringTools.split(object,':'); String[] path=StringTools.split(splitted[0],'/'); // String[] splitted=object.split(":"); // String[] path=splitted[0].split("/"); if(path.length==0) { throw new Exception("invalid object name: given string is empty"); } PathElement pathElement=selectionManager.getRootPathElement(path[0]); for(int i=1;i selectionTypes=selectionManager.getSelectionTypes(); for(SelectionType type : selectionTypes) { if(type.isThisType(object)) { return type; } } throw new Exception("Object type is unknown"); } /** * Adds a object with the given name to the Selection. This operation fails * if the given object has an incorrect type or the referencing string is * invalid for some reasons. * * @param object name of the object * @throws Exception */ public void add(String object) throws Exception { // check if named object has correct type if(getObjectType(object)!=currentType) { throw new Exception("unable to add object to selection because of type mismatch"); } boolean alreadySelected=false; for(int i=0;i objects) throws Exception { // check if named object has correct type for(String object : objects) { if(getObjectType(object)!=currentType) { throw new Exception("unable to add object to selection because of type mismatch"); } boolean alreadySelected=false; for(int i=0;i getSelection() { return (ArrayList) selectedObjects.clone(); } /** * Returns a list of object references for the objects in this selection. * * @return list of object references in this selection * @throws ml.core.exceptions.Exception */ public ArrayList getSelectionObjects() throws ml.core.exceptions.Exception { ArrayList objects=new ArrayList(); for(int i=0;i