package edu.wustl.cab2b.client.ui.main;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

import javax.swing.ButtonGroup;
import javax.swing.DefaultButtonModel;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import edu.common.dynamicextensions.domaininterface.AttributeInterface;
import edu.wustl.cab2b.client.ui.controls.Cab2bPanel;
import edu.wustl.cab2b.client.ui.controls.Cab2bRadioButton;

public class BooleanTypePanel extends AbstractTypePanel {
    private static final long serialVersionUID = 1L;

    public BooleanTypePanel(
            ArrayList<String> conditionList,
            AttributeInterface attributeEntity,
            Boolean showCondition,
            Dimension maxLabelDimension) {
        super(conditionList, attributeEntity, showCondition, maxLabelDimension);
    }

    private JPanel getComboForBoolean(Collection<String> permissibleValueList) {
        JPanel radioPanel = new Cab2bPanel(new GridLayout(1, 0));
        ButtonGroup group = new ButtonGroup();

        for (String permissibleValue : permissibleValueList) {
            JRadioButton jRadioButton = new Cab2bRadioButton(permissibleValue);
            group.add(jRadioButton);
            radioPanel.add(jRadioButton);
        }

        return radioPanel;
    }

    private JPanel getComboForBoolean() {
        Collection<String> permissibleValueList = new LinkedList<String>();
        permissibleValueList.add("True");
        permissibleValueList.add("False");
        permissibleValueList.add("None");
        return getComboForBoolean(permissibleValueList);
    }

    /**
     * Returns the first component, with model set to true and false value.
     */
    public JComponent getFirstComponent() {
        return getComboForBoolean();
    }

    /**
     * Returns the second component.
     * There is no need of second component for Boolean type, 
     * hence returns an empty component.
     */
    public JComponent getSecondComponent() {
        JPanel secondComponent = getComboForBoolean();
        secondComponent.setVisible(false);
        secondComponent.setOpaque(false);
        return secondComponent;
    }

    public static boolean isSelected(JRadioButton jRadioButton) {
        DefaultButtonModel model = (DefaultButtonModel) jRadioButton.getModel();
        return model.getGroup().isSelected(model);
    }

    public ArrayList<String> getValues() {
        ArrayList<String> values = new ArrayList<String>();

        if (isSelected((JRadioButton) (((JPanel) m_NameEdit).getComponent(0)))) {
            values.add("true");
        } else if (isSelected((JRadioButton) (((JPanel) m_NameEdit).getComponent(1)))) {
            values.add("false");
        }

        return values;
    }

    public void setValues(ArrayList<String> values) {
        if (values.size() == 0) {
            return;
        }

        JPanel panel = (JPanel) m_NameEdit;
        JRadioButton radioButton = (JRadioButton) panel.getComponent(0);
        if (radioButton.getText().compareToIgnoreCase(values.get(0)) == 0) {
            radioButton.setSelected(true);
            return;
        }
        
        radioButton = (JRadioButton) panel.getComponent(1);
        if (radioButton.getText().compareToIgnoreCase(values.get(0)) == 0) {
            radioButton.setSelected(true);
            return;
        }

    }

    public void setComponentPreference(String condition) {
    }
    
    public void resetPanel() {
        JPanel panel = (JPanel) m_NameEdit;
        JRadioButton radioButton = (JRadioButton) panel.getComponent(2);
        radioButton.setSelected(true);
    }

}

@@ -80,20 +80,20 @@
 ava.awt.
-Grid
+Flow
 Layout;%0D
@@ -89,24 +89,99 @@
 lowLayout;%0D%0A
+import java.awt.event.ActionEvent;%0D%0Aimport java.awt.event.ActionListener;%0D%0A
 import java.
@@ -730,24 +730,149 @@
 ID = 1L;%0D%0A%0D%0A
+    private JRadioButton noneButton;%0D%0A%0D%0A    private ButtonGroup group;%0D%0A%0D%0A    private static JRadioButton selectedButton;%0D%0A%0D%0A
     public B
@@ -1142,24 +1142,26 @@
 imension);%0D%0A
+%0D%0A
     %7D%0D%0A%0D%0A   
@@ -1290,23 +1290,19 @@
 new 
-Grid
+Flow
 Layout(
-1, 0
 ));%0D
@@ -1314,43 +1314,111 @@
     
-ButtonGroup group
+group = new ButtonGroup();%0D%0A%0D%0A        RadioButtonListener radioButtonListener
  = new 
+Radio
 Button
-Group
+Listener
 ();%0D
@@ -1561,24 +1561,274 @@
 bleValue);%0D%0A
+            if (permissibleValue.equals(%22None%22)) %7B%0D%0A                noneButton = jRadioButton;%0D%0A                noneButton.setVisible(false);%0D%0A            %7D else %7B%0D%0A                jRadioButton.addActionListener(radioButtonListener);%0D%0A            %7D%0D%0A
             
@@ -2691,34 +2691,26 @@
 onent = 
-getComboForBoolean
+new JPanel
 ();%0D%0A   
@@ -3835,24 +3835,16 @@
      %7D%0D%0A
-        
 %0D%0A      
@@ -4130,20 +4130,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    pu
@@ -4332,11 +4332,503 @@
    %7D%0D%0A%0D%0A
+    class RadioButtonListener implements ActionListener %7B%0D%0A        public void actionPerformed(ActionEvent actionEvent) %7B%0D%0A            JRadioButton jRadioButton = (JRadioButton) actionEvent.getSource();%0D%0A            if (selectedButton != null && selectedButton == jRadioButton) %7B%0D%0A                group.setSelected(noneButton.getModel(), true);%0D%0A                selectedButton = null;%0D%0A            %7D else %7B%0D%0A                selectedButton = jRadioButton;%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D;%0D%0A%0D%0A
 %7D%0D%0A

Fix for Bug#4749