<commit_before>/*
 * This file is part of NavalPlan
 *
 * Copyright (C) 2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.navalplanner.web.common.components;

import org.apache.commons.lang.BooleanUtils;
import org.navalplanner.business.calendars.entities.Capacity;
import org.navalplanner.business.workingday.EffortDuration;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.common.Util.Getter;
import org.navalplanner.web.common.Util.Setter;
import org.zkoss.zul.api.Checkbox;

/**
 * It configures some ZK components to work together and edit a Capacity object
 *
 * @author <NAME> <<EMAIL>>
 */
public class CapacityPicker {

    public static CapacityPicker workWith(Checkbox checkbox,
            EffortDurationPicker standardEffortPicker,
            EffortDurationPicker extraHoursPicker,
            final Getter<Capacity> getter,
            Setter<Capacity> setter) {
        return new CapacityPicker(checkbox, standardEffortPicker,
                extraHoursPicker, getter.get(), setter);
    }

    private Capacity currentCapacity;

    private final Setter<Capacity> setter;

    private CapacityPicker(Checkbox checkbox,
            EffortDurationPicker standardEffortPicker,
            final EffortDurationPicker extraEffortPicker,
            Capacity initialCapacity,
            Setter<Capacity> setter) {
        this.currentCapacity = initialCapacity;
        this.setter = setter;
        standardEffortPicker.bind(new Getter<EffortDuration>() {

            @Override
            public EffortDuration get() {
                return currentCapacity.getStandardEffort();
            }
        }, new Setter<EffortDuration>() {

            @Override
            public void set(EffortDuration value) {
                updateCapacity(currentCapacity.withStandardEffort(value));
            }
        });
        extraEffortPicker.bind(new Getter<EffortDuration>() {

            @Override
            public EffortDuration get() {
                if (currentCapacity.getAllowedExtraEffort() == null) {
                    return EffortDuration.zero();
                }
                return currentCapacity.getAllowedExtraEffort();
            }
        }, new Setter<EffortDuration>() {

            @Override
            public void set(EffortDuration value) {
                updateCapacity(currentCapacity.extraEffort(value));
            }
        });
        Util.bind(checkbox, new Getter<Boolean>() {

            @Override
            public Boolean get() {
                return currentCapacity.isOverAssignable();
            }
        }, new Setter<Boolean>() {

            @Override
            public void set(Boolean value) {
                updateCapacity(currentCapacity.overAssignable(BooleanUtils
                        .isTrue(value)));
                updateExtraEffortDisability(extraEffortPicker);
            }
        });
        updateExtraEffortDisability(extraEffortPicker);

    }

    private void updateExtraEffortDisability(
            EffortDurationPicker extraHoursPicker) {
        extraHoursPicker.setDisabled(currentCapacity.isOverAssignable());
    }

    private void updateCapacity(Capacity newCapacity) {
        this.currentCapacity = newCapacity;
        this.setter.set(currentCapacity);
    }

}
<commit_msg>Allow CapacityPicker to work without bindings<commit_after>/*
 * This file is part of NavalPlan
 *
 * Copyright (C) 2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.navalplanner.web.common.components;

import org.apache.commons.lang.BooleanUtils;
import org.navalplanner.business.calendars.entities.Capacity;
import org.navalplanner.business.workingday.EffortDuration;
import org.navalplanner.web.common.Util;
import org.navalplanner.web.common.Util.Getter;
import org.navalplanner.web.common.Util.Setter;
import org.zkoss.zul.api.Checkbox;

/**
 * It configures some ZK components to work together and edit a Capacity object
 *
 * @author <NAME> <<EMAIL>>
 */
public class CapacityPicker {

    public static CapacityPicker workWith(Checkbox checkbox,
            EffortDurationPicker standardEffortPicker,
            EffortDurationPicker extraHoursPicker, Getter<Capacity> getter,
            Setter<Capacity> setter) {
        return new CapacityPicker(checkbox, standardEffortPicker,
                extraHoursPicker, getter.get(), setter);
    }

    public static CapacityPicker workWith(Checkbox checkbox,
            EffortDurationPicker standardEffortPicker,
            EffortDurationPicker extraHoursPicker,
            final Capacity initialCapacity) {
        return new CapacityPicker(checkbox, standardEffortPicker,
                extraHoursPicker, initialCapacity, null);
    }

    private Capacity currentCapacity;

    private final Setter<Capacity> setter;

    private final Checkbox overAssignableWithoutLimitCheckbox;

    private final EffortDurationPicker standardEffortPicker;

    private final EffortDurationPicker extraEffortPicker;

    private CapacityPicker(Checkbox overAssignableWithoutLimitCheckbox,
            EffortDurationPicker standardEffortPicker,
            final EffortDurationPicker extraEffortPicker,
            Capacity initialCapacity,
            Setter<Capacity> setter) {
        this.overAssignableWithoutLimitCheckbox = overAssignableWithoutLimitCheckbox;
        this.standardEffortPicker = standardEffortPicker;
        this.extraEffortPicker = extraEffortPicker;
        this.currentCapacity = initialCapacity;
        this.setter = setter;

        standardEffortPicker.bind(new Getter<EffortDuration>() {

            @Override
            public EffortDuration get() {
                return currentCapacity.getStandardEffort();
            }
        }, new Setter<EffortDuration>() {

            @Override
            public void set(EffortDuration value) {
                updateCapacity(currentCapacity.withStandardEffort(value));
            }
        });
        extraEffortPicker.bind(new Getter<EffortDuration>() {

            @Override
            public EffortDuration get() {
                if (currentCapacity.getAllowedExtraEffort() == null) {
                    return EffortDuration.zero();
                }
                return currentCapacity.getAllowedExtraEffort();
            }
        }, new Setter<EffortDuration>() {

            @Override
            public void set(EffortDuration value) {
                updateCapacity(currentCapacity.extraEffort(value));
            }
        });
        Util.bind(overAssignableWithoutLimitCheckbox, new Getter<Boolean>() {

            @Override
            public Boolean get() {
                return currentCapacity.isOverAssignable();
            }
        }, new Setter<Boolean>() {

            @Override
            public void set(Boolean value) {
                updateCapacity(currentCapacity.overAssignable(BooleanUtils
                        .isTrue(value)));
                updateExtraEffortDisability(extraEffortPicker);
            }
        });
        updateExtraEffortDisability(extraEffortPicker);

    }

    private void updateExtraEffortDisability(
            EffortDurationPicker extraHoursPicker) {
        extraHoursPicker.setDisabled(currentCapacity.isOverAssignable());
    }

    private void updateCapacity(Capacity newCapacity) {
        this.currentCapacity = newCapacity;
        if (this.setter != null) {
            this.setter.set(currentCapacity);
        }
    }

    public Capacity getValue() {
        return currentCapacity;
    }

    public void setValue(Capacity capacity) {
        overAssignableWithoutLimitCheckbox.setChecked(capacity
                .isOverAssignable());
        standardEffortPicker.setValue(capacity.getStandardEffort());
        extraEffortPicker.setValue(capacity.getAllowedExtraEffort());
        currentCapacity = capacity;
        updateExtraEffortDisability(extraEffortPicker);
    }

}

{'fraction_non_alphanumeric': 0.0747014115092291, 'fraction_numerical': 0.0010857763300760044, 'mean_word_length': 2.6135739505688504, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2502521', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2325, 'n_words': 586}