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.console.panels;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.List;
24  import org.apache.syncope.client.console.commons.ActionTableCheckGroup;
25  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.AjaxFallbackDataTable;
26  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.CheckGroupColumn;
27  import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
28  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
29  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink.ActionType;
30  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
31  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormChoiceComponentUpdatingBehavior;
32  import org.apache.wicket.AttributeModifier;
33  import org.apache.wicket.ajax.AjaxRequestTarget;
34  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
35  import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
36  import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
37  import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
38  import org.apache.wicket.markup.html.WebMarkupContainer;
39  import org.apache.wicket.markup.html.form.Form;
40  import org.apache.wicket.model.ResourceModel;
41  
42  public class ActionDataTablePanel<T extends Serializable, S> extends DataTablePanel<T, S> {
43  
44      private static final long serialVersionUID = -8826989026203543957L;
45  
46      private static final String CANCEL = "cancel";
47  
48      private final Form<T> batchForm;
49  
50      private final ActionsPanel<Serializable> actionPanel;
51  
52      public ActionDataTablePanel(
53              final String id,
54              final List<IColumn<T, S>> columns,
55              final ISortableDataProvider<T, S> dataProvider,
56              final int rowsPerPage) {
57  
58          super(id);
59  
60          batchForm = new Form<>("groupForm");
61          add(batchForm);
62  
63          group = new ActionTableCheckGroup<>("checkgroup", model) {
64  
65              private static final long serialVersionUID = -8667764190925075389L;
66  
67              @Override
68              public boolean isCheckable(final T element) {
69                  return isElementEnabled(element);
70              }
71          };
72          group.add(new IndicatorAjaxFormChoiceComponentUpdatingBehavior() {
73  
74              private static final long serialVersionUID = -151291731388673682L;
75  
76              @Override
77              protected void onUpdate(final AjaxRequestTarget target) {
78                  // triggers AJAX form submit
79              }
80          });
81          batchForm.add(group);
82  
83          columns.add(0, new CheckGroupColumn<>(group));
84          dataTable = new AjaxFallbackDataTable<>("dataTable", columns, dataProvider, rowsPerPage, this);
85          group.add(dataTable);
86  
87          final WebMarkupContainer actionPanelContainer = new WebMarkupContainer("actionPanelContainer");
88          batchForm.add(actionPanelContainer);
89  
90          actionPanel = new ActionsPanel<>("actions", null);
91          actionPanelContainer.add(actionPanel);
92  
93          if (dataTable.getRowCount() == 0) {
94              actionPanelContainer.add(new AttributeModifier("style", "display: none"));
95          }
96  
97          batchForm.add(new IndicatingAjaxButton(CANCEL, new ResourceModel(CANCEL)) {
98  
99              private static final long serialVersionUID = -2341391430136818025L;
100 
101             @Override
102             protected void onSubmit(final AjaxRequestTarget target) {
103                 // ignore
104             }
105         }.setVisible(false).setEnabled(false));
106     }
107 
108     public void addAction(final ActionLink<Serializable> action, final ActionType type, final String entitlements) {
109         actionPanel.add(action, type, entitlements);
110     }
111 
112     public void addCancelButton(final BaseModal<?> modal) {
113         AjaxButton cancel = new IndicatingAjaxButton(CANCEL, new ResourceModel(CANCEL)) {
114 
115             private static final long serialVersionUID = -4804368561204623354L;
116 
117             @Override
118             protected void onSubmit(final AjaxRequestTarget target) {
119                 modal.close(target);
120             }
121         };
122 
123         cancel.setDefaultFormProcessing(false);
124         batchForm.addOrReplace(cancel);
125     }
126 
127     public Collection<T> getModelObject() {
128         return group.getModelObject();
129     }
130 
131     public boolean isElementEnabled(final T element) {
132         return true;
133     }
134 }