package org.sparx.javaexample.examples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Attribute;
import org.sparx.Collection;
import org.sparx.Element;
import org.sparx.Method;
import org.sparx.Repository;
import org.sparx.javaexample.ExampleHelpers;
import org.sparx.javaexample.ExampleUI;


public class ManageAttributesMethodsExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Add and Manage Attributes/Methods";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "Example illustrating how to add and delete Attributes and Methods.<br/><br/><b>NOTE:</b> Requires an element id for the Element to work on, so try running <code>Dump Model Diagrams</code> or <code>Dump Model</code> first and note some suitable element IDs down.<br/><br/><b>Related APIs</b><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?element2.htm'>Element API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?attribute.htm'>Attribute API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?method.htm'>Method API</a></li></ul>";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		// Prompt the user for an Element ID to work on
		short elementID = ExampleHelpers.promptForShortID( 
				"Enter an Element ID to work on", uiFrame);
		
		if ( elementID < 0 )
		{
			outputList.addElement( "Add and Manage Attributes/Methods Example cancelled by user" );
			return;
		}
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// Get the element to work on
				Element theElement = rep.GetElementByID( elementID );
				outputList.addElement( "Working on element '" + theElement.GetName() 
						+ "' (Type=" + theElement.GetType() + " ID=" 
						+ theElement.GetElementID() + ")" );
			
				// ==================================================
				// MANAGE ATTRIBUTES
				// ==================================================
				// Add an attribute constraint
				Collection<Attribute> attributes = theElement.GetAttributes();
				String attributeName = "m_newAttribute";
				Attribute newAttribute  
						= attributes.AddNew( attributeName, "string" );
				newAttribute.Update();
				attributes.Refresh();
				
				outputList.addElement( "Added attribute: " + newAttribute.GetName() );
				
				newAttribute = null;
								
				// Search the attribute collection for the added attribute and delete it
				for ( short i = 0 ; i < attributes.GetCount() ; i++ )
				{
					Attribute currentAttribute = attributes.GetAt( i );
					String currentAttributeName = currentAttribute.GetName();
					outputList.addElement( "Attribute: " + currentAttributeName );
					
					// Delete the attribute we just added
					if ( currentAttributeName.equals(attributeName) )
					{
						attributes.DeleteAt( i, false );
						outputList.addElement( "Deleted Attribute: " + currentAttributeName);
					}
				}
				
				attributes = null;
				
				// ==================================================
				// MANAGE METHODS
				// ==================================================
				// Add a method
				Collection<Method> methods = theElement.GetMethods();
				String methodName = "NewMethod";
				Method newMethod 
						= methods.AddNew( methodName, "int" );
				newMethod.Update();
				methods.Refresh();
				
				outputList.addElement( "Added method: " + newMethod.GetName() );
				
				newMethod = null;
								
				// Search the method collection for the added method and delete it
				for ( short i = 0 ; i < methods.GetCount() ; i++ )
				{
					Method currentMethod = methods.GetAt( i );
					String currentMethodName = currentMethod.GetName();
					outputList.addElement( "Method: " + currentMethodName );
					
					// Delete the constraint we just added
					if ( currentMethodName.equals(methodName) )
					{
						methods.DeleteAt( i, false );
						outputList.addElement( "Deleted Method: " + currentMethodName);
					}
				}
				
				methods = null;
				
				outputList.addElement( "Done!" );
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
