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.wicket.extensions.markup.html.repeater.data.table;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.client.console.panels.AjaxDataTablePanel;
25  import org.apache.syncope.client.console.wicket.ajax.markup.html.navigation.paging.AjaxDataNavigationToolbar;
26  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLinksTogglePanel;
27  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
28  import org.apache.syncope.client.ui.commons.Constants;
29  import org.apache.wicket.AttributeModifier;
30  import org.apache.wicket.ajax.AjaxEventBehavior;
31  import org.apache.wicket.ajax.AjaxRequestTarget;
32  import org.apache.wicket.extensions.ajax.markup.html.repeater.data.sort.AjaxFallbackOrderByBorder;
33  import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackHeadersToolbar;
34  import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator;
35  import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
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.extensions.markup.html.repeater.data.table.NoRecordsToolbar;
39  import org.apache.wicket.markup.html.WebMarkupContainer;
40  import org.apache.wicket.markup.html.form.Check;
41  import org.apache.wicket.markup.html.form.CheckGroupSelector;
42  import org.apache.wicket.markup.repeater.Item;
43  import org.apache.wicket.markup.repeater.OddEvenItem;
44  import org.apache.wicket.model.IModel;
45  import org.apache.wicket.model.Model;
46  
47  public class AjaxFallbackDataTable<T extends Serializable, S> extends DataTable<T, S> {
48  
49      private static final long serialVersionUID = 6861105496141602937L;
50  
51      private ActionLinksTogglePanel<T> togglePanel;
52  
53      public AjaxFallbackDataTable(
54              final String id,
55              final List<? extends IColumn<T, S>> columns,
56              final ISortableDataProvider<T, S> dataProvider,
57              final int rowsPerPage,
58              final WebMarkupContainer container) {
59  
60          super(id, columns, dataProvider, rowsPerPage);
61  
62          setOutputMarkupId(true);
63          setVersioned(false);
64  
65          togglePanel = getTogglePanel();
66  
67          addTopToolbar(new AjaxFallbackHeadersToolbar<>(this, dataProvider) {
68  
69              private static final long serialVersionUID = 7406306172424359609L;
70  
71              @Override
72              protected WebMarkupContainer newSortableHeader(
73                  final String borderId, final S property, final ISortStateLocator<S> locator) {
74                  return new AjaxFallbackOrderByBorder<>(borderId, property, locator) {
75  
76                      private static final long serialVersionUID = 8261993963983329775L;
77  
78                      @Override
79                      protected void onAjaxClick(final AjaxRequestTarget target) {
80                          if (container != null) {
81                              target.add(container);
82                          }
83                      }
84                  };
85              }
86          });
87  
88          addBottomToolbar(new AjaxFallbackHeadersToolbar<>(this, dataProvider) {
89  
90              private static final long serialVersionUID = 7406306172424359609L;
91  
92              @Override
93              protected WebMarkupContainer newSortableHeader(
94                  final String borderId, final S property, final ISortStateLocator<S> locator) {
95                  return new AjaxFallbackOrderByBorder<>(borderId, property, locator) {
96  
97                      private static final long serialVersionUID = 985887006636879421L;
98  
99                      @Override
100                     protected void onAjaxClick(final AjaxRequestTarget target) {
101                         if (container != null) {
102                             target.add(container);
103                         }
104                     }
105                 };
106             }
107         });
108         addBottomToolbar(new AjaxDataNavigationToolbar(this, container));
109         addBottomToolbar(new NoRecordsToolbar(this));
110     }
111 
112     protected ActionsPanel<T> getActions(final IModel<T> model) {
113         return null;
114     }
115 
116     protected ActionLinksTogglePanel<T> getTogglePanel() {
117         return null;
118     }
119 
120     @Override
121     protected Item<T> newRowItem(final String id, final int index, final IModel<T> model) {
122         final OddEvenItem<T> item = new OddEvenItem<>(id, index, model);
123 
124         if (togglePanel != null) {
125             final ActionsPanel<T> actions = getActions(model);
126 
127             if (actions != null && !actions.isEmpty()) {
128                 item.add(new AttributeModifier("style", "cursor: pointer;"));
129                 item.add(new AjaxEventBehavior(Constants.ON_CLICK) {
130 
131                     private static final long serialVersionUID = -4609215765213990763L;
132 
133                     @Override
134                     protected String findIndicatorId() {
135                         return StringUtils.EMPTY;
136                     }
137 
138                     @Override
139                     protected void onEvent(final AjaxRequestTarget target) {
140                         final String lastFocussedElementId = target.getLastFocusedElementId();
141                         if (lastFocussedElementId == null) {
142                             togglePanel.toggleWithContent(target, getActions(model), model.getObject());
143                         } else {
144                             final AjaxDataTablePanel<?, ?> parent = findParent(AjaxDataTablePanel.class);
145                             final Model<Boolean> isCheck = Model.<Boolean>of(Boolean.FALSE);
146 
147                             parent.visitChildren(CheckGroupSelector.class, (selector, ivisit) -> {
148                                 if (selector.getMarkupId().equalsIgnoreCase(lastFocussedElementId)) {
149                                     isCheck.setObject(Boolean.TRUE);
150                                     ivisit.stop();
151                                 }
152                             });
153 
154                             if (!isCheck.getObject()) {
155                                 parent.visitChildren(Check.class, (check, ivisit) -> {
156                                     if (check.getMarkupId().equalsIgnoreCase(lastFocussedElementId)) {
157                                         isCheck.setObject(Boolean.TRUE);
158                                         ivisit.stop();
159                                     }
160                                 });
161                             }
162 
163                             if (!isCheck.getObject()) {
164                                 togglePanel.toggleWithContent(target, getActions(model), model.getObject());
165                             }
166                         }
167                     }
168                 });
169             }
170         }
171 
172         return item;
173     }
174 }