/*******************************************************************************
 * 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.designer.internal.ui.dialogs;

import org.eclipse.gef.GraphicalViewer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Composite;

/**
 * Support the draw the GraphicsView to the Composite like image.
 */

public abstract class GraphicsViewComposite extends Composite
{

	private static String OPERATION_NAME = "paint";//$NON-NLS-1$
	private Object contents;
	/**
	 * @param parent
	 * @param style
	 * @param contents
	 */
	public GraphicsViewComposite( Composite parent, int style,Object contents )
	{
		super( parent, style );
		assert contents != null;
		setContents( contents );
		configPrintComposite( this );
	}

	/**
	 * @param printComposite
	 */
	private void configPrintComposite( Composite printComposite )
	{
		GraphicalViewer viewer = createGraphicalViewer( );
		configGraphicalViewer( viewer );
		final ReportPrintGraphicalViewerOperation op = new ReportPrintGraphicalViewerOperation( viewer,
				printComposite );

		printComposite.addPaintListener( new PaintListener( ) {

			public void paintControl( PaintEvent e )
			{
				op.run( OPERATION_NAME );		
			}
		} );
	}
	
	/**
	 * @return
	 */
	protected abstract GraphicalViewer createGraphicalViewer();
	
	/**
	 * @param viewer
	 */
	protected void configGraphicalViewer(GraphicalViewer viewer)
	{
		//do nothing
	}
	
	/**Gets the GraphicsView contents, the default value is the contents.
	 * @return
	 */
	protected Object getDrawContents( )
	{
		return getContents( ); 
	}

	/**
	 * @return
	 */
	protected Object getContents( )
	{
		return contents;
	}

	
	/**
	 * @param contents
	 */
	protected void setContents( Object contents )
	{
		this.contents = contents;
	}
}
