001package fi.jyu.mit.fxgui;
002
003import java.util.ArrayList;
004
005import javafx.beans.property.BooleanProperty;
006import javafx.beans.property.SimpleBooleanProperty;
007import javafx.beans.value.ChangeListener;
008import javafx.scene.control.RadioButton;
009import javafx.scene.control.ToggleGroup;
010
011/**
012 * Allows multiple radiobuttons to be selected and managed with only 1 component
013 * @author terop
014 * @version 13.1.2017
015 * @param <T> Type to be stored in to this class
016 */
017public class RadioButtonChooser<T> extends BaseBoxChooser<T, RadioButton> {
018
019    private ToggleGroup group = new ToggleGroup();
020    /**
021     * Should multiple selections be allowed
022     */
023    protected BooleanProperty multipleSelectionsAllowed = new SimpleBooleanProperty(false);
024    
025        @Override
026        protected void addChangeListener(RadioButton box, ChangeListener<Boolean> listener) {
027                box.selectedProperty().addListener(listener);
028        }
029
030        @Override
031        protected RadioButton addCorrectComponent(String name, T object) {
032                RadioButton button = new RadioButton(name);
033                if(!multipleSelectionsAllowed.get())
034                    button.setToggleGroup(group);
035                button.setMnemonicParsing(true);
036                return button;
037        }
038
039        
040        
041        @Override
042        protected void removeListener(RadioButton node, ArrayList<ChangeListener<Boolean>> list) {
043                for (ChangeListener<Boolean> changeListener : list) {
044                        node.selectedProperty().removeListener(changeListener);
045                }
046                
047        }
048        
049        /**
050         * Sets if selecting multiple toggles is allowed
051         * @param allowMultiples if multiples are allowed
052         */
053        public void setMultipleSelectionsAllowed(boolean allowMultiples){
054            this.multipleSelectionsAllowed.set(allowMultiples);
055            UpdateComponents(getItems());
056        }
057
058        /**
059         * Gets the multiple selection allowed propertys value
060         * @return if multiple selections are allowed
061         */
062        public boolean getMultipleSelectionsAllowed(){
063        return this.multipleSelectionsAllowed.get();
064    }
065        
066
067        @Override
068        protected boolean isComponentSelected(RadioButton component) {
069                return component.selectedProperty().get();
070        }
071
072
073    @Override
074    protected boolean setComponentSelected(RadioButton component, boolean value) {
075        boolean oldSelect = isComponentSelected(component);
076        component.selectedProperty().set(value);
077        return oldSelect;
078    }
079        
080}