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.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.tuple.MutablePair;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.wicket.Component;
28  import org.apache.wicket.ajax.AjaxRequestTarget;
29  import org.apache.wicket.markup.html.basic.Label;
30  import org.apache.wicket.markup.html.panel.Panel;
31  import org.apache.wicket.model.IModel;
32  import org.apache.wicket.model.Model;
33  import org.apache.wicket.model.ResourceModel;
34  import org.wicketstuff.egrid.column.AbstractEditablePropertyColumn;
35  import org.wicketstuff.egrid.column.RequiredEditableTextFieldColumn;
36  import org.wicketstuff.egrid.provider.EditableListDataProvider;
37  
38  public class AjaxGridFieldPanel<K, V, S> extends Panel {
39  
40      private static final long serialVersionUID = 7589570522964677729L;
41  
42      public AjaxGridFieldPanel(final String id, final String name, final IModel<Map<K, V>> model) {
43          super(id, model);
44  
45          add(new Label(AbstractFieldPanel.LABEL, new ResourceModel(name, name)));
46  
47          add(new AjaxGrid<>(
48                  "grid",
49                  getColumns(),
50                  new EditableListDataProvider<>(model.getObject().entrySet().stream().
51                          map(entry -> MutablePair.of(entry.getKey(), entry.getValue())).
52                          collect(Collectors.toList())), 10) {
53  
54              private static final long serialVersionUID = -1315456128897492459L;
55  
56              @Override
57              protected boolean displayHeader() {
58                  return false;
59              }
60  
61              @Override
62              protected void onAdd(final AjaxRequestTarget target, final Pair<K, V> newRow) {
63                  model.getObject().put(newRow.getLeft(), newRow.getRight());
64              }
65  
66              @Override
67              protected void onDelete(final AjaxRequestTarget target, final IModel<Pair<K, V>> rowModel) {
68                  model.getObject().remove(rowModel.getObject().getLeft());
69              }
70  
71              @Override
72              protected void onSave(final AjaxRequestTarget target, final IModel<Pair<K, V>> rowModel) {
73                  model.getObject().put(rowModel.getObject().getLeft(), rowModel.getObject().getRight());
74              }
75          });
76      }
77  
78      public AjaxGridFieldPanel<K, V, S> hideLabel() {
79          Component label = get(AbstractFieldPanel.LABEL);
80          if (label != null) {
81              label.setVisible(false);
82          }
83  
84          return this;
85      }
86  
87      private List<AbstractEditablePropertyColumn<Pair<K, V>, S>> getColumns() {
88          List<AbstractEditablePropertyColumn<Pair<K, V>, S>> columns = new ArrayList<>();
89          columns.add(new RequiredEditableTextFieldColumn<>(Model.of(), "left"));
90          columns.add(new RequiredEditableTextFieldColumn<>(Model.of(), "right"));
91          return columns;
92      }
93  }