/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * 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 org.wandora.application.gui.topicpanels.queryeditorpanel;

import java.awt.Component;

import org.wandora.query2.DirectiveUIHints.Parameter;

/**
 *
 * @author olli
 */


public class OperandParameterPanel extends AbstractTypePanel {


	private static final long serialVersionUID = 1L;
	
	
	protected AbstractTypePanel parameterPanel;
    
    /**
     * Creates new form OperandParameterPanel
     */
    public OperandParameterPanel(Parameter parameter,DirectivePanel panel) {
        super(parameter,panel);
        initComponents();
        setOperandTypes();
    }

    @Override
    public void disconnect() {
        super.disconnect();
        for(Component c : operandPanel.getComponents()){
            if(c instanceof AbstractTypePanel) ((AbstractTypePanel)c).disconnect();
        }
    }

    @Override
    public void setValue(Object o){
        /*if(o instanceof Directive){
            operandTypeComboBox.setSelectedItem("Directive");
            operandTypeChanged();
            parameterPanel.setValue(o);
        }*/
        if(o instanceof DirectivePanel){
            operandTypeComboBox.setSelectedItem("Directive");
            operandTypeChanged();
            parameterPanel.setValue(o);            
        }
        else if(o instanceof String){
            operandTypeComboBox.setSelectedItem("String");
            operandTypeChanged();
            parameterPanel.setValue(o);            
        }
    }
    
    @Override
    public Object getValue(){
        if(parameterPanel==null) return null;
        return parameterPanel.getValue();
    }    
    @Override
    public String getValueScript(){
        if(parameterPanel==null) return "null";
        return parameterPanel.getValueScript();        
    }
    
    @Override
    public void setLabel(String label){
        parameterLabel.setText(label);
    }
    
    protected void setOperandTypes(){
        operandTypeComboBox.addItem("String");
        operandTypeComboBox.addItem("Directive");
    }
    
    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        parameterLabel = new javax.swing.JLabel();
        operandTypeComboBox = new javax.swing.JComboBox();
        operandPanel = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        parameterLabel.setText("Label");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
        add(parameterLabel, gridBagConstraints);

        operandTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                operandTypeComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        add(operandTypeComboBox, gridBagConstraints);

        operandPanel.setLayout(new javax.swing.BoxLayout(operandPanel, javax.swing.BoxLayout.LINE_AXIS));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        add(operandPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    protected boolean operandTypeChanged(){
        if(parameterPanel!=null) parameterPanel.disconnect();
        parameterPanel=null;
        Object o=operandTypeComboBox.getSelectedItem();
        if(o==null){
            operandPanel.removeAll();
            this.revalidate();
            operandPanel.repaint();
            return true;
        }
        String type=o.toString();
        if(type.equalsIgnoreCase("String")){
            operandPanel.removeAll();
            StringParameterPanel p=new StringParameterPanel(parameter,this.directivePanel);
            p.setLabel("");
            p.setOrderingHint(this.orderingHint);
            operandPanel.add(p);
            this.revalidate();
            operandPanel.repaint();
            parameterPanel=p;
            return true;
        }
        else if(type.equalsIgnoreCase("Directive")){
            operandPanel.removeAll();
            DirectiveParameterPanel p=new DirectiveParameterPanel(parameter,this.directivePanel);
            p.setLabel("");
            p.setOrderingHint(this.orderingHint);
            operandPanel.add(p);
            this.revalidate();
            operandPanel.repaint();
            parameterPanel=p;
            return true;
        }
        else return false;
    }

    @Override
    public void setOrderingHint(String orderingHint) {
        super.setOrderingHint(orderingHint); //To change body of generated methods, choose Tools | Templates.
        if(this.parameterPanel!=null) this.parameterPanel.setOrderingHint(orderingHint);
    }
    
    
    
    private void operandTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_operandTypeComboBoxActionPerformed
        operandTypeChanged();
    }//GEN-LAST:event_operandTypeComboBoxActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JPanel operandPanel;
    protected javax.swing.JComboBox operandTypeComboBox;
    private javax.swing.JLabel parameterLabel;
    // End of variables declaration//GEN-END:variables
}
