/*
 * BINewPresWizard.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 class provides the Wizard functionality.  It enables a user to select 
 *   the type of presentation to create and specify the query behind the 
 *   presentation.
 */
package samples.javaclient;

import java.awt.Frame;
import oracle.dss.queryBuilder.QueryBuilder;
import oracle.dss.datautil.gui.panel.StandardPanel;
import oracle.dss.datautil.gui.panel.DefaultStandardPanel;
import oracle.dss.dataSource.client.QueryManager;
import oracle.dss.dataSource.common.Query;
import oracle.dss.util.LayoutContext;
import oracle.dss.dataView.Dataview;
import oracle.dss.graph.Graph;
import oracle.dss.graph.gui.GraphLayout;
import oracle.dss.graph.gui.GraphType;
import oracle.dss.crosstab.Crosstab;
import oracle.dss.util.gui.layout.CrosstabLayout;
import oracle.dss.datautil.gui.DefaultBuilderDialog;
import oracle.bali.ewt.wizard.ImageWizardPage;
import oracle.bali.ewt.wizard.WizardEvent;
import oracle.bali.ewt.wizard.WizardListener;

public class BINewPresWizard extends QueryBuilder 
                                   implements WizardListener { 

  private Graph                           m_graph;
  private BINewPresStandardWizardPanel    m_graphTypePanel;
  private GraphType                       m_graphType;
  private Crosstab                        m_crosstab;
  private QueryManager                    m_queryManager;
  private int                             m_viewType;
  private BINewPresTypePanel              m_prezType;
  private boolean                         m_isGraphTypeVisible = false;
  public static final int       CROSSTAB = 0;
  public static final int       GRAPH = 1;
  public static final String    GRAPH_TYPE = "GraphType";
  public static final String    GRAPH_TYPE_TITLE = "Choose a Graph Type";
  
  public BINewPresWizard(Frame f) {
    super(f);

    // Hide Welcome page by default
    setShowWelcomeNextTime(false);
  }

  /*
   * run() will update the QueryBuilder by adding a new panel.  This panel
   * asks the user to create a Graph or a Crosstab.
   * 
   * After updating the QueryBuilder with the new panel, it will then run
   * the QueryBuilder super class
   */
  public boolean run () {
    boolean retval = false;
    
    // Check if there is a query associated with the Wizard.
    // if not, try to create one.
    // if one can not be created, return null
    try {

      Query query = (Query) this.getQuery();

      if (query == null) {
        if (m_queryManager == null) {
          return retval;
        }
        query = m_queryManager.createQuery();
        setQuery(query);
      }

      // Specify that the QueryBuilder will run in Wizard mode
      // Then, make the Presentation Type panel the first page of the wizard
      setMode(QueryBuilder.WIZARD);
      initialize();

      m_prezType = new BINewPresTypePanel(this);    
      this.addPanel((StandardPanel) m_prezType, QueryBuilder.ITEMS_PANEL_ID);

      // Listen to navigation events on the wizard        
      ((DefaultBuilderDialog) this.getContainer()).getWizard().addWizardListener(this);

      // Run the QueryBuilder.  If the user clicked OK, then return true
      if (super.run())
        retval = true;

    }
    catch (Exception ex) {
      System.out.println(ex);
      
    }

    return retval;

  }

  /*
   *   Returns the dataview that was created by the wizard
   */
  public Dataview getDataview() {
    if (getViewType() == GRAPH) 
      return (Dataview) m_graph;
    else if (getViewType() == CROSSTAB) 
      return (Dataview) m_crosstab;
    else return null;      
   
  }
  
  public void setQueryManager(QueryManager qm) {
    // Set the metadatamanager based on the QueryManager
    m_queryManager = qm;
    setMetadataManager(m_queryManager.getMetadataManager());
  }

  public QueryManager getQueryManager() {
    return m_queryManager;
  }
  

  public void setViewType(int viewType) {
    if (viewType != 0 && viewType != 1) {
      return;
    }
    m_viewType = viewType;
  }
  public int getViewType () {
    return m_viewType;
  }

  /*
   * wizardSelection process the navigation through the wizard.  The QueryBuilder
   * is based on the JEWT wizard framework - so review the JEWT javadoc for
   * details on the wizard's methods and events.
   *
   * Wizard pages will be added/removed/modified based on the type of presentation
   * being created.  For example, a graph type panel is added if the user is 
   * creating a graph.  This panel is not displayed if the user is creating 
   * a crosstab.
   */
  public void wizardSelectionChanged(WizardEvent e) {

    // Look at the page that is being navigated to
    ImageWizardPage nextPage = (ImageWizardPage) e.getPage();
    if (nextPage == null) {
      return;
    }

    /*
     * Each QueryBuilder panel has a unique ID.  This enables us to know 
     * where the user is in the wizard
     */
    String nextID = ((StandardPanel) nextPage.getInteractiveArea()).getId();

    /* If the user is creating a graph, then create a graph type dialog - which 
       will appear after the Pick Items panel.  Also, create the graph
       layout panel 
    */
    if (nextID.equals(QueryBuilder.ITEMS_PANEL_ID) && getViewType()==GRAPH) {
      // If graph has never been initialized, then initialize it and set the 
      // wizard up so that there are graph panels
      if (m_graph == null) {
        // Create a graph
        m_graph = new Graph();
        m_graph.setDataSource((Query) this.getQuery());

        m_graphType = new GraphType(m_graph);
        m_graphTypePanel = new BINewPresStandardWizardPanel(GRAPH_TYPE, GRAPH_TYPE_TITLE);
        m_graphTypePanel.add(m_graphType);
        m_isGraphTypeVisible = true;
        this.addPanel((StandardPanel) m_graphTypePanel, QueryBuilder.LAYOUT_PANEL_ID);        
        
      }
      /*
       * The wizard needs to handle when the user navigates backwards and forwards.
       * 
       * Case: User created a graph previously, changed to Crosstab, 
       * Changed back to graph 
       */
      if (!m_isGraphTypeVisible) {
        this.addPanel((StandardPanel) m_graphTypePanel, QueryBuilder.LAYOUT_PANEL_ID);
        m_isGraphTypeVisible = true;
      }      

    }  // nextID == ITEMSPanel and GRAPH
    else if (nextID.equals(QueryBuilder.ITEMS_PANEL_ID) && getViewType()==CROSSTAB) {
      // If crosstab has never been initialized, then initialize it 
      if (m_crosstab == null) {
        // Create a crosstab
        m_crosstab = new Crosstab();
        m_crosstab.setDataSource((Query) this.getQuery());      
      }

      // Hide graph type panel if nec
      if (m_graphTypePanel != null) {
        removePanel(GRAPH_TYPE);
        m_isGraphTypeVisible = false;  
      }
    } 
    /*
     * Next panel is the Layout panel.  Use the Crosstab layout panel here
     */
    else if (nextID.equals(QueryBuilder.LAYOUT_PANEL_ID) && this.getViewType()==CROSSTAB) {
        CrosstabLayout crosstabLayout = new CrosstabLayout();
        crosstabLayout.setPagingControlVisible(true);        
        crosstabLayout.setLayoutContext((LayoutContext) this.getQuery());        
        this.setViewLayoutPanel(crosstabLayout);
    }
    
    /*
     *  Next panel is the layout panel, but this time the user is creating a graph
     */ 
    else if (nextID.equals(QueryBuilder.LAYOUT_PANEL_ID) && this.getViewType()==GRAPH) {
        m_graphType.apply();
        // Set up the Graph Layout panel
        GraphLayout graphLayout = new GraphLayout();
        graphLayout.setGraph(m_graph);
        graphLayout.setPagingControlVisible(true);        
        graphLayout.setLayoutContext((LayoutContext) this.getQuery());       
        this.setViewLayoutPanel(graphLayout);

    }
  }


  public void wizardApplyState(WizardEvent e) {
  }

  public void wizardCanceled(WizardEvent e) {
  }

  public void wizardFinished(WizardEvent e) {
    if (getViewType() == GRAPH) 
      m_graphType.apply();
  }
  
}