/*

 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.

 *

 * EXPORT CONTROLLED

 * ECCN#   EAR99

 * Warning: This document contains technical data which is controlled under the

 * Bureau of Industry & Security's Export Administration Regulations and may not be

 * exported or re-exported without authorization by the Bureau of Industry & Security.

 * Diversion contrary to US law is prohibited.

 *

 * (c) 2011 United Technologies Corp.

 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).

 *

 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,

 * including any software or models in source or binary form, as well as any drawings, 

 * specifications, and documentation (collectively "the Data"), to deal in the Data without 

 * restriction, including without limitation the rights to use, copy, modify, merge, publish,

 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data

 * is furnished to do so, subject to the following conditions:

 * 

 * The above copyright notice and this permission notice shall be included in all copies or

 * substantial portions of the Data.

 *

 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING

 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR

 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION

 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE

 * USE OR OTHER DEALINGS IN THE DATA.

*/



package aee.excel;



import org.eclipse.swt.SWTError;

import org.eclipse.swt.ole.win32.OleAutomation;

import org.eclipse.swt.ole.win32.Variant;



import aee.exception.AeeException;



/**

 * Wrapper around a COM automation object

 */

public class ComAutomation {

	private OleAutomation automation;



	/**

	 * Default constructor

	 */

	public ComAutomation() {

		automation = null;

	}

	

	/**

	 * Constructor

	 * @param comobject  The COM automation object

	 */

	public ComAutomation(OleAutomation comobject) {

		this.automation = comobject;

	}

	

	/**

	 * Report the COM automation object

	 * @return the automation object

	 */

	public OleAutomation getAutomation() {

		return this.automation;

	}

	

	/**

	 * Set the automation object (for default constructor)

	 * @param comobject The COM automation object

	 */

	public void setAutomation(OleAutomation comobject) {

		this.automation = comobject;

	}

	

	/**

	 * Dispose of the automation

	 */

	public void dispose() {

		if (automation != null)

			automation.dispose();

	}

	

	/**

	 * Utility finds a property on an object

	 * @param property  The property

	 * @return          A COM automation object for the property or null if

	 *                  the property is invalid

	 * @throws AeeException if any SWT/COM error

	 */

	public OleAutomation getProperty(String property) throws AeeException {

		Variant result = null;

		try {

			int[]         dispIDs = getAutomation().getIDsOfNames(new String[] {property}); 

			result                = getAutomation().getProperty(dispIDs[0]); 

			OleAutomation oleauto = (result == null)? null

				                    :                   result.getAutomation();

			return oleauto;

			

		} catch (SWTError swterr) {

			throw new AeeException("Error getting %s: %s", property, swterr);

		} finally {

			if (result != null) result.dispose();

		}

	}

	

	/**

	 * Utility finds a property with an argument

	 * @param property The property

	 * @param arg      The argument

	 * @return         A COM automation object for the property or null if invalid

	 * @throws AeeException if any SWT/COM error

	 */

	public OleAutomation getProperty(String property, Variant arg) throws AeeException {

		Variant result = null;

		try {

			int[]         dispIDs = getAutomation().getIDsOfNames(new String[] {property}); 

			Variant[]     args    = new Variant[]{ arg };

			result                = getAutomation().getProperty(dispIDs[0], args); 

			OleAutomation oleauto = (result == null)? null

          :                   result.getAutomation();

			return oleauto;

			

		} catch (SWTError swterr) {

			throw new AeeException("Error getting %s: %s", property, swterr);

		} finally {

			if (result != null) result.dispose();

		}

	}

	

	/**

	 * Utility finds a property on an object that is not an OLE automation object

	 * @param property The property

	 * @return         A Variant defining the property value. You must dispose it

	 * @throws AeeException if any SWT/COM error

	 */

	public Variant getVariantProperty(String property) throws AeeException {

		try {

			int[]   dispIDs = getAutomation().getIDsOfNames(new String[] {property}); 

			Variant result  = getAutomation().getProperty(dispIDs[0]); 

			return result;

			

		} catch (SWTError swterr) {

			throw new AeeException("Error getting %s: %s", property, swterr);

		}

	}

	

	/**

	 * Utility sets a property on an object

	 * @param property  The property

	 * @param value     The property's value

	 * @throws AeeException if any SWT/COM error

	 */

	public void setProperty(String property, Variant value) throws AeeException {

		int[] dispIDs = getAutomation().getIDsOfNames(new String[] {property}); 

		getAutomation().setProperty(dispIDs[0], value); 

	}

}

