/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.das2.qds.filters;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.das2.datum.Units;
import static org.das2.datum.Units.getAllUnits;

/**
 *
 * @author mmclouth
 */
public class SetDepend0UnitsFilterEditorPanel extends AbstractFilterEditorPanel {

    /**
     * Creates new form SetDepend0UnitsFilterEditorPanel
     */
    public SetDepend0UnitsFilterEditorPanel() {
        initComponents();
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        unitsCB = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setText("Depend0 Units:  ");

        List<Units> units = getAllUnits();
        Units[] array = units.toArray(new Units[units.size()]);
        unitsCB.setEditable(true);
        unitsCB.setModel(new javax.swing.DefaultComboBoxModel(array));
        unitsCB.setPreferredSize(new java.awt.Dimension(200, 27));

        jLabel2.setText("Explicitly set the units of the independent variable corresponding to the first index");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(unitsCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(unitsCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JComboBox unitsCB;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setFilter(String filter) {
        Pattern p= Pattern.compile("\\|setDepend0Units\\('(\\w+)'\\)");
        Matcher m= p.matcher(filter);
        if ( m.matches() ) {
            unitsCB.setSelectedItem( Units.lookupUnits(m.group(1)) );
        }
        else {
            unitsCB.setSelectedItem( Units.lookupUnits("s") );
        }
        
    }

    @Override
    public String getFilter() {
        return "|setDepend0Units('" + unitsCB.getSelectedItem().toString() + "')";
    }
}
