<commit_before>/*
 * Copyright 2003 - 2016 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.efaps.ui.wicket.components.values;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.efaps.admin.datamodel.ui.UIValue;
import org.efaps.admin.event.EventDefinition;
import org.efaps.admin.event.EventType;
import org.efaps.ui.wicket.behaviors.AjaxFieldUpdateBehavior;
import org.efaps.ui.wicket.models.cell.CellSetValue;
import org.efaps.ui.wicket.models.cell.FieldConfiguration;
import org.efaps.ui.wicket.models.cell.UIFormCellSet;
import org.efaps.ui.wicket.models.field.AbstractUIField;
import org.efaps.ui.wicket.models.objects.DropDownOption;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.CacheReloadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Render a DropDown Field.
 *
 * @author The eFaps Team
 */
public class DropDownField
    extends DropDownChoice<DropDownOption>
    implements IFieldConfig, IFormSubmittingComponent
{

    /**
     * Needed for serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(DropDownField.class);

    /**
     * value of this field.
     */
    private final AbstractUIField cellvalue;

    /**
     * Was the value already converted.
     */
    private boolean converted = false;

    /**
     * Instantiates a new drop down field.
     *
     * @param _wicketId     wicket id for this component
     * @param _model        Model for this component
     * @param _choices    Choices for the dropdowns
     */
    public DropDownField(final String _wicketId,
                         final Model<AbstractUIField> _model,
                         final IModel<Map<Object, Object>> _choices)
    {
        super(_wicketId);
        setOutputMarkupId(true);
        this.cellvalue = _model.getObject();
        final Serializable value = this.cellvalue.getValue().getDbValue();
        if (value != null) {
            setDefaultModel(Model.of(new DropDownOption(String.valueOf(value), null)));
        } else {
            setDefaultModel(new Model<String>());
        }
        setChoices(DropDownField.getSelectChoices(_choices.getObject()));
        setChoiceRenderer(new ChoiceRenderer());
    }

    /**
     * @param _wicketId     wicket id for this component
     * @param _model        Model for this component
     * @param _choices    Choices for the dropdowns
     */
    public DropDownField(final String _wicketId,
                         final Model<AbstractUIField> _model,
                         final List<DropDownOption> _choices)
    {
        super(_wicketId);
        this.cellvalue = _model.getObject();
        for (final DropDownOption choice : _choices) {
            if (choice.isSelected()) {
                setDefaultModel(Model.of(choice));
                break;
            }
        }
        if (getDefaultModel() == null) {
            if (_choices.isEmpty()) {
                setDefaultModel(new Model<String>());
            } else {
                setDefaultModel(Model.of(_choices.get(0)));
            }
        }
        setChoices(_choices);
        setChoiceRenderer(new ChoiceRenderer());
        if (this.cellvalue.getFieldConfiguration().getField().hasEvents(EventType.UI_FIELD_UPDATE)) {
            final List<EventDefinition> events =
                            this.cellvalue.getFieldConfiguration().getField().getEvents(EventType.UI_FIELD_UPDATE);
            String eventName = "change";
            for (final EventDefinition event : events) {
                eventName = event.getProperty("Event") == null ? "change" : event.getProperty("Event");
            }
            add(new AjaxFieldUpdateBehavior(eventName, Model.of(this.cellvalue)));
        }
    }

    @Override
    protected CharSequence getDefaultChoice(final String _selectedValue)
    {
        // do not set a default choice, because the esjp must do that
        return "";
    }
<commit_msg>- Issue #55: DropDown inside a AttributeSet are not set after adding a new line <commit_after>/*
 * Copyright 2003 - 2016 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.efaps.ui.wicket.components.values;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.efaps.admin.datamodel.ui.UIValue;
import org.efaps.admin.event.EventDefinition;
import org.efaps.admin.event.EventType;
import org.efaps.ui.wicket.behaviors.AjaxFieldUpdateBehavior;
import org.efaps.ui.wicket.models.cell.CellSetValue;
import org.efaps.ui.wicket.models.cell.FieldConfiguration;
import org.efaps.ui.wicket.models.cell.UIFormCellSet;
import org.efaps.ui.wicket.models.field.AbstractUIField;
import org.efaps.ui.wicket.models.objects.DropDownOption;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.CacheReloadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Render a DropDown Field.
 *
 * @author The eFaps Team
 */
public class DropDownField
    extends DropDownChoice<DropDownOption>
    implements IFieldConfig, IFormSubmittingComponent
{

    /**
     * Needed for serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(DropDownField.class);

    /**
     * value of this field.
     */
    private final AbstractUIField cellvalue;

    /**
     * Was the value already converted.
     */
    private boolean converted = false;

    /**
     * Instantiates a new drop down field.
     *
     * @param _wicketId     wicket id for this component
     * @param _model        Model for this component
     * @param _choices    Choices for the dropdowns
     */
    public DropDownField(final String _wicketId,
                         final Model<AbstractUIField> _model,
                         final IModel<Map<Object, Object>> _choices)
    {
        super(_wicketId);
        setOutputMarkupId(true);
        this.cellvalue = _model.getObject();
        final Serializable value = this.cellvalue.getValue().getDbValue();
        if (value != null) {
            setDefaultModel(Model.of(new DropDownOption(String.valueOf(value), null)));
        } else {
            setDefaultModel(new Model<String>());
        }
        setChoices(DropDownField.getSelectChoices(_choices.getObject()));
        setChoiceRenderer(new ChoiceRenderer());
    }

    /**
     * @param _wicketId     wicket id for this component
     * @param _model        Model for this component
     * @param _choices    Choices for the dropdowns
     */
    public DropDownField(final String _wicketId,
                         final Model<AbstractUIField> _model,
                         final List<DropDownOption> _choices)
    {
        super(_wicketId);
        this.cellvalue = _model.getObject();
        for (final DropDownOption choice : _choices) {
            if (choice.isSelected()) {
                setDefaultModel(Model.of(choice));
                break;
            }
        }
        if (getDefaultModel() == null) {
            if (_choices.isEmpty()) {
                setDefaultModel(new Model<String>());
            } else {
                // check if value is set before
                for (final DropDownOption choice : _choices) {
                    if (choice.getValue().equals(String.valueOf(_model.getObject().getValue().getDbValue()))) {
                        setDefaultModel(Model.of(choice));
                        break;
                    }
                }
                if (getDefaultModel() == null) {
                    setDefaultModel(Model.of(_choices.get(0)));
                }
            }
        }
        setChoices(_choices);
        setChoiceRenderer(new ChoiceRenderer());
        if (this.cellvalue.getFieldConfiguration().getField().hasEvents(EventType.UI_FIELD_UPDATE)) {
            final List<EventDefinition> events =
                            this.cellvalue.getFieldConfiguration().getField().getEvents(EventType.UI_FIELD_UPDATE);
            String eventName = "change";
            for (final EventDefinition event : events) {
                eventName = event.getProperty("Event") == null ? "change" : event.getProperty("Event");
            }
            add(new AjaxFieldUpdateBehavior(eventName, Model.of(this.cellvalue)));
        }
    }

    @Override
    protected CharSequence getDefaultChoice(final String _selectedValue)
    {
        // do not set a default choice, because the esjp must do that
        return "";
    }

{'fraction_non_alphanumeric': 0.097336745332196, 'fraction_numerical': 0.003222443370296654, 'mean_word_length': 2.5684815691579304, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3229427', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2909, 'n_words': 715}