View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons.markup.html.form;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import org.apache.syncope.client.ui.commons.Constants;
24  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
25  import org.apache.wicket.ajax.AjaxRequestTarget;
26  import org.apache.wicket.markup.html.form.ChoiceRenderer;
27  import org.apache.wicket.markup.html.form.DropDownChoice;
28  import org.apache.wicket.markup.html.form.IChoiceRenderer;
29  import org.apache.wicket.model.IModel;
30  import org.apache.wicket.model.ResourceModel;
31  
32  public class AjaxDropDownChoicePanel<T extends Serializable> extends FieldPanel<T> implements Cloneable {
33  
34      private static final long serialVersionUID = -4716376580659196095L;
35  
36      public AjaxDropDownChoicePanel(final String id, final String name, final IModel<T> model) {
37          this(id, name, model, true);
38      }
39  
40      public AjaxDropDownChoicePanel(
41              final String id, final String name, final IModel<T> model, final boolean enableOnBlur) {
42  
43          super(id, name, model);
44  
45          field = new DropDownChoice<>("dropDownChoiceField", model, List.of(), new ChoiceRenderer<>());
46          add(field.setLabel(new ResourceModel(name, name)).setOutputMarkupId(true));
47  
48          if (enableOnBlur) {
49              field.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_BLUR) {
50  
51                  private static final long serialVersionUID = -1107858522700306810L;
52  
53                  @Override
54                  protected void onUpdate(final AjaxRequestTarget target) {
55                      // nothing to do
56                  }
57              }).add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
58  
59                  private static final long serialVersionUID = -1107858522700306810L;
60  
61                  @Override
62                  protected void onUpdate(final AjaxRequestTarget target) {
63                      // nothing to do
64                  }
65              });
66          }
67  
68          setNullValid(true);
69      }
70  
71      @Override
72      public FieldPanel<T> setRequired(final boolean required) {
73          setNullValid(!required);
74          return super.setRequired(required);
75      }
76  
77      @SuppressWarnings("unchecked")
78      public AjaxDropDownChoicePanel<T> setChoiceRenderer(final IChoiceRenderer<T> renderer) {
79          DropDownChoice.class.cast(field).setChoiceRenderer(renderer);
80          return this;
81      }
82  
83      @SuppressWarnings("unchecked")
84      public AjaxDropDownChoicePanel<T> setChoices(final List<T> choices) {
85          DropDownChoice.class.cast(field).setChoices(choices);
86          return this;
87      }
88  
89      @SuppressWarnings("unchecked")
90      public AjaxDropDownChoicePanel<T> setChoices(final IModel<? extends List<? extends T>> choices) {
91          DropDownChoice.class.cast(field).setChoices(choices);
92          return this;
93      }
94  
95      public final AjaxDropDownChoicePanel<T> setNullValid(final boolean validity) {
96          DropDownChoice.class.cast(field).setNullValid(validity);
97          return this;
98      }
99  
100     @Override
101     @SuppressWarnings("unchecked")
102     public FieldPanel<T> clone() {
103         final AjaxDropDownChoicePanel<T> panel = (AjaxDropDownChoicePanel<T>) super.clone();
104         panel.setChoiceRenderer(DropDownChoice.class.cast(field).getChoiceRenderer());
105         panel.setChoices(DropDownChoice.class.cast(field).getChoices());
106         return panel;
107     }
108 }