/*
 * BIPrint.java
 *
 * Copyright © 1994-2004, Oracle. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 */
package samples.javaclient;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JToolBar;
import oracle.dss.crosstab.Crosstab;
import oracle.dss.crosstab.CrosstabPrinter;
import oracle.dss.dataView.ViewPrinter;
import oracle.dss.dataView.gui.PageSetupDialog;
import oracle.dss.dataView.gui.PreviewDialog;
import oracle.dss.dataView.gui.PrintDialog;
import oracle.dss.dataView.gui.PrintDialogsAdapter;
import oracle.dss.dataSource.client.QueryManager;
import oracle.dss.dataSource.common.Query;

/** 
 * This class shows how to print a Crosstab.
 */ 
public class BIPrint extends JFrame {
  private JButton m_printButton;    //Button to invoke print dialog
  private JMenu m_sampleMenu;       //Menu for the sample
  private JMenuItem m_pageSetupMenu;//Menu item to invoke pageSetup dialog
  private JMenuItem m_previewMenu;  //Menu item to invoke preview dialog
  private JMenuItem m_printMenu;    //Menu item to invoke print dialog
  private CrosstabPrinter m_crosstabPrinter; //The crosstab printer object
  private Crosstab m_crosstab;      //Crosstab object
  private PrinterJob m_printerJob;  //Java PrinterJob object
  private BIFrame m_frame;
  private JPanel m_panel;
  
  //This class extends the PrintDialogsAdapter to override the
  //showPrintDialog() method.
  private class BIPrintDialogsAdapter extends PrintDialogsAdapter {
    private PrinterJob m_printerJob;
    
    /**
     * Constructor for BIPrintDialogsAdapter
     * 
     * @param printer the ViewPrinter to print 
     * @param printerJob the PrinterJob object
     */
    public BIPrintDialogsAdapter(ViewPrinter printer, PrinterJob printerJob) {
      super(printer, null, printerJob);
      m_printerJob = printerJob;
    }
    
    /**
     * Retrives the PrintDialog to be displayed when print button
     * is pressed in the preview dialog.
     *
     * @param f the Frame from which the dialog is displayed. 
     */
    public void showPrintDialog(Frame f) {
      PrintDialog dialog = new PrintDialog(f, m_crosstabPrinter, m_printerJob);  
      //When OK is pressed in the PrintDialog or print is pressed in the 
      //preview dialog, by default System print dialog is displayed. If the 
      //application wants to prevent the display of the System print dialog, 
      //then the following line should be added
      //
      // dialog.setSystemPrintDialogDisplayed(false);
      //
      //If you have a help provider, it can be set on the Print Dialog using
      //the following line
      // dialog.setHelpProvider(helpProvider);

      dialog.show();
      dialog.dispose();
      dialog = null;
    }
  }
  
  /** 
   * Constructor for BIPrint.
   *
   * @param title The title of the JFrame.
   */ 
  public BIPrint(BIFrame parentFrame, String title) {
    super(title);
    m_frame = parentFrame;
    init();
  }
  
   /**
   * This method creates a new Crosstab, sets the DataSource on the crosstab
   * and adds the Crosstab to the frame. Creates a CrosstabPrinter object and
   * PrinterJob required for printing.
   */
  protected void createCrosstab() {
    //Create a new Crosstab and set titles
    m_crosstab = new Crosstab();
    m_crosstab.setPagingControlVisible(true);
    m_crosstab.getDataviewTitle().setText("Title");
    m_crosstab.getDataviewSubtitle().setText("Subtitle");
    m_crosstab.getDataviewFootnote().setText("Footnote");
    
    //Create a crosstabPrinter
    m_crosstabPrinter = new CrosstabPrinter(m_crosstab);
    
    //Create a PrinterJob object
    m_printerJob = PrinterJob.getPrinterJob();
    
    //Set default PageFormat object on the CrosstabPrinter
    m_crosstabPrinter.setPageFormat(m_printerJob.defaultPage());
    
    //Create Query
    m_crosstab.setDataSource(m_frame.getDefaultQuery());
    
    //Add crosstab to the Frame
    m_panel.add(m_crosstab, BorderLayout.CENTER);
    m_panel.revalidate();
    m_panel.repaint();
        
  }

