package org.sparx.javaexample.examples;

import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Diagram;
import org.sparx.DiagramObject;
import org.sparx.Element;
import org.sparx.Repository;
import org.sparx.javaexample.ExampleUI;

/**
 * Example that iterates over all Views in a Model and outputs Diagrams and their contained
 * diagram objects.
 */
public class NonRecursiveDumpExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Dump Model Diagrams";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "Runs through all the packages and diagrams in a non-recursive style.<br/><br/><b>Related APIs</b><br/><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?repository3.htm'>Repository 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();
		
		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 first Model node
				org.sparx.Package myModel = rep.GetModels().GetAt( (short)0 );
				
				// Iterate through all views in the model
				Collection<org.sparx.Package> views = myModel.GetPackages();
				for ( short i = 0 ; i < views.GetCount() ; i++ )
				{
					org.sparx.Package currentView = views.GetAt( i );
					
					// Add the name of the package to the list
					outputList.addElement( "View: " + currentView.GetName() );
					
					// Iterate through all diagrams in this view
					Collection<Diagram> diagrams = currentView.GetDiagrams();
					for ( short j = 0 ; j < diagrams.GetCount() ; j++ )
					{
						Diagram currentDiagram = diagrams.GetAt( j );
						
						// Add the name of this diagram to the list
						outputList.addElement( "    -> Diagram: " 
												+ currentDiagram.GetName()
												+ " (" + currentDiagram.GetType() + ")");
						
						// Iterate through all objects in this diagram
						Collection<DiagramObject> diagramObjects 
							= currentDiagram.GetDiagramObjects();
						for ( short k = 0 ; k < diagramObjects.GetCount() ; k++ )
						{
							// Get the current diagram object
							DiagramObject currentDO = diagramObjects.GetAt( k );
							int elementID = currentDO.GetElementID();
							
							// Get the element that it represents
							Element currentElement = rep.GetElementByID( elementID );
							
							// Add the element details to the list
							outputList.addElement( "        -> Element: "
													+ currentElement.GetName()
													+ " (" + currentElement.GetType() 
													+ ", ID=" + currentElement.GetElementID() 
													+ ")" );
						}
					}
				}
				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();
			}
		}
	}

}
