/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.gui;

import net.aavalla.utils.jobpool.parameters.gui.ValueComponent;
import compid.application.CompidApp;
import compid.application.DatasetsChangedListener;
import compid.application.DatasetsHandler;
import compid.data.tuples.DatasetInfo;
import compid.report.DatasetCollection;
import net.aavalla.utils.jobpool.parameters.FieldInstance;
import java.util.Vector;

/**
 *
 * @author Lari Natri
 */
public class DatasetCollectionComponent extends ValueComponent<DatasetCollection> implements DatasetsChangedListener {

    private Vector<DatasetInfo> chosenSets = new Vector<DatasetInfo>(1);

    /** Creates new form DatasetCollectionComponent */
    public DatasetCollectionComponent(FieldInstance field, boolean allowEdit) {
        super(field, allowEdit);
        initComponents();

        DatasetsHandler dh = CompidApp.getApplication().getDatasetsHandler();
        dh.addDatasetsChangedListener(this);

        jComboBox1.setModel(dh.getAsComboBoxModel());

        if (allowEdit==false) {
            jComboBox1.setVisible(false);
            addButton.setVisible(false);
            clearButton.setVisible(false);
        }

        this.setFieldValue((DatasetCollection)field.getValue()); // set default value

    }

    @Override
    public void dispose() {
        CompidApp.getApplication().getDatasetsHandler().removeDatasetsChangedListener(this);
        super.dispose();
    }


    @Override
    public void datasetsChanged(DatasetsHandler source) {
        jComboBox1.setModel(source.getAsComboBoxModel());
    }

    @Override
    protected void updateUIWithValue(DatasetCollection newValue) {
        chosenSets.clear();
        String text = "";
        if (newValue!=null) {
            chosenSets.addAll(newValue.getCollection());
            for (DatasetInfo dsi : chosenSets) {
                text+=dsi.getName()+"; ";
            }
        }

        if (text.length()>0) {
            text = text.substring(0,text.length()-2);
        }
        this.chosenTextField.setText(text);
        this.countLabel.setText(chosenSets.size() + " selected");
    }


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

        jComboBox1 = new javax.swing.JComboBox();
        chosenTextField = new javax.swing.JTextField();
        clearButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        countLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(compid.application.CompidApp.class).getContext().getResourceMap(DatasetCollectionComponent.class);
        chosenTextField.setBackground(resourceMap.getColor("chosenTextField.background")); // NOI18N
        chosenTextField.setEditable(false);
        chosenTextField.setFont(chosenTextField.getFont().deriveFont(chosenTextField.getFont().getStyle() | java.awt.Font.BOLD));
        chosenTextField.setText(resourceMap.getString("chosenTextField.text")); // NOI18N
        chosenTextField.setToolTipText(resourceMap.getString("chosenTextField.toolTipText")); // NOI18N
        chosenTextField.setName("chosenTextField"); // NOI18N

        clearButton.setIcon(resourceMap.getIcon("clearButton.icon")); // NOI18N
        clearButton.setText(resourceMap.getString("clearButton.text")); // NOI18N
        clearButton.setToolTipText(resourceMap.getString("clearButton.toolTipText")); // NOI18N
        clearButton.setName("clearButton"); // NOI18N
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        addButton.setIcon(resourceMap.getIcon("addButton.icon")); // NOI18N
        addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
        addButton.setToolTipText(resourceMap.getString("addButton.toolTipText")); // NOI18N
        addButton.setName("addButton"); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        countLabel.setLabelFor(chosenTextField);
        countLabel.setText(resourceMap.getString("countLabel.text")); // NOI18N
        countLabel.setName("countLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jComboBox1, 0, 171, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chosenTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(countLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clearButton)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(clearButton)
                        .addComponent(addButton))
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chosenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(countLabel)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        DatasetInfo dsiChosen = (DatasetInfo)jComboBox1.getSelectedItem();
        if (dsiChosen!=null) {
            chosenSets.add(dsiChosen);
        }
        String name = "";
        for (DatasetInfo dsi : chosenSets) {
            name+=dsi.getName()+"; ";
        }
        if (name.length()>0) { // remove trailing separator
            name = name.substring(0,name.length()-2);
        }
        DatasetCollection coll = new DatasetCollection(name);
        for (DatasetInfo dsi : chosenSets) {
            coll.add(dsi);
        }
        this.setFieldValue(coll);
    }//GEN-LAST:event_addButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        this.chosenSets.clear();
        this.setFieldValue(new DatasetCollection(""));
        this.chosenTextField.setText("");
        this.countLabel.setText("0 selected");

    }//GEN-LAST:event_clearButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JTextField chosenTextField;
    private javax.swing.JButton clearButton;
    private javax.swing.JLabel countLabel;
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration//GEN-END:variables

}
