package samples.javaclient.analyzer;

import javax.swing.JTabbedPane;

import oracle.bali.ewt.dialog.JEWTDialog;

import oracle.dss.graph.Graph;
import oracle.dss.graph.gui.GraphStylePanel;
import oracle.dss.graph.gui.GraphLayout;
import oracle.dss.graph.gui.GroupAxis;
import oracle.dss.graph.gui.Legend;
import oracle.dss.graph.gui.PlotArea;
import oracle.dss.graph.gui.Title;
import oracle.dss.graph.gui.ValueAxis;
import oracle.dss.util.DataSource;
import oracle.dss.util.LayoutContext;

/**
 * Tabbed Dialog for customizing all of the key aspects
 * of the graph.  
 * This class takes the individual graph customizer panels (Title, 
 * Legend, PlotArea, GroupAxis, ValueAxis and GraphLayout) and places 
 * them in a tabbed dialog.
 */

public class GraphFormatDialog extends JEWTDialog {
    /**
     * The graph whose properties will be affected by the settings in this 
     * dialog.
     */
    private Graph m_graph;
    /**
     * Graph Style panel. 
     * Predefined, visually appealing graph styles panel.
     */
    private GraphStylePanel graphStylePanel;
    /**
     * Plot area panel. 
     * Sets background colors, marker colors, marker text, and so on.
     */
    private PlotArea plotArea;
    /**
     * Title panel. Sets title attributes.
     */
    private Title titlePanel; 
    /**
     * Data axis panel for the horizontal axis on scatter and bubble graphs. 
     * Sets scaling and axis titles.
     */
    private ValueAxis x1Axis;
    /**
     * Data axis panel for the left axis. 
     * Sets scaling and axis titles.
     */
    private ValueAxis y1Axis;
    /**
     * Data axis panel for the right axis. 
     * Sets scaling and axis titles.
     */
    private ValueAxis y2Axis;
    /**
     * Group axis panel for the horizontal axis on most graph types.
     * Sets label skipping, font attributes, and axis titles.
     */
    private GroupAxis groupAxis;
    /**
     * Legend panel.
     */
    private Legend legend;
    /**
     * Layout panel.
     */
    private GraphLayout graphLayout;
    /**
     * Tabbed pane to hold the panels.
     */
    private JTabbedPane pane;
    /**
     * Text for the title of this dialog: "Format Graph".
     */
    private static final String FORMAT_GRAPH = "Format Graph";
    /**
     * Text for the Graph Style panel: "Style".
     */
    private static final String STYLE = "Style";    
    /**
     * Text for the Title panel: "Title".
     */
    private static final String TITLE = "Title";
    /**
     * Text for the Legend panel: "Legend".
     */
    private static final String LEGEND = "Legend";
    /**
     * Text for the plot area panel: "Plot Area".
     */
    private static final String PLOT_AREA = "Plot Area";
    /**
     * Text for the X-axis panel: "X Axis".
     */
    private static final String X_AXIS = "X Axis";
    /**
     * Text for the Y1-Axis panel: "Y1 Axis".
     */
    private static final String Y1_AXIS = "Y1 Axis";
    /**
     * Text for the Y2-Axis panel: "Y2 Axis".
     */
    private static final String Y2_AXIS = "Y2 Axis";
    /**
     * Text for the layout panel: "Layout".
     */
    private static final String LAYOUT = "Layout";

