/* * 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; /** * \package ml.backend.selection * * This is the core of the selection framework. * * This package is part of the core program. */ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import ml.core.exceptions.Exception; import ml.core.helper.NamePool; /** * This class manages the collection of named selections within Moonlight|3D. * At any time there may be more than one selection in existence within the program. * To distinguish them, they are carrying unique names. This class serves as a central * collection of all selection sets. * * @author gregor */ public class Manager { private static ArrayList selectionTypes; static { selectionTypes=new ArrayList(); } /** * Register a selection type class * * @param type a selection type */ public static void registerSelectionType(SelectionType type) { selectionTypes.add(type); } /** * Unregister a selection type * * @param typeName name of the selection type */ public static void unregisterSelectionType(String typeName) { for(int i=0;i selections; private ArrayList rootPathElements; private HashMap rootPathElementMap; private NamePool rootPathElementNames; /** * Create a new Manager with an empty set of selections * */ public Manager() { selections=new ArrayList(); rootPathElements=new ArrayList(); rootPathElementMap=new HashMap(); rootPathElementNames=new NamePool(); } /** * Return a list of currently registered selection types * * @return list of currently registered selection types */ public List getSelectionTypes() { return (ArrayList) selectionTypes.clone(); } public void registerRootPathElement(RootPathElement element) throws Exception { if(!rootPathElementNames.isNameUnique(element.getName())) { throw new ml.core.exceptions.Exception("tried to register RootPathElement without unique name: " + element.getName()); } rootPathElementNames.add(element); rootPathElementMap.put(element.getName(), element); rootPathElements.add(element); } public void unregisterRootPathElement(String elementName) { RootPathElement element=rootPathElementMap.get(elementName); if(element==null) { return; } rootPathElements.remove(element); rootPathElementMap.remove(elementName); rootPathElementNames.remove(element); } public RootPathElement getRootPathElement(String elementName) { return rootPathElementMap.get(elementName); } /** * Add a selection with a given name. This function generates the selection * provided that the name is unique and returns it to the caller. * * @param selectionName the desired name of the selection * @return the new selection * @throws Exception if the name of the selection is not unique */ public Selection addSelection(String selectionName) throws Exception { try { // note that we want an error to get thrown here! getSelection(selectionName); } catch(Exception e) { Selection newSelection=new Selection(this,selectionName); selections.add(newSelection); return newSelection; } throw new Exception("A selection with the given name already exists"); } /** * Return the selection with the given name, if it exists * * @param selectionName the name of the selection to be returned * @return the requested selection * @throws Exception if the selection doesn't exist */ public Selection getSelection(String selectionName) throws Exception { for(int i=0;i