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.Optional;
24  import org.apache.wicket.AttributeModifier;
25  import org.apache.wicket.Component;
26  import org.apache.wicket.MarkupContainer;
27  import org.apache.wicket.ajax.AjaxRequestTarget;
28  import org.apache.wicket.markup.html.WebMarkupContainer;
29  import org.apache.wicket.markup.html.form.Form;
30  import org.apache.wicket.markup.html.form.FormComponent;
31  import org.apache.wicket.markup.html.form.IFormVisitorParticipant;
32  import org.apache.wicket.markup.html.list.Loop;
33  import org.apache.wicket.markup.html.list.LoopItem;
34  import org.apache.wicket.model.PropertyModel;
35  import org.wicketstuff.egrid.column.AbstractEditablePropertyColumn;
36  import org.wicketstuff.egrid.column.EditableCellPanel;
37  import org.wicketstuff.egrid.component.EditableDataTable;
38  import org.wicketstuff.egrid.component.EditableGridSubmitLink;
39  import org.wicketstuff.egrid.toolbar.AbstractEditableGridToolbar;
40  
41  public abstract class AjaxGridBottomToolbar<T, S> extends AbstractEditableGridToolbar {
42  
43      private static final long serialVersionUID = 1L;
44  
45      protected static final String CELL_ID = "cell";
46  
47      protected static final String CELLS_ID = "cells";
48  
49      protected T newRow = null;
50  
51      public AjaxGridBottomToolbar(final EditableDataTable<?, ?> table) {
52          super(table);
53  
54          newRow = newRowInstance();
55  
56          MarkupContainer td = new WebMarkupContainer("td");
57          td.add(new AttributeModifier("colspan", table.getColumns().size() - 1));
58          AddToolBarForm addToolBarForm = new AddToolBarForm("addToolbarForm");
59          td.add(addToolBarForm);
60          add(td);
61          add(newAddButton(addToolBarForm));
62      }
63  
64      protected abstract T newRowInstance();
65  
66      protected abstract void onAdd(AjaxRequestTarget target, T newRow);
67  
68      protected void onError(final AjaxRequestTarget target) {
69      }
70  
71      protected class AddToolBarForm extends Form<T> implements IFormVisitorParticipant {
72  
73          protected static final long serialVersionUID = 1L;
74  
75          public AddToolBarForm(final String id) {
76              super(id);
77              add(newEditorComponents());
78          }
79  
80          @Override
81          public boolean processChildren() {
82              return Optional.ofNullable(getRootForm().findSubmitter()).
83                      map(submitter -> submitter.getForm() == this).
84                      orElse(false);
85          }
86      }
87  
88      protected Component newAddButton(final WebMarkupContainer encapsulatingContainer) {
89          return new EditableGridSubmitLink("add", encapsulatingContainer) {
90  
91              protected static final long serialVersionUID = 1L;
92  
93              @Override
94              protected void onSuccess(final AjaxRequestTarget target) {
95                  onAdd(target, newRow);
96                  newRow = newRowInstance();
97                  target.add(getTable());
98              }
99  
100             @Override
101             protected void onError(final AjaxRequestTarget target) {
102                 AjaxGridBottomToolbar.this.onError(target);
103             }
104         };
105     }
106 
107     protected Loop newEditorComponents() {
108         List<AbstractEditablePropertyColumn<T, S>> columns = getEditableColumns();
109         return new Loop(CELLS_ID, columns.size()) {
110 
111             protected static final long serialVersionUID = 1L;
112 
113             @Override
114             protected void populateItem(final LoopItem item) {
115                 addEditorComponent(item, getEditorColumn(columns, item.getIndex()));
116             }
117         };
118     }
119 
120     protected void addEditorComponent(final LoopItem item, final AbstractEditablePropertyColumn<T, S> toolBarCell) {
121         item.add(newCell(toolBarCell));
122     }
123 
124     @SuppressWarnings("unchecked")
125     protected List<AbstractEditablePropertyColumn<T, S>> getEditableColumns() {
126         List<AbstractEditablePropertyColumn<T, S>> columns = new ArrayList<>();
127         getTable().getColumns().stream().
128                 filter(AbstractEditablePropertyColumn.class::isInstance).
129                 map(AbstractEditablePropertyColumn.class::cast).
130                 forEach(columns::add);
131         return columns;
132     }
133 
134     protected Component newCell(final AbstractEditablePropertyColumn<T, S> editableGridColumn) {
135         EditableCellPanel panel = editableGridColumn.getEditableCellPanel(CELL_ID);
136         FormComponent<?> editorComponent = panel.getEditableComponent();
137         editorComponent.setDefaultModel(new PropertyModel<T>(newRow, editableGridColumn.getPropertyExpression()));
138         return panel;
139     }
140 
141     protected AbstractEditablePropertyColumn<T, S> getEditorColumn(
142             final List<AbstractEditablePropertyColumn<T, S>> editorColumn, final int index) {
143 
144         return editorColumn.get(index);
145     }
146 }