  /**
   * Initialize the user interface.
   */
  protected void init() {

    // Get application menu and add the menu items
    JMenuBar menuBar = new JMenuBar();
          
    // create new menu items and buttons
    m_sampleMenu = new JMenu("Printing crosstabs"); 
    m_pageSetupMenu = new JMenuItem("Page Setup...");
    m_previewMenu = new JMenuItem("Print Preview...");
    m_printMenu = new JMenuItem("Print...");

    // set Mnemonic
    m_printMenu.setMnemonic('P');
    m_previewMenu.setMnemonic('v');
    m_pageSetupMenu.setMnemonic('u');
    
    m_sampleMenu.add(m_pageSetupMenu);
    m_sampleMenu.add(m_previewMenu);
    m_sampleMenu.add(m_printMenu);
    
    menuBar.add(m_sampleMenu);
    
    // create a toolbar
    JToolBar m_toolBar = new JToolBar();
    
    //Get the url for the image file.
    URL url = BIPrint.class.getResource("images/print.gif");
    ImageIcon imagePrint = new ImageIcon(url);  
    m_printButton = new JButton(imagePrint);
    m_toolBar.add(m_printButton);
 
    //Add Listener to m_pageSetupMenu
    m_pageSetupMenu.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        showPageSetupDialog();
      }
    });
    
    //Add Listener to m_previewMenu
    m_previewMenu.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        showPreviewDialog();
      }
    });

    //Add Listener to m_printMenu
    m_printMenu.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        showPrintDialog();
      }
    });

    //Add Listener to m_printButton
    m_printButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        //When the print button is pressed in the toolbar, the application 
        //can show the Print dialog by calling the following method
        showPrintDialog();
        
       //Alternate code:
       //Directly initiates printing by replacing the above line with the 
       //following code
       //if(m_crosstabPrinter.startPrint(false)) {
       //  try{
       //    m_printerJob.setPrintable(m_crosstabPrinter, 
       //                              m_crosstabPrinter.getPageFormat());
       //    m_printerJob.print();
       //  }
       //  catch(PrinterException pe) {
       //    showExceptionDialog(BIPrint.this,pe);
       //  }
       //}

      }
    });
    
    m_panel = new JPanel();
    m_panel.setLayout(new BorderLayout());
    
    // put all of these into the frame
    setJMenuBar(menuBar);
    getContentPane().removeAll();
    getContentPane().add(m_toolBar, BorderLayout.NORTH);
    getContentPane().add(m_panel, BorderLayout.CENTER);
  
    // add a listener to listen for the window closing event
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        cleanUp();
      }
    });
    
    createCrosstab();
    
  }
  
  /**
   * This method cleans up before returning to the main menu
   */
  protected void cleanUp() {
  
    // clean the crosstab
    if(m_crosstab != null) {
      m_crosstab.setDataSource(null);
      m_panel.remove(m_crosstab);      
      m_crosstabPrinter = null;
      m_crosstab = null;
    }
    m_pageSetupMenu = null;
    m_previewMenu = null;
    m_printMenu = null;
    m_printButton = null;
    
    dispose();
  }
  
  /**
   * This method is called when the page setup menu option is selected from 
   * the file menu. 
   */
  protected void showPageSetupDialog() {
    PageSetupDialog dialog = new PageSetupDialog(this, "Page Setup", 
                             false, m_crosstabPrinter, 
                             new BIPrintDialogsAdapter(m_crosstabPrinter, 
                             m_printerJob));  
    //If you have a help provider, it can be set on the PageSetup Dialog using
    //the following line
    // dialog.setHelpProvider(helpProvider);

    dialog.setVisible(true);
    dialog.dispose();
    dialog = null;
  }
  
  /**
   * This method is called when the print preview menu option is selected from 
   * the file menu. 
   */
  protected void showPreviewDialog() {
    PreviewDialog dialog = new PreviewDialog(this, "Preview",m_crosstabPrinter, 
                           new BIPrintDialogsAdapter(m_crosstabPrinter, 
                           m_printerJob));  
    //If you have a help provider, it can be set on the Preview Dialog using
    //the following line
    // dialog.setHelpProvider(helpProvider);
    dialog.setVisible(true);
    dialog.dispose();
    dialog = null;
  }
  
  /**
   * This method is called when the print button in the toolbar is pressed or
   * the print menu option is selected from the file menu. 
   */
  protected void showPrintDialog() {
    PrintDialog dialog = new PrintDialog(this, m_crosstabPrinter, m_printerJob); 
    //When OK is pressed in the PrintDialog or print is pressed in the 
    //preview dialog, by default System print dialog is displayed. If the 
    //application wants to prevent the display of the System print dialog, then 
    //the following line should be added
    //
    // dialog.setSystemPrintDialogDisplayed(false);
    //
    
    //If you have a help provider, it can be set on the Print Dialog using
    //the following line
    // dialog.setHelpProvider(helpProvider);
    
    dialog.setVisible(true);
    dialog.dispose();
    dialog = null;
  }
}