package org.helioviewer.jhv.plugins.swek.view.filter;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.helioviewer.basegui.components.WheelSupport;
import org.helioviewer.jhv.plugins.swek.config.SWEKEventType;
import org.helioviewer.jhv.plugins.swek.config.SWEKParameter;
import org.helioviewer.jhv.plugins.swek.download.SWEKOperand;
import org.helioviewer.jhv.plugins.swek.download.SWEKParam;

/**
 * Creates a filter that downloads events between a minimum and a maximum value.
 * 
 * @author Bram Bourgoignie (Bram.bourgoignie@oma.be)
 * 
 */
public class DoubleMinMaxFilterPanel extends AbstractFilterPanel {

    /** The UID */
    private static final long serialVersionUID = -580186824513381393L;

    /** Minimum value spinner */
    private JSpinner minimumValueSpinner;

    /** Maximum value spinner */
    private JSpinner maximumValueSpinner;

    /**
     * Creates a double min max filter for the given event type and parameter.
     * 
     * @param eventType
     *            the event type
     * @param parameter
     *            the parameter
     */
    public DoubleMinMaxFilterPanel(SWEKEventType eventType, SWEKParameter parameter) {
        super(eventType, parameter);

    }

    @Override
    public void filter(boolean active) {
        if (active) {
            if ((Double) minimumValueSpinner.getValue() <= (Double) maximumValueSpinner.getValue()) {
                SWEKParam paramMin = new SWEKParam(parameter.getParameterName(), "" + minimumValueSpinner.getValue(),
                        SWEKOperand.BIGGER_OR_EQUAL);
                SWEKParam paramMax = new SWEKParam(parameter.getParameterName(), "" + maximumValueSpinner.getValue(),
                        SWEKOperand.SMALLER_OR_EQUAL);
                ArrayList<SWEKParam> params = new ArrayList<SWEKParam>();
                params.add(paramMin);
                params.add(paramMax);
                filterManager.addFilter(eventType, parameter, params);
            }
        } else {
            filterManager.removedFilter(eventType, parameter);
        }
    }

    @Override
    public JComponent initFilterComponents() {
        SpinnerModel minimumSpinnerModel = new SpinnerNumberModel(middleValue, min, max, stepSize);
        SpinnerModel maximumSpinnerModel = new SpinnerNumberModel(middleValue, min, max, stepSize);

        minimumValueSpinner = new JSpinner(minimumSpinnerModel);
        minimumValueSpinner.setEditor(new JSpinner.NumberEditor(minimumValueSpinner, "0.00000000"));
        minimumSpinnerModel.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if ((Double) minimumValueSpinner.getValue() > (Double) maximumValueSpinner.getValue()) {
                    maximumValueSpinner.setValue(minimumValueSpinner.getValue());
                    filterToggleButton.setSelected(false);
                }
            }
        });
        WheelSupport.installMouseWheelSupport(minimumValueSpinner);
        maximumValueSpinner = new JSpinner(maximumSpinnerModel);
        maximumValueSpinner.setEditor(new JSpinner.NumberEditor(maximumValueSpinner, "0.0000000"));
        maximumSpinnerModel.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                if ((Double) maximumValueSpinner.getValue() < (Double) minimumValueSpinner.getValue()) {
                    minimumValueSpinner.setValue(maximumValueSpinner.getValue());
                    filterToggleButton.setSelected(false);
                }
            }
        });
        WheelSupport.installMouseWheelSupport(maximumValueSpinner);
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.5;
        p.add(new JLabel("Minimum Value:"), c);
        c.gridx = 1;
        p.add(minimumValueSpinner, c);
        c.gridx = 0;
        c.gridy = 1;
        p.add(new JLabel("Maximum Value:"), c);
        c.gridx = 1;
        p.add(maximumValueSpinner, c);
        return p;
    }

}

@@ -3135,32 +3135,46 @@
 etValue());%0A    
+            %7D%0A
                 
@@ -3212,34 +3212,16 @@
 false);%0A
-                %7D%0A
         
@@ -3462,24 +3462,25 @@
 , %220.0000000
+0
 %22));%0A       
@@ -3800,32 +3800,46 @@
 etValue());%0A    
+            %7D%0A
                 
@@ -3877,34 +3877,16 @@
 false);%0A
-                %7D%0A
         

Fix problem with value filter