    /**
     * Constructor.  Sets up the dialog using the prebuilt
     * BI Beans Graph customizer panels.
     * @param graph The graph to format.
     */
    public GraphFormatDialog(Graph graph) {
        setTitle(FORMAT_GRAPH);        
        m_graph = graph;

        // Add ok and cancel buttons
        setButtonMask(BUTTON_CANCEL + BUTTON_OK); 
            
        // Initialize all the panels.
        graphStylePanel = new GraphStylePanel(m_graph);
        titlePanel = new Title(m_graph);
        legend = new Legend(m_graph);
        plotArea = new PlotArea(m_graph);
        x1Axis = new ValueAxis(m_graph, Graph.X1AXIS);
        y1Axis = new ValueAxis(m_graph, Graph.Y1AXIS);
        y2Axis = new ValueAxis(m_graph, Graph.Y2AXIS);
        groupAxis = new GroupAxis(m_graph);
        graphLayout = new GraphLayout();
        
        // The GraphLayout needs extra details about the datasource, enabling
        // the user to rotate the dimensions
        DataSource dataSource = m_graph.getDataSource();
        LayoutContext layoutContext = (LayoutContext)dataSource;
        graphLayout.setGraph(m_graph);
        graphLayout.setLayoutContext(layoutContext);

        // add panels to tabbed pane
        pane = new JTabbedPane();
        pane.add(STYLE, graphStylePanel);
        pane.add(LAYOUT, graphLayout);        
        pane.add(TITLE, titlePanel);
        pane.add(LEGEND, legend);
        pane.add(PLOT_AREA, plotArea);
        if (m_graph.isO1AxisSupported())
            pane.add(X_AXIS, groupAxis);
        else
            pane.add(X_AXIS, x1Axis);

        pane.add(Y1_AXIS, y1Axis);

        if (isDual(m_graph.getGraphType()))
          pane.add(Y2_AXIS, y2Axis);
                       
        // adding tabbed pane to the dialog box
        setContent(pane);
           
        //pack causes this dialog to be laid out at its preferred size.
        pack();                        
    }       

    /**
     * Applies the format that was specified by the user.
     */
    public void applyFormat() {
      /*
       * For each of the panels, apply the format if the user entered that panel
       */
      graphStylePanel.apply();
      graphLayout.apply();       
      titlePanel.apply();
      plotArea.apply();
      y1Axis.applyAfterCheck();
      legend.apply();

      if (m_graph.isO1AxisSupported())
        groupAxis.applyAfterCheck();
      else
        x1Axis.applyAfterCheck();

      if (isDual(m_graph.getGraphType()))
        y2Axis.applyAfterCheck();                    
    }

    /**
     * Indicates whether a graph type supports a Y2-axis.
     * <p>
     * <strong>Note:</strong> In 9.0.5, the Graph class will have this method,
     * under the name isDualY. 
     * When you move to 9.0.5, you should call the Graph method instead of this
     * one. 
     * The Graph method will be updated when new graph types are added.
     * 
     * @param graphType The graph type to check.
     * 
     * @return <code>true</code> if the graph type supports a Y2-axis,
     *         <code>false</code> if it does not.
     */
    private boolean isDual(int graphType)
    {
        switch(graphType)
        {
            case Graph.AREA_VERT_ABS_SPLIT2Y:
            case Graph.AREA_VERT_STACK_SPLIT2Y:
            case Graph.BAR_HORIZ_CLUST_2Y:
            case Graph.BAR_HORIZ_CLUST_SPLIT2Y:
            case Graph.BAR_HORIZ_STACK_2Y:
            case Graph.BAR_HORIZ_STACK_SPLIT2Y:
            case Graph.BAR_VERT_CLUST_SPLIT2Y:
            case Graph.BAR_VERT_CLUST2Y:
            case Graph.BAR_VERT_STACK_SPLIT2Y:
            case Graph.BAR_VERT_STACK2Y:
            case Graph.BUBBLE_2Y:
            case Graph.LINE_VERT_ABS_2Y:
            case Graph.LINE_VERT_ABS_SPLIT2Y:
            case Graph.LINE_VERT_STACK_2Y:
            case Graph.LINE_VERT_STACK_SPLIT2Y:
            case Graph.POLAR_2Y:
            case Graph.SCATTER_2Y:
            case Graph.AREA_HORIZ_ABS_SPLIT2Y:
            case Graph.AREA_HORIZ_STACK_SPLIT2Y:
            case Graph.BUBBLE_LABELS_2Y:
            case Graph.LINE_HORIZ_ABS_2Y:
            case Graph.LINE_HORIZ_ABS_SPLIT2Y:
            case Graph.LINE_HORIZ_STACK_2Y:
            case Graph.LINE_HORIZ_STACK_SPLIT2Y:
            case Graph.RADAR_LINE_2Y:
            case Graph.SCATTER_LABELS_2Y:
            case Graph.STOCK_2Y:
            case Graph.STOCK_HILO_2Y:
            case Graph.STOCK_HILO_CLOSE2Y:
            case Graph.STOCK_HILO_CLOSE_SPLIT2Y:
            case Graph.STOCK_HILO_SPLIT2Y:
            case Graph.STOCK_SPLIT_2Y:
                return true;  
        }
        return false;
    }
  
}