/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//This class includes a main method to demonstrate its use
package etomica.graphics;
import javax.swing.JCheckBox;

import etomica.modifier.ModifierBoolean;

/**
 * Button that toggles a boolean value via a check box.  This device can connect to any object
 * capable of switching between two states.  The device operates through a 
 * ModifierBoolean instance that must be connected to the state of the
 * controlled object.
 * 
 * @author David Kofke
 */
public class DeviceCheckBox extends Device {
    
    private ModifierBoolean modifier;
    private JCheckBox box;
    private boolean currentValue = false;
    
    public DeviceCheckBox(ModifierBoolean modifier) {
        this("Select", modifier);
    }
    public DeviceCheckBox(String label, ModifierBoolean modifier) {
        super();
        init(label, modifier);
    }
    
    private void init(String label, ModifierBoolean modifier) {
        this.modifier = modifier;
        currentValue = modifier.getBoolean();
        box = new JCheckBox(label,currentValue);
        box.addActionListener( new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggle();
            }
        });
    }

    /**
     * Sets the button to the given state.
     */
    public void setState(boolean b) {
        if(b != currentValue) toggle();
    }
    /**
     * Returns the currents true/false state of the button.
     */
    public boolean getState() {return currentValue;}
    
    /**
     * Toggles the state of the button (true to false, false to true).
     */
    public void toggle() {
        currentValue = !currentValue;
        modifier.setBoolean(currentValue);
        box.setSelected(currentValue);
    }
    
    /**
     * Specifies the boolean modifier that is set between true and false by the button.
     */
    public ModifierBoolean getModifier() {return modifier;}
    /**
     * Returns the boolean modifier set by this button.
     */
    public void setModifier(ModifierBoolean newModifier) {
        modifier = newModifier;
        modifier.setBoolean(currentValue);
    }
    
    /**
     * Returns the GUI box element for display in the simulation.
     */
    public java.awt.Component graphic(Object obj) {
        return box;
    }
    
    
    /**
     * @return a handle to the JCheckBox instance used by this device
     */
    public JCheckBox getCheckBox() {return box;}
        
    /**
     * Method to demonstrate and test the use of this class.  
     */
//    public static void main(String[] args) {
//        
//        final etomica.simulation.prototypes.HSMD2D sim = new etomica.simulation.prototypes.HSMD2D();
//        Simulation.instance = sim;
//        
//        //here's the part unique to this class
//        //sets up box to toggle atoms between red and blue
//        final ColorSchemeByType colorScheme = new ColorSchemeByType();
//        sim.display.setColorScheme(colorScheme);
//        ModifierBoolean modifier = new ModifierBoolean() {
//            public void setBoolean(boolean b) {
//                if(b) ColorSchemeByType.setColor((SpeciesSpheresMono)sim.species, java.awt.Color.blue);
//                else ColorSchemeByType.setColor((SpeciesSpheresMono)sim.species, java.awt.Color.red);
////                if(b) sim.species.allAtoms(new AtomAction() {public void actionPerformed(Atom a) {a.setColor(java.awt.Color.red);}});
////                else  sim.species.allAtoms(new AtomAction() {public void actionPerformed(Atom a) {a.setColor(java.awt.Color.blue);}});
//                sim.panel().repaint();
//            }
//            public boolean getBoolean() {return colorScheme.atomColor(sim.box.firstAtom()) == java.awt.Color.blue;}
//        };
//        modifier.setBoolean(true);
//        DeviceCheckBox button = new DeviceCheckBox(sim, "Blue", modifier);
//        //end of unique part
// 
//        sim.elementCoordinator.go();
//        SimulationGraphic.makeAndDisplayFrame(sim);
//    }
//    */
}

@@ -287,38 +287,8 @@
 ics;
-%0Aimport javax.swing.JCheckBox;
 %0A%0Aim
@@ -325,16 +325,84 @@
 Boolean;
+%0Aimport etomica.modifier.ModifyBooleanAction;%0A%0Aimport javax.swing.*;
 %0A%0A/**%0A *
@@ -739,20 +739,64 @@
 evice %7B%0A
+%0A
     
+protected ModifyBooleanAction modifyAction;
 %0A    pri
@@ -1211,32 +1211,90 @@
 ier = modifier;%0A
+        modifyAction = new ModifyBooleanAction(modifier);%0A
         currentV
@@ -2020,35 +2020,78 @@
 odif
-ier.setBoolean(currentValue
+yAction.setValueForAction(currentValue);%0A        doAction(modifyAction
 );%0A 
@@ -2768,32 +2768,32 @@
  device%0A     */%0A
+
     public JChec
@@ -2829,1582 +2829,6 @@
 x;%7D%0A
-        %0A    /**%0A     * Method to demonstrate and test the use of this class.  %0A     */%0A//    public static void main(String%5B%5D args) %7B%0A//        %0A//        final etomica.simulation.prototypes.HSMD2D sim = new etomica.simulation.prototypes.HSMD2D();%0A//        Simulation.instance = sim;%0A//        %0A//        //here's the part unique to this class%0A//        //sets up box to toggle atoms between red and blue%0A//        final ColorSchemeByType colorScheme = new ColorSchemeByType();%0A//        sim.display.setColorScheme(colorScheme);%0A//        ModifierBoolean modifier = new ModifierBoolean() %7B%0A//            public void setBoolean(boolean b) %7B%0A//                if(b) ColorSchemeByType.setColor((SpeciesSpheresMono)sim.species, java.awt.Color.blue);%0A//                else ColorSchemeByType.setColor((SpeciesSpheresMono)sim.species, java.awt.Color.red);%0A////                if(b) sim.species.allAtoms(new AtomAction() %7Bpublic void actionPerformed(Atom a) %7Ba.setColor(java.awt.Color.red);%7D%7D);%0A////                else  sim.species.allAtoms(new AtomAction() %7Bpublic void actionPerformed(Atom a) %7Ba.setColor(java.awt.Color.blue);%7D%7D);%0A//                sim.panel().repaint();%0A//            %7D%0A//            public boolean getBoolean() %7Breturn colorScheme.atomColor(sim.box.firstAtom()) == java.awt.Color.blue;%7D%0A//        %7D;%0A//        modifier.setBoolean(true);%0A//        DeviceCheckBox button = new DeviceCheckBox(sim, %22Blue%22, modifier);%0A//        //end of unique part%0A// %0A//        sim.elementCoordinator.go();%0A//        SimulationGraphic.makeAndDisplayFrame(sim);%0A//    %7D%0A//    */%0A
 %7D%0A

make checkbox device actually use Controller