/*
 * BIWritebackPanel.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.Color;
import java.awt.Cursor;
import java.awt.Frame;

import java.io.IOException;

import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

import oracle.bali.ewt.border.GrayPane;
import oracle.dss.crosstab.Crosstab;
import oracle.dss.dataSource.client.QueryClient;
import oracle.dss.dataSource.common.Query;
import oracle.dss.dataSource.common.QueryException;
import oracle.dss.datautil.QueryAccess;
import oracle.dss.dataView.Dataview;
import oracle.dss.graph.BaseGraphComponent;
import oracle.dss.graph.Graph;
import oracle.dss.metadataManager.common.MetadataManagerException;

import oracle.dss.util.LayerOutOfRangeException;
import oracle.dss.util.PageItemComponentInfo;
import oracle.dss.util.SeriesComponentInfo;
import oracle.dss.util.SeriesOutOfRangeException;
import oracle.dss.util.SliceOutOfRangeException;
import oracle.dss.util.xml.BIIOException;
import oracle.dss.util.xml.BIParseException;
import oracle.dss.util.xml.BISAXException;
import oracle.dss.graph.Series;

/**
 * This class is used by the BI Beans writeback sample. 
 * It displays a Crosstab with editable cells, and a linked Graph.
 * 
 */

public class BIWritebackPanel extends JPanel  {

  /////////////////////
  //
  // Member Variables
  //
  /////////////////////

  // Supported view objects
  private Crosstab m_crosstab = null;
  private Graph m_graph = null;


  // Panels for each supported view
  private JPanel m_crosstabPanel;
  private JPanel m_graphPanel;

  // Parent frame for this panel
  private Frame m_frameParent;

  // Horizontal and vertical split panes
  private JSplitPane m_splitPaneV;
  private JSplitPane m_splitPaneH;

  /////////////////////
  //
  // Constructors
  //
  /////////////////////

  /**
   * Constructor for <code>BIWritebackPanel</code>.
   *
   */
  public BIWritebackPanel() {
    super();
  }

  /**
   * Creates linked views based on the <code>Query</code>.
   *
   * @param frameParent   a <code>Frame</code> that the panel is inserted into.
   * @param query         a <code>Query</code> value associated with the linked
   *                      view.
   * @throws Exception
   *
   
   */
  public BIWritebackPanel(Frame frameParent, Query query)
    throws Exception {
    super();

    // Initialize our panel based on the specified query
    try {
      // Use the wait cursor until we are done initializing
      frameParent.setCursor(new Cursor(Cursor.WAIT_CURSOR));
      init(query);
    }

    catch (Exception exception) {
      // Restore the default cursor
      frameParent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

      // Rethrow exception
      throw exception;
    }

      // Restore the default cursor
    frameParent.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  }

  /////////////////////
  //
  // Public Methods
  //
  /////////////////////

  /**
   * Retrieves the <code>Crosstab</code> associated with the
   * <code>BIWritebackPanel</code>.
   *
   * @return <code>Crosstab</code> associated with the <code>BIWritebackPanel
   * </code>.
   */
  public Crosstab getCrosstab() {
      return m_crosstab;
  }

  /**
   * Retrieves the <code>Graph</code> associated with the
   * <code>BIWritebackPanel</code>.
   *
   * @return <code>Graph</code> associated with the <code>BIWritebackPanel
   * </code>.
   */
  public Graph getGraph() {
    return m_graph;
  }

  /**
   * Frees up resources used by this <code>BIWritebackPanel</code>.
   */
  public void free() {
    
    // Panel clean up
    if (m_crosstabPanel != null) {
      m_crosstabPanel.removeAll();
      m_crosstabPanel = null;
    }

    if (m_graphPanel != null) {
      m_graphPanel.removeAll();
      m_graphPanel = null;
    }

    // General variable cleanup
    m_crosstab = null;
    m_graph = null;

    m_splitPaneV = null;
    m_splitPaneH = null;
  }
  
  /////////////////////
  //
  // Private Methods
  //
  /////////////////////

  /**
   * Initialize the linked views based on the specified<code>Query</code>.
   *
   * @param query The Query associated with the linked view.
   * @throws Exception if linked views cannot be initialized
   */
  private void init(Query query) throws Exception {
    setLayout(new BorderLayout());
    // Link Table and Graph together
    initCrosstab(query);
    initGraph(query);
    m_splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
           m_crosstabPanel, m_graphPanel);
    m_splitPaneV.setOneTouchExpandable(true);
    m_splitPaneV.setContinuousLayout(true);
    m_splitPaneV.setBorder(new EmptyBorder(6, 6, 6, 6));
    add(m_splitPaneV, BorderLayout.CENTER);
  }

  /**
   * Initialize the <code>Crosstab</code> based on the specified<code>Query
   * </code>.
   *
   * @param query The Query associated with the crosstab.
   * @return <code>Dataview</code> which represents the newly created crosstab.
   */
  private Dataview initCrosstab(Query query) {
    // Create a panel that contains a Crosstab
    m_crosstabPanel = new JPanel(new BorderLayout());
    if (m_crosstab == null) {
      m_crosstab = new Crosstab();
      m_crosstab.setCellEditingAllowed(true);
      m_crosstab.setPagingControlVisible(true);

      // Remember the query
      m_crosstab.setDataSource(query);
    }
    m_crosstabPanel.add(m_crosstab, BorderLayout.CENTER);
    return m_crosstab;
  }

  
  /**
   * Initialize the <code>Graph</code> based on the specified<code>Query</code>.
   *
   * @param query The Query associated with the graph.
   * @return <code>Dataview</code> which represents the newly created graph.
   */
  private Dataview initGraph(Query query) {
    // Create a panel that contains a Graph
    m_graphPanel = new JPanel(new BorderLayout());
    m_graphPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
    if (m_graph == null) 
    {
      m_graph = new Graph();
      // Enable drilling (defaults to false)
      m_graph.setDrillingEnabled(true);
      // Enable 'paging items' controls (defaults to false)
      m_graph.getPagingControl().setVisible(true);
      m_graph.setGraphType(Graph.BAR_VERT_CLUST2Y);
        try{
            m_graph.setStylePath("/oracle/dss/graph/styles/earth.xml");
            m_graph.getLegendArea().setPosition(BaseGraphComponent.LAP_BOTTOM);
            int seriesCount = m_graph.getSeriesObjectCount();
            // for each series, set a different line style
            Series series = m_graph.getSeries();
            // set Y1 as the default axis for all series
            series.setDefaultAssignedToY2(BaseGraphComponent.DY2_FALSE);
            series.setAssignedToY2(true, 2);
            series.setMarkerType(BaseGraphComponent.MT_BAR, 0);
            series.setMarkerType(BaseGraphComponent.MT_BAR, 1);
            series.setMarkerType(BaseGraphComponent.MT_MARKER, 2);
            series.setLineWidth(5, 2);
            series.setColor(Color.RED, 2);
        }
        catch(SeriesOutOfRangeException e){
           System.out.println(e.getMessage());
        }   
        catch (BIIOException biioex){
            System.out.println("BIIOException");
            biioex.printStackTrace();
        }
        catch (BISAXException bisaxex){
            System.out.println("BISAXException");
            bisaxex.printStackTrace();
        }
        catch(BIParseException bipex){
            System.out.println("BIParseException");
            bipex.printStackTrace();
        }
        catch(IOException ioex){
            System.out.println("IOException");
            ioex.printStackTrace();
        }

      // Remember the query
      m_graph.setDataSource(query);
    }
    m_graphPanel.add(m_graph, BorderLayout.CENTER);
    return m_graph;
  }
}




