/*
 * BINewPres.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.
 */

/*
 *  This sample enables an end user to define new, data-aware
 *  crosstabs and graphs.  This class is the main window for the application.  It 
 *  creates a frame with menus that enable the user to launch the New Presenation 
 *  Wizard.  After the user creates the presentation, it is added to a tabbed pane -
 *  enabling the user to view previous presentations that were created.
 *  
 */
package samples.javaclient;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import oracle.dss.dataSource.common.Query;
import oracle.dss.queryBuilder.QueryBuilder;
import oracle.dss.dataView.Dataview;



public class BINewPres extends JFrame{

  private JMenuItem   m_mnuNew; 
  private JMenu m_sampleMenu;
  private JTabbedPane m_tabbedPane = new JTabbedPane();
  private BIFrame m_frame;
  private JPanel m_panel;
  

  // Default constructor
  public BINewPres(BIFrame parentFrame, String title) {
    super(title);
    m_frame = parentFrame;
    init();
  }

  /*
   *   Initializes the user interface for the application
   *   Adds action listener to handle the New... event
   */
  protected void init() {
            
    //Initialize menus
    JMenuBar m_menuBar = new JMenuBar();
 
    // adding the sample menu
    m_sampleMenu = new JMenu("Custom presentation wizard");
    m_mnuNew = new JMenuItem("New...");
    m_mnuNew.setMnemonic('N');
    m_sampleMenu.add(m_mnuNew);
    m_menuBar.add(m_sampleMenu);
    
    m_mnuNew.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        showNewPresentationWizard();
      }
    });
    
    m_panel = new JPanel();
    m_panel.setLayout(new GridLayout());
    
    // put all of these into the frame
    setJMenuBar(m_menuBar);
    getContentPane().removeAll();
    getContentPane().add(m_panel);
  
    // add a listener to listen for the window closing event
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        cleanUp();
      }
    });
    
    // show the new presentation wizard on startup
    showNewPresentationWizard();
  }


  /**
   * This method cleans up before returning to the main menu
   */
  protected void cleanUp() {
  
    // Remove tab pane from the display (will remove any presentations
    // that had been created
    m_tabbedPane.removeAll();    
    dispose();
  }
  
  /*
   *  Create a new presentation.  Uses the BINewPresWizard class - which
   *  is derived from QueryBuilder. 
   */
  
  void showNewPresentationWizard() {
    try {
      // Instantiate the wizard.  Set the QueryManager on the new wizard object
      BINewPresWizard npw = new BINewPresWizard(this);
      npw.setQueryManager(m_frame.getQueryManager());
      
      /*
       *  The "run" method on the BINewPresWizard returns true if a view
       *  (graph or crosstab) is fully specified, false if user cancelled...
       *  If all is okay, add the view to the panel
       */
      if (npw.run()) {
        /*
         *  Add the new view to the tabbed pane.  If it's the first view, then 
         *  add the tab pane to the page
         *  
         */         
        Dataview dv = npw.getDataview();

        // add the newly created dataview to a panel - and add the panel to
        // the tab pane
        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout());
        pnl.add(dv);
        m_tabbedPane.add("Presentation" + (m_tabbedPane.getTabCount()+1), pnl);
        m_tabbedPane.setSelectedComponent(pnl);

        // If this is the first view added to the pane, add the pane to the
        // main display panel
        if (m_tabbedPane.getTabCount() == 1) {
          m_panel.add(m_tabbedPane);
          m_tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
        }
          
        m_panel.validate();        
      }
    }
    catch (Exception ex) {
      m_frame.showExceptionDialog(this, ex);
    }

  }
}