FileDocCategorySizeDatePackage
insertbydatasource.javaAPI DocExample6330Wed Mar 15 11:29:30 GMT 2000None

insertbydatasource.java

/* 
 * This is the java class file for the insertbydatasource example 
 * referenced in the SDK documentation. It insert symbols, specified
 * in the data source.  
 */ 

import java.io.*;     //imports the standard java classes
import java.util.*;   //imports the standard java classes
import com.macromedia.generator.api.*;  // imports the Generator API classes.
import com.macromedia.generator.app.*;  // imports the classes that support the Generator API classes

/*
 * The class definition for Generator objects must extend the  
 * GenericCommand class which implements the Flash interface. 
 */
public class insertbydatasource extends GenericCommand
{

	/* Context for processing the command */
	Context context;
	Properties params;
	String valArrays[][];
	FlashEnvironment	env;
	Script				script;
	

	/* All Generator object .java files must contain the doCommand.
	 * The doCommand is called by Generator when the custom object is
	 * processed.
	 */
	public void doCommand (FlashEnvironment xenv, Script xscript, Context xcontext, int cmdIndex, Properties xparams) 
    {

		script    = xscript;
		env       = xenv;
		context   = xcontext;
		params    = xparams;

		try
		{
			/* Use the getCommandScript method to retrieve the script 
			 * for the object being created.  A script represents a 
			 * Flash movie symbol containing a timeline, and possibly 
			 * shapes, symbols, and text.
			 */
			Script templateClip = script.getCommandScript(cmdIndex);

			if (templateClip == null)
				throw new Exception("movie clip missing");
			
			/* Use getCommandFrameCount to retrieve the number of 
			 * frames associated with the command. */
			int frameCount = script.getCommandFrameCount(cmdIndex);

			/* Use the getStringParam method in the GenericCommand class
			 * to obtain data source info.  Please note that for each 
			 * getStringParam method, the third parameter should be 
			 * matched with the 'token' name in the insertbydatasource.def
			 * file.
			 */
			String datasourceParam = getStringParamEmpty( context, params, "datasource");
			
			/*
			 * The URLDataSource class parses text data referenced by a
			 * URL path, into a series of rows and columns.
			 */
			UrlDataSource dataSource = new UrlDataSource();

			/* Use setEnvironment to allow the data source to log errors
			 * to the environment. */
			dataSource.setEnvironment(env);

			/* Use getData to retrieve the values from the data 
			 * source. */
			if (datasourceParam != "")
				valArrays = dataSource.getData(datasourceParam);
			
			/* Get the index of the 'clip' column in the data source. */
			int clipColumn = FindColumn("clip");
		
			/* Create scripts for all of the items in the array. */
			Script items[] = new Script[valArrays.length];


			for (int iRow = 1;  iRow <= valArrays.length - 1;  iRow++)
			{	
				/* Create a context for the clip */
				Context clipContext = new Context();

				/* Set the parent to the context. */
				clipContext.setParent( xcontext);
				
				/* Place each column of data into the clip context.*/
				if (valArrays != null)  {
						for (int iCol = 0; iCol < valArrays[iRow].length; ++iCol)  {
							clipContext.setValue(valArrays[0][iCol], valArrays[iRow][iCol]);
						}
				}
			
			/* retrieve the symbol to be used */
			items[iRow - 1] = findSymbol(valArrays[iRow][clipColumn]);

				if (items[iRow - 1] != null)
				{
					/* Create a copy of the clip to insert into the 
					 * script. */
					items[iRow - 1] = items[iRow - 1].copyScript();

					/* Process the clip with the current context. */
					env.processScript(items[iRow - 1], clipContext);
				}
				
				else
				{
					/* The specified symbol was not found log the error
					 *  with the symbol name. */
					env.logError( "UnableToFindSymbol :  " +  valArrays[iRow][clipColumn]); 
				}
				
			}

			/* Use getCommandRect to retrieve the rectangle associated
			 * with the script command. */
			Rect rect = script.getCommandRect(cmdIndex);
			int rectXMin = rect.getXMin();
			int rectYMin = rect.getYMin();
			int rectXMax = rect.getXMax();
			int rectYMax = rect.getYMax();

			/* retrieve  the offset values entered by the user in the 
			 * Generator inspector.  */
			int offset_x = getIntParam( context, params, "offset_x", 500);
			int offset_y = getIntParam( context, params, "offset_y", 500);
		
			/* Place all of the items into the movie */
			for (int itemCount = 0;  itemCount <= valArrays.length - 2;  itemCount++)
			{

				/* Use the x, y offset values entered by the user, to 
				 * avoid having all of the objects rendered in the same
				 * location.
				 */
				Matrix matrix = new Matrix();
				matrix.setXOffset( offset_x * itemCount);
				matrix.setYOffset( offset_y * itemCount);

				String anylabel = null;

				/* insert the object into the script.  */
				templateClip.insertObject(items[itemCount], 0, 0, matrix, frameCount, matrix, null, anylabel);
			}

		}
		catch (Exception e)
		{
			/* Catch an exception and display the name of the exception
			 * in the Generator Output window if there are any errors.  
			 * You must set the log level to "Verbose" to view errors.  
			 * The exception may not throw a detailed message.
			 */
			env.logMessage(" An exception occured at:  " + e.getClass() + "  with message: " + e.getMessage());
		}

	}


	/* This method  returns the index of the column name */
	private int FindColumn(String colName)
	{
		/* if data source was not specified, no valArrays. */
		if (valArrays == null)  return -1;
		
		/* Determine the index of the column. */
		for (int column = 0; column < valArrays[0].length; ++column)
		{
			/* Is this the column? */
			if (valArrays[0][column].equalsIgnoreCase(colName))
			{
				/* Yes. return the column. */
				return column;
			}
		}
		/* Column not found. */
		return -1;
	}

	/* This method searches for the specified symbol. */
	private Script findSymbol(String data)
	{
		Script symbol = script.findScript(data);

		/* If not found, look in the main template script for the clip 
		 * to insert. */
		if (symbol == null)
			symbol = env.getTemplate().findScript(data);
		 
		return symbol;
		
	}
}