/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.apache.isis.viewer.wicket.ui.components.scalars.choices;

import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.wicketstuff.select2.Select2MultiChoice;

import org.apache.isis.commons.internal.exceptions._Exceptions;
import org.apache.isis.core.metamodel.object.ManagedObject;
import org.apache.isis.viewer.wicket.ui.components.widgets.formcomponent.CancelHintRequired;
import org.apache.isis.viewer.wicket.ui.components.widgets.formcomponent.FormComponentPanelAbstract;

class ObjectChoiceFormComponent
extends FormComponentPanelAbstract<ManagedObject>
implements CancelHintRequired  {

    private static final long serialVersionUID = 1L;

    private ObjectChoicesSelect2Panel owningPanel;

    public ObjectChoiceFormComponent(final String id, final ObjectChoicesSelect2Panel owningPanel) {
        super(id, owningPanel.scalarModel());
        this.owningPanel = owningPanel;
        setType(ManagedObject.class);
    }

    @Override
    public void validate() {
        if(shouldValidate()) {
            super.validate();
        }
    }

    /**
     * Necessary because {@link FormComponentPanel} overrides this as <tt>true</tt>,
     * whereas we want to
     * report on the state of the underlying {@link org.wicketstuff.select2.Select2Choice} or
     * {@link Select2MultiChoice}.
     */
    @Override
    public boolean checkRequired() {
        return owningPanel.checkSelect2Required();
    }

    /**
     * Since we override {@link #convertInput()}, it is (apparently) enough to
     * just return a value that is suitable for error reporting.
     */
    @Override
    public String getInput() {
        return owningPanel.getTitleForFormComponentInput();
    }

    @Override
    public void convertInput() {
        owningPanel.convertInput();
    }

    @Override
    public void onCancel() {
        // no-op
    }

    // -- HELPER

    /**
     * [ISIS-3070]
     * Skip validation if one of the callers is an {@link AjaxFormComponentUpdatingBehavior},
     * as this validation might be called too early,
     * when the pending value is not yet updated to the user's (new) input.
     * Instead wait for the AjaxFormComponentUpdatingBehavior, which happens later,
     * when the actual pending value is available for validation.
     * <p>
     * This is a hack, for the lack of a better solution yet.
     */
    private boolean shouldValidate() {
        return !_Exceptions
        .streamStackTrace()
        .anyMatch(element->
                    element.getClassName().contains(AjaxFormComponentUpdatingBehavior.class.getSimpleName()));
    }

}

@@ -893,78 +893,8 @@
 s;%0A%0A
-import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;%0A
 impo
@@ -1007,72 +1007,8 @@
 e;%0A%0A
-import org.apache.isis.commons.internal.exceptions._Exceptions;%0A
 impo
@@ -1716,129 +1716,8 @@
  %7D%0A%0A
-    @Override%0A    public void validate() %7B%0A        if(shouldValidate()) %7B%0A            super.validate();%0A        %7D%0A    %7D%0A%0A
     
@@ -2510,746 +2510,6 @@
  %7D%0A%0A
-    // -- HELPER%0A%0A    /**%0A     * %5BISIS-3070%5D%0A     * Skip validation if one of the callers is an %7B@link AjaxFormComponentUpdatingBehavior%7D,%0A     * as this validation might be called too early,%0A     * when the pending value is not yet updated to the user's (new) input.%0A     * Instead wait for the AjaxFormComponentUpdatingBehavior, which happens later,%0A     * when the actual pending value is available for validation.%0A     * %3Cp%3E%0A     * This is a hack, for the lack of a better solution yet.%0A     */%0A    private boolean shouldValidate() %7B%0A        return !_Exceptions%0A        .streamStackTrace()%0A        .anyMatch(element-%3E%0A                    element.getClassName().contains(AjaxFormComponentUpdatingBehavior.class.getSimpleName()));%0A    %7D%0A%0A
 %7D%0A

remove ISIS-3070 hack