/*
 * ComparisonPane.java
 *
 * Created on September 8, 2008, 2:26 AM
 */

package bicat.gui;

import bicat.GO.List;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
/**
 *
 * @author  Hamed
 */
public class ComparisonPane extends javax.swing.JPanel {

    /** Creates new form ComparisonPane */
    public ComparisonPane() {
        initComponents();
    colorWheel = new Paint[10]; // samo 8?
    colorWheel[0] = Color.CYAN;
    colorWheel[1] = Color.ORANGE;
    colorWheel[2] = Color.GREEN;                    
    colorWheel[3] = Color.MAGENTA;
    colorWheel[4] = Color.PINK;
    colorWheel[5] = Color.RED;
    colorWheel[6] = Color.YELLOW;
    colorWheel[7] = Color.BLUE;    
    colorWheel[8]= Color.PINK.darker();
    colorWheel[9] = Color.magenta.darker();
    }
   public void setOwner(BicatGui o) {
		owner = o;
	}
// ===========================================================================
/**
 * For <code>ActionListener</code> interface, could be used to react to
 * user input.
 * 
 */
public void actionPerformed(ActionEvent e) {
        owner.comparisonScrollPane.repaint();
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        fr_Panel = new javax.swing.JPanel();
        sp_Panel = new javax.swing.JPanel();

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        fr_Panel.setName("fr_Panel"); // NOI18N

        javax.swing.GroupLayout fr_PanelLayout = new javax.swing.GroupLayout(fr_Panel);
        fr_Panel.setLayout(fr_PanelLayout);
        fr_PanelLayout.setHorizontalGroup(
            fr_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        fr_PanelLayout.setVerticalGroup(
            fr_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 272, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Percentage of Enriched Biclusters", fr_Panel);

        sp_Panel.setName("sp_Panel"); // NOI18N

        javax.swing.GroupLayout sp_PanelLayout = new javax.swing.GroupLayout(sp_Panel);
        sp_Panel.setLayout(sp_PanelLayout);
        sp_PanelLayout.setHorizontalGroup(
            sp_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        sp_PanelLayout.setVerticalGroup(
            sp_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 272, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Selected Patterns", sp_Panel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel fr_Panel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel sp_Panel;
    // End of variables declaration//GEN-END:variables
    BicatGui owner;
    org.jfree.chart.JFreeChart chart;
    org.jfree.chart.ChartPanel chartPanel; // ChartPanel chartPanel;
    Paint[] colorWheel = null;
// for the chart 
    
    
public void setFRData()
{
    
}
public void updateFRGraphic_3D()
{
    // create the dataset
    chart = create3DChart(createDataset());
    chartPanel = new org.jfree.chart.ChartPanel(chart); // ChartPanel(chart);
    //chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    fr_Panel.removeAll();
    fr_Panel.setLayout(new GridLayout(0, 1));
    fr_Panel.add(chartPanel);
    fr_Panel.setVisible(true);
    this.setVisible(true);
    
    chartPanel.revalidate();
    owner.comparisonScrollPane.repaint();
    
}

private CategoryDataset createDataset() {

         double[][] data = new double[][]
            {{10.0, 4.0, 15.0, 14.0,55},
             {5.0, 7.0, 14.0, 3.0,65},
             {6.0, 17.0, 12.0, 7.0,56},
             {7.0, 15.0, 11.0, 0.0,56},
             {8.0, 6.0, 10.0, 9.0,56},
             {9.0, 8.0, 0.0, 6.0,56},
             {10.0, 9.0, 7.0, 7.0,56},
             {11.0, 13.0, 9.0, 9.0,56},
             {3.0, 7.0, 11.0, 10.0,56}};
        
    //==========set the data======================================/
    data=new double[owner.goBean.getPValues().length] [owner.goBean.getTotalList().size()];
    //LinkedList fClassList=null;
    List list;
    Vector fc_AllPvalues;
    Vector fc_pvalue=new Vector();
    LinkedList fClassList=null;
    int count=0;
    double percentage=0;
    String  [] algoNames_categories=new String [owner.goBean.getTotalList().size()];
    String  [] pvalues_Series=new String [owner.goBean.getPValues().length];
    
    for(int i=0;i<owner.goBean.getTotalList().size();i++)
    {
        list=(List)owner.goBean.getTotalList().get(i);
        algoNames_categories[i]=list.getName();
        
        for(int j=0;j<list.getFc_AllPvalues().size();j++)
        {
            pvalues_Series[j]="P-value threshold = " +owner.goBean.getPValues()[j];
            fc_pvalue=(Vector)list.getFc_AllPvalues().get(j);
            for(int k=0;k<fc_pvalue.size();k++)
            {
                
                fClassList=(LinkedList) fc_pvalue.get(k);
                if(fClassList!=null &&  fClassList.size()>0) // condition of enrichement
                count++;
            }
           percentage=count*100/list.getGoFilePaths().length;
           data[j][i]=percentage;
           count=0;
        }
        
    }        
   return DatasetUtilities.createCategoryDataset(pvalues_Series, algoNames_categories, data);
  // return DatasetUtilities.createCategoryDataset("Series", "category", data);
      
    }
private JFreeChart create3DChart(final CategoryDataset dataset) {
        
        final JFreeChart chart = ChartFactory.createBarChart3D(
            "Enrichement with Gene Ontology Functional Classes",      // chart title
            "Bi/Clustering Algorithms",               // domain axis label
            "Proportion of Enriched Biclusters per Significance Level (%)",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        
        chart.addSubtitle(new TextTitle(" GO Category :  "+owner.goBean.getGoCategory()));
       // chart.addSubtitle(new TextTitle(" GO Category :"));
        
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setNoDataMessage("NO DATA!");
        
        final CategoryAxis axis = plot.getDomainAxis();
        //axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8));
        //axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / dataset.getColumnCount()));
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        
     //   axis.setCategoryMargin(0.4);
//        axis.setUpperMargin(0.05);
//        axis.setLowerMargin(0.05);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinesVisible(true);
        plot.setWeight(100);
        
        final BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setItemLabelsVisible(true);
        for(int i=0;i<dataset.getRowCount();i++)
        {
         renderer.setSeriesPaint(i,colorWheel[i % colorWheel.length]);
         //   renderer.setSeriesPaint(i,colorWheel[colorWheel.length-1-(i % colorWheel.length)]);
        }
         
        
        return chart;

    }
public void updateFRGraphic_2D()
{
    // create the dataset
    chart = create2DChart(createDataset());
    chartPanel = new org.jfree.chart.ChartPanel(chart); // ChartPanel(chart);
    //chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    fr_Panel.removeAll();
    fr_Panel.setLayout(new GridLayout(0, 1));
    fr_Panel.add(chartPanel);
    fr_Panel.setVisible(true);
    this.setVisible(true);
    
    chartPanel.revalidate();
    owner.comparisonScrollPane.repaint();
    
    
    
}    

private  JFreeChart create2DChart(CategoryDataset dataset) {
        
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
            "Enrichement with Gene Ontology Functional Classes",      // chart title
            "Bi/Clustering Algorithms",               // domain axis label
            "Proportion of Enriched Biclusters per Significance Level (%)",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        chart.addSubtitle(new TextTitle(" GO Category :  "+owner.goBean.getGoCategory()));
     //   chart.addSubtitle(new TextTitle(" GO Category :"));
        
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setNoDataMessage("NO DATA!");
        
        final CategoryAxis axis = plot.getDomainAxis();
        //axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 8));
        //axis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / dataset.getColumnCount()));
        axis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
        
     //   axis.setCategoryMargin(0.4);
//        axis.setUpperMargin(0.05);
//        axis.setLowerMargin(0.05);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinesVisible(true);
        plot.setWeight(100);
        
//        // set the range axis to display integers only...
//        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        
       for(int i=0;i<dataset.getRowCount();i++)
        {
          renderer.setSeriesPaint(i,colorWheel[i % colorWheel.length]);
          //renderer.setSeriesPaint(i,colorWheel[colorWheel.length-1-(i % colorWheel.length)]);
        }

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / dataset.getColumnCount()));
        // OPTIONAL CUSTOMISATION COMPLETED.
        
        return chart;
        
    }
    

}
