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 de.agilecoders.wicket.extensions.markup.html.bootstrap.form.password.strength.PasswordStrengthBehavior;
22  import java.util.Optional;
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.PasswordTextField;
27  import org.apache.wicket.model.IModel;
28  import org.apache.wicket.model.ResourceModel;
29  
30  public class AjaxPasswordFieldPanel extends FieldPanel<String> {
31  
32      private static final long serialVersionUID = -5490115280336667460L;
33  
34      public AjaxPasswordFieldPanel(
35              final String id,
36              final String name,
37              final IModel<String> model,
38              final boolean enableOnChange) {
39  
40          this(id, name, model, enableOnChange, null);
41      }
42  
43      public AjaxPasswordFieldPanel(
44              final String id,
45              final String name,
46              final IModel<String> model,
47              final boolean enableOnChange,
48              final PasswordStrengthBehavior passwordStrengthBehavior) {
49  
50          super(id, name, model);
51  
52          field = new PasswordTextField("passwordField", model);
53          add(field.setLabel(new ResourceModel(name, name)).setRequired(false).setOutputMarkupId(true));
54          Optional.ofNullable(passwordStrengthBehavior).ifPresent(field::add);
55  
56          if (enableOnChange && !isReadOnly()) {
57              field.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  
69      @Override
70      public FieldPanel<String> addRequiredLabel() {
71          if (!isRequired()) {
72              setRequired(true);
73          }
74  
75          this.isRequiredLabelAdded = true;
76          return this;
77      }
78  }