package org.gephi.desktop.search.filter;

import javax.swing.DefaultComboBoxModel;
import org.gephi.desktop.search.api.SearchCategory;
import org.gephi.desktop.search.impl.SearchCategoryImpl;

public class SearchPanel extends javax.swing.JPanel {

    public SearchPanel() {
        initComponents();

        // Combo
        DefaultComboBoxModel<SearchCategory> comboBoxModel = new DefaultComboBoxModel<>();
        comboBoxModel.addElement(SearchCategoryImpl.NODES());
        comboBoxModel.addElement(SearchCategoryImpl.EDGES());
        ;
        typeCombo.setModel(comboBoxModel);
        typeCombo.setSelectedIndex(0);
    }

    public void setup(SearchFilterBuilder.SearchFilter filter) {
        searchField.setText(filter.getQuery());
        typeCombo.setSelectedItem(SearchCategory.findById(filter.getType()));
        searchButton.addActionListener(evt -> {
            filter.getProperties()[0].setValue(searchField.getText());
            filter.getProperties()[1].setValue(((SearchCategory) typeCombo.getSelectedItem()).getId());
        });
    }

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

        searchField = new javax.swing.JTextField();
        typeCombo = new javax.swing.JComboBox<>();
        searchButton = new javax.swing.JButton();
        searchInLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(searchButton,
            org.openide.util.NbBundle.getMessage(SearchPanel.class, "SearchPanel.searchButton.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(searchInLabel,
            org.openide.util.NbBundle.getMessage(SearchPanel.class, "SearchPanel.searchInLabel.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(searchInLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(typeCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(searchField, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(searchButton)))
                    .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(searchInLabel)
                        .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(searchButton))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JLabel searchInLabel;
    private javax.swing.JComboBox<SearchCategory> typeCombo;
    // End of variables declaration//GEN-END:variables
}
