/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.apache.jmeter.report.gui;

import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import org.apache.jmeter.gui.util.ReportMenuFactory;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.testelement.AbstractChart;
import org.apache.jmeter.testelement.AbstractTable;
import org.apache.jmeter.testelement.BarChart;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.gui.JLabeledChoice;
import org.apache.jorphan.gui.JLabeledTextField;

public class BarChartGui extends AbstractReportGui {

    private JLabeledChoice xAxisLabel = new JLabeledChoice();
    
    private JLabeledTextField yAxisLabel = 
        new JLabeledTextField(JMeterUtils.getResString("report_chart_y_axis_label"));
    
    private JLabeledTextField caption = 
        new JLabeledTextField(JMeterUtils.getResString("report_chart_caption"),
                Color.white);
    private JLabeledTextField url = 
        new JLabeledTextField(JMeterUtils.getResString("report_bar_graph_url"),
                Color.white);

    private JLabeledChoice yItems = new JLabeledChoice();
	private JLabeledChoice xItems = new JLabeledChoice();

    public BarChartGui() {
		super();
		init();
	}
	
	public String getLabelResource() {
		return "report_bar_chart";
	}
	
	public JPopupMenu createPopupMenu() {
        JPopupMenu pop = new JPopupMenu();
        ReportMenuFactory.addFileMenu(pop);
        ReportMenuFactory.addEditMenu(pop,true);
        return pop;
	}

	private void init() {// called from ctor, so must not be overridable
        setLayout(new BorderLayout(10, 10));
        setBorder(makeBorder());
        setBackground(Color.white);

        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout(10,10));
        pane.setBackground(Color.white);
        pane.add(this.getNamePanel(),BorderLayout.NORTH);
        
        VerticalPanel options = new VerticalPanel(Color.white);
        xAxisLabel.setBackground(Color.white);
        yAxisLabel.setBackground(Color.white);

        JLabel xLabel = new JLabel(JMeterUtils.getResString("report_chart_x_axis"));
		HorizontalPanel xpanel = new HorizontalPanel(Color.white);
		xLabel.setBorder(new EmptyBorder(5,2,5,2));
        xItems.setBackground(Color.white);
        xItems.setValues(AbstractTable.xitems);
        xpanel.add(xLabel);
        xpanel.add(xItems);
        options.add(xpanel);

        JLabel xALabel = new JLabel(JMeterUtils.getResString("report_chart_x_axis_label"));
        HorizontalPanel xApanel = new HorizontalPanel(Color.white);
        xALabel.setBorder(new EmptyBorder(5,2,5,2));
        xAxisLabel.setBackground(Color.white);
        xAxisLabel.setValues(AbstractChart.X_LABELS);
        xApanel.add(xALabel);
        xApanel.add(xAxisLabel);
        options.add(xApanel);
        
		JLabel yLabel = new JLabel(JMeterUtils.getResString("report_chart_y_axis"));
		HorizontalPanel ypanel = new HorizontalPanel(Color.white);
		yLabel.setBorder(new EmptyBorder(5,2,5,2));
        yItems.setBackground(Color.white);
        yItems.setValues(AbstractTable.items);
        ypanel.add(yLabel);
        ypanel.add(yItems);
        options.add(ypanel);
        options.add(yAxisLabel);
        options.add(caption);
        options.add(url);
        
        add(pane,BorderLayout.NORTH);
        add(options,BorderLayout.CENTER);
	}
	
	public TestElement createTestElement() {
		BarChart element = new BarChart();
		modifyTestElement(element);
		return element;
	}

	public void modifyTestElement(TestElement element) {
		this.configureTestElement(element);
		BarChart bc = (BarChart)element;
		bc.setXAxis(xItems.getText());
		bc.setYAxis(yItems.getText());
		bc.setXLabel(xAxisLabel.getText());
		bc.setYLabel(yAxisLabel.getText());
        bc.setCaption(caption.getText());
        bc.setURL(url.getText());
	}
	
    public void configure(TestElement element) {
        super.configure(element);
        BarChart bc = (BarChart)element;
        xItems.setText(bc.getXAxis());
        yItems.setText(bc.getYAxis());
        xAxisLabel.setText(bc.getXLabel());
        yAxisLabel.setText(bc.getYLabel());
        caption.setText(bc.getCaption());
        url.setText(bc.getURL());
    }
    
}
