/*
  i-net software provides programming examples for illustration only, without warranty
  either expressed or implied, including, but not limited to, the implied warranties
  of merchantability and/or fitness for a particular purpose. This programming example
  assumes that you are familiar with the programming language being demonstrated and
  the tools used to create and debug procedures. i-net software support professionals
  can help explain the functionality of a particular procedure, but they will not modify
  these examples to provide added functionality or construct procedures to meet your
  specific needs.
  © i-net software 1998-2010
*/
 
package samples.viewer;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JFrame;

import com.inet.report.EngineRenderData;
import com.inet.viewer.SwingReportViewer;

/**
 * This sample shows a simple way to display a report in an applet using the Java report viewer.
 * It is only necessary to initialize an EngineRenderData object which
 * then provides the report data to the SwingReportViewer, which in turn can
 * be displayed in a JFrame or placed in another Swing component.
 * 
 * Note that this applet will need to be certified in order to work correctly,
 * since it will require reading permission. To learn how to certify your
 * applet, see http://www-personal.umich.edu/~lsiden/tutorials/signed-applet/signed-applet.html
 */
public class EngineInApplet extends Applet implements java.awt.event.ActionListener {
	
	private SwingReportViewer viewer;          // Top-level viewer object
	private EngineRenderData renderConnection; // Main render data connection - the source of the raw report data coming from the report server

	private java.awt.Button showButton = new java.awt.Button();
	private java.awt.TextField textField1 = new java.awt.TextField();
	private java.awt.Label label1 = new java.awt.Label();
	private java.awt.Label label2 = new java.awt.Label();
	    
	/**
	 * Initialize the GUI.
	 */
	public void init() {
        // Display a Simple Text field where we can provide a report location and
        // a button where we can "start" the viewer.
		setLayout(null);
		setSize(626,129);
		showButton.setLabel("Show report");
		add(showButton);
		showButton.setBackground(java.awt.Color.lightGray);
		showButton.setBounds(480,84,96,24);
		add(textField1);
		textField1.setBounds(36,84,444,24);
		
		// default value for the textfield
		textField1.setText("http://webserver/report.rpt");
		
		label1.setText("Enter the url of a report:");
		add(label1);
		label1.setBounds(36,60,360,24);
		label2.setText("i-net Clear Reports in an applet");
		label2.setAlignment(java.awt.Label.CENTER);
		add(label2);
		label2.setFont(new Font("Dialog", Font.BOLD, 12));
		label2.setBounds(156,12,287,24);

		showButton.addActionListener(this);
	}

	/**
	 * Displays report in the viewer after showButton has been clicked.
	 * @param event The action event
	 */
	public void actionPerformed(java.awt.event.ActionEvent event) {
		Object object = event.getSource();
        SymWindow aSymWindow = new SymWindow();		
		if (object == showButton) {
            try { 
                // Create a target JFrame for the viewer
                JFrame frame = new JFrame();
                frame.addWindowListener(aSymWindow);
                frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
                
                //  To initialize we first create a top level ReportViewer:
	      	    viewer = new SwingReportViewer();
	      	       
                // Create the report URL with the content of the text field.
                String reportURL= textField1.getText();
		  
                // Initialize the render data connection.
		   		renderConnection = new EngineRenderData( reportURL );
                  
		   		// addNewReportView causes a new report view to be created using the given connection as its data source, 
		   	    // and then for this newly created report to be added to the viewer.
		   		viewer.addNewReportView( renderConnection );
                 
                // Add the viewer to the target frame
                frame.getContentPane().add( BorderLayout.CENTER, viewer);
                frame.pack();
                
                frame.setVisible( true );
            } catch(Throwable e) {
                e.printStackTrace();
            }			    
		}
	}
	
    /**
     * Window Adapter  
     */
	class SymWindow extends java.awt.event.WindowAdapter {
        /**
         * Dispose the window
         * @param event The window event
         */
	    public void windowClosing(java.awt.event.WindowEvent event) {
            Object object = event.getSource();
            ((Window)object).dispose();
        }
    }
}

