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.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.AjaxFallbackDataTable;
27  import org.apache.wicket.Component;
28  import org.apache.wicket.extensions.markup.html.repeater.data.grid.DataGridView;
29  import org.apache.wicket.markup.html.form.CheckGroup;
30  import org.apache.wicket.markup.html.panel.Panel;
31  import org.apache.wicket.markup.repeater.Item;
32  import org.apache.wicket.model.IModel;
33  
34  public abstract class DataTablePanel<T extends Serializable, S> extends Panel {
35  
36      private static final long serialVersionUID = -7264400471578272966L;
37  
38      protected CheckGroup<T> group;
39  
40      protected AjaxFallbackDataTable<T, S> dataTable;
41  
42      protected IModel<Collection<T>> model;
43  
44      public DataTablePanel(final String id) {
45          super(id);
46  
47          model = new IModel<>() {
48  
49              private static final long serialVersionUID = 4886729136344643465L;
50  
51              private final Collection<T> values = new HashSet<>();
52  
53              @Override
54              public Collection<T> getObject() {
55                  // Someone or something call this method to change the model: this is not the right behavior.
56                  // Return a copy of the model object in order to avoid SYNCOPE-465
57                  return new HashSet<>(values);
58              }
59  
60              @Override
61              public void setObject(final Collection<T> selected) {
62                  final Collection<T> all = getGroupModelObjects();
63                  values.removeAll(all);
64                  values.addAll(selected);
65              }
66  
67              @Override
68              public void detach() {
69              }
70          };
71      }
72  
73      public final void setCurrentPage(final long page) {
74          dataTable.setCurrentPage(page);
75      }
76  
77      public final long getRowCount() {
78          return dataTable.getRowCount();
79      }
80  
81      public final long getCurrentPage() {
82          return dataTable.getCurrentPage();
83      }
84  
85      public final long getPageCount() {
86          return dataTable.getPageCount();
87      }
88  
89      public void setItemsPerPage(final int resourcePaginatorRows) {
90          dataTable.setItemsPerPage(resourcePaginatorRows);
91      }
92  
93      protected Collection<T> getGroupModelObjects() {
94          final Set<T> res = new HashSet<>();
95  
96          final Component rows = group.get("dataTable:body:rows");
97          if (rows instanceof DataGridView) {
98              @SuppressWarnings("unchecked")
99              final Iterator<Item<T>> iter = ((DataGridView<T>) rows).getItems();
100 
101             while (iter.hasNext()) {
102                 res.add(iter.next().getModelObject());
103             }
104         }
105         return res;
106     }
107 }