/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.debug.internal.script.model;

import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugTarget;

/**
 * All elemet super class.
 */
public abstract class ScriptDebugElement extends PlatformObject implements
		IDebugElement
{

	/**
	 * Debug target
	 */
	ScriptDebugTarget target;

	/**
	 * Constructor
	 * 
	 * @param target
	 */
	public ScriptDebugElement( ScriptDebugTarget target )
	{
		this.target = target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
	 */
	public IDebugTarget getDebugTarget( )
	{
		return target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
	 */
	public ILaunch getLaunch( )
	{
		return target.getLaunch( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
	 */
	public String getModelIdentifier( )
	{
		return IScriptConstants.SCRIPT_DEBUG_MODEL;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
	 */
	public Object getAdapter( Class adapter )
	{
		if ( adapter == IDebugElement.class || adapter==ScriptDebugElement.class)
		{
			return this;
		}
		else if ( adapter == IDebugTarget.class )
		{
			return getDebugTarget( );
		}
		return super.getAdapter( adapter );
	}

	/**
	 * Fires a debug event
	 * 
	 * @param event
	 *            the event to be fired
	 */
	protected void fireEvent( DebugEvent event )
	{
		DebugPlugin.getDefault( ).fireDebugEventSet( new DebugEvent[]{
			event
		} );
	}

	/**
	 * Fires a <code>CREATE</code> event for this element.
	 */
	protected void fireCreationEvent( )
	{
		fireEvent( new DebugEvent( this, DebugEvent.CREATE ) );
	}

	/**
	 * Fires a <code>RESUME</code> event for this element with the given
	 * detail.
	 * 
	 * @param detail
	 *            event detail code
	 */
	public void fireResumeEvent( int detail )
	{
		fireEvent( new DebugEvent( this, DebugEvent.RESUME, detail ) );
	}

	/**
	 * Fires a <code>SUSPEND</code> event for this element with the given
	 * detail.
	 * 
	 * @param detail
	 *            event detail code
	 */
	public void fireSuspendEvent( int detail )
	{
		fireEvent( new DebugEvent( this, DebugEvent.SUSPEND, detail ) );
	}

	/**
	 * Fires a <code>TERMINATE</code> event for this element.
	 */
	protected void fireTerminateEvent( )
	{
		fireEvent( new DebugEvent( this, DebugEvent.TERMINATE ) );
	}
	
	/**Gets the displayname
	 * @return
	 */
	public abstract String getDisplayName();
}
