package net.sf.cannagrower.gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JSplitPane;
import java.awt.Toolkit;
import net.sf.cannagrower.i18n.Messages;
import net.sf.cannagrower.data.Plantation;
import net.sf.cannagrower.data.Event;
import net.sf.cannagrower.data.event.EnvironmentMediumState;
import net.sf.orexio.lopf.container.Container;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.util.Vector;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
//TODO to_v0.2.0_Beta: Add more statistics and information colleration
public class DialogPlantationStatistics extends JDialog {
private static final long serialVersionUID = 1L;
private Plantation plantation;
private JPanel jContentPane = null;
private JSplitPane jSplitPane = null;
private JScrollPane jScrollPaneIndex = null;
private JList jListIndex = null;
private JPanel jPanelStatistics = null;
public DialogPlantationStatistics(JFrame parent,Plantation plantation) {
super(parent);
this.plantation=plantation;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(640, 480);
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/net/sf/cannagrower/images/klipper_dock_16.png")));
this.setContentPane(getJContentPane());
this.setTitle(Messages.getMessage(Messages.plantationStatistics));
}
private void statisticShow(){
Statistic statistic;
if (jListIndex.getSelectedIndex() >= 0
&& jListIndex.getSelectedIndex() < jListIndex
.getModel().getSize()) {
statistic=(Statistic)jListIndex.getSelectedValue();
// Show panel
getJPanelStatistics().removeAll();
getJPanelStatistics().add(statistic.getPanel(plantation),BorderLayout.CENTER);
getJPanelStatistics().updateUI();
}
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerLocation(200);
jSplitPane.setLeftComponent(getJScrollPaneIndex());
jSplitPane.setRightComponent(getJPanelStatistics());
}
return jSplitPane;
}
/**
* This method initializes jScrollPaneIndex
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPaneIndex() {
if (jScrollPaneIndex == null) {
jScrollPaneIndex = new JScrollPane();
jScrollPaneIndex.setViewportView(getJListIndex());
}
return jScrollPaneIndex;
}
/**
* This method initializes jListIndex
*
* @return javax.swing.JList
*/
private JList getJListIndex() {
if (jListIndex == null) {
Vector<Statistic> statistics=new Vector<Statistic>();
statistics.add(new Statistic_EnvironmentMedium());
jListIndex = new JList(statistics);
jListIndex
.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
statisticShow();
}
});
}
return jListIndex;
}
/**
* This method initializes jPanelStatistics
*
* @return javax.swing.JPanel
*/
private JPanel getJPanelStatistics() {
if (jPanelStatistics == null) {
jPanelStatistics = new JPanel();
jPanelStatistics.setLayout(new BorderLayout());
}
return jPanelStatistics;
}
}
interface Statistic {
String toString();
JPanel getPanel(Plantation plantation);
}
class Statistic_EnvironmentMedium implements Statistic{
public String toString(){return "EC / PH";}
public JPanel getPanel(Plantation plantation){
XYSeriesCollection collection = new XYSeriesCollection();
XYSeries ec = new XYSeries("EC");
XYSeries ph = new XYSeries("PH");
for(Container container:plantation.getEvents()){
Event event=null;
try{event=(Event)container.getData();}
catch(ClassNotFoundException e){Messages.showException(e);}
catch(java.io.IOException e){Messages.showException(e);}
if(event.getClass().equals(EnvironmentMediumState.class)){
int x;
float y;
long day;
day=(event.getDate().getTime()-plantation.getStarted().getTime());
day+=(((1000*60)*60)*24);
day=day/(((1000*60)*60)*24);
x=Integer.parseInt(Long.toHexString(day));
y=Float.parseFloat(event.getProperties().getProperty(Messages.eventMediumEc));
if(y>0){ec.add(x,y);}
y=Float.parseFloat(event.getProperties().getProperty(Messages.eventMediumPh));
if(y>0){ph.add(x,y);}
}
}
collection.addSeries(ph);
collection.addSeries(ec);
JFreeChart chart = ChartFactory.createXYAreaChart
("EC / PH", // Title
"Day", // X-Axis label
"Value", // Y-Axis label
collection, // Dataset
org.jfree.chart.plot.PlotOrientation.VERTICAL,
true,
true,
true // Show legend
);
return new ChartPanel(chart);
}
}