/*
 * Licensed Materials - Use restricted, please refer to the "Samples Gallery" terms
 * and conditions in the IBM International Program License Agreement.
 *
 * © Copyright IBM Corporation 2003, 2004. All Rights Reserved. 
 */
${package_declaration}

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EObject;

import com.ibm.xtools.modeler.ui.UMLModeler;
import com.ibm.xtools.pluglets.Pluglet;

public class ${type_name} extends Pluglet {

	/**
	 * Walk the selected objects and log them to the console
	 */
	public void plugletmain(String[] args) {

        /* Perform remaining work within a Runnable */
        try {
            UMLModeler.getEditingDomain().runExclusive(new Runnable() {

                public void run() {
                    // Get selection
                    final List elements = UMLModeler.getUMLUIHelper().getSelectedElements();

                    if (elements.size() == 0) {
						out.println("Cannot perform enumeration on current selection.\nPlease select a UML Element from the Project Explorer or\nselect a Notation element from a diagram."); //$NON-NLS-1$
                    }
                    
                    // Log each elements
                    for (Iterator iter = elements.iterator(); iter.hasNext();) {
                        logObject(iter.next(), ""); //$NON-NLS-1$
                    }
                }

            });
        } catch (InterruptedException e) {
			out.println("The operation was interrupted"); //$NON-NLS-1$
        }
	}


	/**
	 * Logs the specified object's toString() and its content
	 * @param object The object to log 
	 * @param indent The string to prefix to the object's toString()
	 */
	private void logObject(Object object, String indent) {

		// Log object
		if (null != object) {
			out.println(indent + object.toString());
		} else {
			out.println(indent + "<null>"); //$NON-NLS-1$
		}
		
		// Log contents
		if (object instanceof EObject) {
			Iterator contents = ((EObject)object).eContents().iterator();
			while (contents.hasNext()) {
				logObject(contents.next(), indent + "\t"); //$NON-NLS-1$
			}
		}
	}

}
