// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//  Description:
//      Invert the selection of dag nodes and dag components.
//
//  Return Value:
//      None.
//
global proc invertSelection(){
	
	// determine if anything is selected
	string $selection[] = `ls -selection`;
	if (`size($selection)` != 0){
		// something is selected
		// now determine if any objects are selected
		string $objects[] = `ls -dagObjects -visible -selection`;

		//note that this gives preference to objects if both objects and
		//components are selected
		//there is no user case where the user wants to invert a mixed
		//selection of objects and components
		if (`size($objects)` != 0){
			select -toggle -allDagObjects -visible;
			//check if selection is in a hierarchy
			string $parents[] = `listRelatives -parent -path $selection`;
			if (`size ($parents)` > 0){
				//object is in a hierarchy

				//deselect all parents
				select -deselect $parents;
				//iterate through parents
				for ($parent in $parents){
					//find the children with the full path
					string $children[] = `listRelatives -children -path $parent`;
					//select the children
					select -add $children;
				}
				//make sure the original objects are not selected
				select -deselect $selection;
			}

		} else {
			// must be a component selected
			
			string $newComponents[], $componentID[];

			for ($component in $selection) {
				if (`tokenize $component "[]" $componentID` > 1) {
					int $newCompSize = `size $newComponents`;
					$newComponents[$newCompSize] = $componentID[0] + "[*] ";
				}
			}
			
			select -replace $newComponents;
			select -deselect $selection;
			
		}
	} else {
		// nothing is selected
		error "No dag objects or components selected to invert.";
	}
	
}