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.enduser.markup.html.form;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
24  import org.apache.syncope.client.ui.commons.Constants;
25  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
26  import org.apache.syncope.client.ui.commons.markup.html.form.AbstractMultiPanel;
27  import org.apache.syncope.client.ui.commons.markup.html.form.FieldPanel;
28  import org.apache.wicket.ajax.AjaxRequestTarget;
29  import org.apache.wicket.markup.html.list.ListItem;
30  import org.apache.wicket.markup.html.panel.Panel;
31  import org.apache.wicket.model.IModel;
32  
33  public abstract class MultiFieldPanel<E extends Serializable> extends AbstractMultiPanel<E> {
34  
35      private static final long serialVersionUID = -6322397761456513324L;
36  
37      private MultiFieldPanel(
38              final String id,
39              final String name,
40              final IModel<List<E>> model) {
41  
42          super(id, name, model);
43      }
44  
45      public static class Builder<E extends Serializable> implements Serializable {
46  
47          private static final long serialVersionUID = -5304396077613727937L;
48  
49          private final IModel<List<E>> model;
50  
51          private boolean eventTemplate = false;
52  
53          public Builder(final IModel<List<E>> model) {
54              this.model = model;
55          }
56  
57          /**
58           * Set on_change event in order to send MultiValueSelectorEvent to page.
59           *
60           * @param eventTemplate whether this is an event template
61           * @return this instance, for fluent building
62           */
63          public Builder<E> setEventTemplate(final boolean eventTemplate) {
64              this.eventTemplate = eventTemplate;
65              return this;
66          }
67  
68          /**
69           * Default model object instance.
70           *
71           * @return default model object instance.
72           */
73          protected E newModelObject() {
74              return null;
75          }
76  
77          public MultiFieldPanel<E> build(final String id, final String name, final FieldPanel<E> panelTemplate) {
78              return new MultiFieldPanel<>(id, name, model) {
79  
80                  private static final long serialVersionUID = 6600411297376841521L;
81  
82                  @Override
83                  protected E newModelObject() {
84                      return Builder.this.newModelObject();
85                  }
86  
87                  @Override
88                  protected FieldPanel<? extends Serializable> getItemPanel(final ListItem<E> item) {
89                      final FieldPanel<? extends Serializable> fieldPanel = panelTemplate.clone();
90                      fieldPanel.setIndex(item.getIndex());
91                      fieldPanel.setNewModel(item);
92                      fieldPanel.settingsDependingComponents();
93                      fieldPanel.hideLabel();
94  
95                      if (eventTemplate) {
96                          fieldPanel.getField().add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
97  
98                              private static final long serialVersionUID = -1107858522700306810L;
99  
100                             @Override
101                             protected void onUpdate(final AjaxRequestTarget target) {
102                             }
103                         });
104                     }
105 
106                     return fieldPanel;
107                 }
108 
109                 @Override
110                 protected void clearInput(final Panel panel) {
111                     FieldPanel.class.cast(panel).getField().clearInput();
112                 }
113 
114                 @Override
115                 protected void sendError(final String message) {
116                     SyncopeEnduserSession.get().error(getString(Constants.OPERATION_ERROR));
117                 }
118             };
119         }
120     }
121 }