2 * Copyright 2010 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.admin.client.ui;
21 import gr.ebs.gss.admin.client.ui.UsersTable.UserSorter.UserComparator;
22 import gr.ebs.gss.server.domain.dto.StatsDTO;
23 import gr.ebs.gss.server.domain.dto.UserDTO;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.LinkedList;
33 import java.util.List;
37 import com.google.gwt.event.dom.client.ClickEvent;
38 import com.google.gwt.event.dom.client.ClickHandler;
39 import com.google.gwt.gen2.table.client.AbstractColumnDefinition;
40 import com.google.gwt.gen2.table.client.AbstractScrollTable.ScrollPolicy;
41 import com.google.gwt.gen2.table.client.AbstractScrollTable.SortPolicy;
42 import com.google.gwt.gen2.table.client.CachedTableModel;
43 import com.google.gwt.gen2.table.client.CellRenderer;
44 import com.google.gwt.gen2.table.client.ColumnDefinition;
45 import com.google.gwt.gen2.table.client.DefaultRowRenderer;
46 import com.google.gwt.gen2.table.client.DefaultTableDefinition;
47 import com.google.gwt.gen2.table.client.FixedWidthGridBulkRenderer;
48 import com.google.gwt.gen2.table.client.MutableTableModel;
49 import com.google.gwt.gen2.table.client.PagingOptions;
50 import com.google.gwt.gen2.table.client.PagingScrollTable;
51 import com.google.gwt.gen2.table.client.ScrollTable;
52 import com.google.gwt.gen2.table.client.SelectionGrid.SelectionPolicy;
53 import com.google.gwt.gen2.table.client.TableDefinition;
54 import com.google.gwt.gen2.table.client.TableDefinition.AbstractCellView;
55 import com.google.gwt.gen2.table.client.TableModel;
56 import com.google.gwt.gen2.table.client.TableModelHelper.Request;
57 import com.google.gwt.gen2.table.client.TableModelHelper.Response;
58 import com.google.gwt.gen2.table.event.client.RowSelectionEvent;
59 import com.google.gwt.gen2.table.event.client.RowSelectionHandler;
60 import com.google.gwt.gen2.table.event.client.TableEvent.Row;
61 import com.google.gwt.i18n.client.DateTimeFormat;
62 import com.google.gwt.user.client.ui.Button;
63 import com.google.gwt.user.client.ui.CheckBox;
64 import com.google.gwt.user.client.ui.Composite;
65 import com.google.gwt.user.client.ui.DialogBox;
66 import com.google.gwt.user.client.ui.FlexTable;
67 import com.google.gwt.user.client.ui.HTML;
68 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
69 import com.google.gwt.user.client.ui.Label;
70 import com.google.gwt.user.client.ui.VerticalPanel;
77 public class UsersTable extends Composite {
78 private CachedTableModel<UserDTO> cachedTableModel = null;
79 private DefaultTableDefinition<UserDTO> tableDefinition = null;
80 private PagingScrollTable<UserDTO> pagingScrollTable = null;
81 private Label countLabel = new Label("There are no users to display.");
82 private DataSourceTableModel tableModel = null;
84 private VerticalPanel vPanel = new VerticalPanel();
85 private FlexTable flexTable = new FlexTable();
92 pagingScrollTable = createScrollTable();
93 pagingScrollTable.setHeight("200px");
94 pagingScrollTable.setScrollPolicy(ScrollPolicy.DISABLED);
95 PagingOptions pagingOptions = new PagingOptions(pagingScrollTable);
97 flexTable.setWidget(0, 0, pagingScrollTable);
98 flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);
99 flexTable.setWidget(1, 0, pagingOptions);
101 countLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
102 vPanel.add(countLabel);
103 vPanel.add(flexTable);
105 vPanel.setWidth("100%");
106 flexTable.setWidth("100%");
108 super.initWidget(vPanel);
109 showUsers(new ArrayList<UserDTO>());
110 pagingScrollTable.setFooterGenerated(true);
115 * @param newList the list of users to show
117 public void showUsers(List<UserDTO> newList) {
118 countLabel.setText("There are "+ newList.size() + " users.");
119 tableModel.setData(newList);
120 tableModel.setRowCount(newList.size());
121 cachedTableModel.clearCache();
122 cachedTableModel.setRowCount(newList.size());
124 pagingScrollTable.gotoPage(0, true);
126 flexTable.setWidget(1, 0, new HTML());
128 PagingOptions pagingOptions = new PagingOptions(pagingScrollTable);
129 flexTable.setWidget(1, 0, pagingOptions);
133 public UserDTO getUserOnRow(int rowIdx){
134 String id = pagingScrollTable.getDataTable().getHTML(rowIdx, 0);
135 final UserDTO m = tableModel.getUserById(Long.parseLong(id));
140 public UserDTO getSelectedRowObject(RowSelectionEvent event){
141 Set<Row> set = event.getSelectedRows();
142 if(set.size() == 1) {
143 int rowIdx = set.iterator().next().getRowIndex();
144 String id = pagingScrollTable.getDataTable().getHTML(rowIdx, 0);
145 UserDTO m = tableModel.getUserById(Long.parseLong(id));
151 public UserDTO getUser(Long id){
153 return tableModel.getUserById(id);
156 public DialogBox createDialogBox(UserDTO m, StatsDTO s) {
158 // Create a dialog box and set the caption text
159 final DialogBox dialogBox = new DialogBox();
160 dialogBox.setHTML("User Details: "+m.getUsername());
162 // Create a table to layout the content
163 VerticalPanel dialogVPanel = new VerticalPanel();
164 dialogBox.setSize("50%", "50%");
166 ClickHandler cancelHandler = new ClickHandler() {
168 public void onClick(ClickEvent event) {
173 dialogBox.setWidget(dialogVPanel);
174 dialogVPanel.add(new Label("Username: "+m.getUsername()));
175 dialogVPanel.add(new Label("Email: "+m.getEmail()));
176 dialogVPanel.add(new Label("Name: "+m.getName()));
177 if(m.getUserClass()!=null)
178 dialogVPanel.add(new Label("Quota: "+m.getUserClass().getQuotaAsString()));
179 dialogVPanel.add(new Label("File Count: "+s.getFileCount()));
180 dialogVPanel.add(new Label("File Size: "+s.getFileSizeAsString()));
181 dialogVPanel.add(new Label("Quota Left: "+s.getQuotaLeftAsString()));
183 Button close = new Button("Close");
184 close.addClickHandler(cancelHandler);
185 dialogVPanel.add(close);
187 // Return the dialog box
192 * Initializes the scroll table
195 private PagingScrollTable<UserDTO> createScrollTable() {
196 // create our own table model
197 tableModel = new DataSourceTableModel();
198 // add it to cached table model
199 cachedTableModel = createCachedTableModel(tableModel);
201 // create the table definition
202 TableDefinition<UserDTO> tableDef = createTableDefinition();
204 // create the paging scroll table
205 pagingScrollTable = new PagingScrollTable<UserDTO>(cachedTableModel, tableDef);
206 pagingScrollTable.setPageSize(10);
207 pagingScrollTable.setEmptyTableWidget(new HTML("There is no data to display"));
208 pagingScrollTable.getDataTable().setSelectionPolicy(SelectionPolicy.ONE_ROW);
210 FixedWidthGridBulkRenderer<UserDTO> bulkRenderer = new FixedWidthGridBulkRenderer<UserDTO>(pagingScrollTable.getDataTable(), pagingScrollTable);
211 pagingScrollTable.setBulkRenderer(bulkRenderer);
214 pagingScrollTable.setCellPadding(3);
215 pagingScrollTable.setCellSpacing(0);
216 pagingScrollTable.setResizePolicy(ScrollTable.ResizePolicy.FILL_WIDTH);
218 pagingScrollTable.setSortPolicy(SortPolicy.SINGLE_CELL);
220 return pagingScrollTable;
223 public void addRowSelectionHandler(RowSelectionHandler handler){
224 pagingScrollTable.getDataTable().addRowSelectionHandler(handler);
228 * Create the {@link CachedTableModel}
232 private CachedTableModel<UserDTO> createCachedTableModel(DataSourceTableModel aTableModel) {
233 CachedTableModel<UserDTO> tm = new CachedTableModel<UserDTO>(aTableModel);
234 tm.setPreCachedRowCount(20);
235 tm.setPostCachedRowCount(20);
240 private DefaultTableDefinition<UserDTO> createTableDefinition() {
241 tableDefinition = new DefaultTableDefinition<UserDTO>();
243 final String[] rowColors = new String[] { "#FFFFDD", "EEEEEE" };
244 tableDefinition.setRowRenderer(new DefaultRowRenderer<UserDTO>(rowColors));
248 IdColumnDefinition columnDef = new IdColumnDefinition();
249 columnDef.setColumnSortable(true);
250 columnDef.setColumnTruncatable(false);
251 columnDef.setPreferredColumnWidth(35);
252 columnDef.setHeader(0, new HTML("Id"));
253 columnDef.setHeaderCount(1);
254 columnDef.setHeaderTruncatable(false);
255 tableDefinition.addColumnDefinition(columnDef);
258 UserClassColumnDefinition columnDef = new UserClassColumnDefinition();
259 columnDef.setColumnSortable(true);
260 columnDef.setColumnTruncatable(true);
261 columnDef.setHeader(0, new HTML("User Class"));
262 columnDef.setHeaderCount(1);
263 columnDef.setHeaderTruncatable(false);
264 tableDefinition.addColumnDefinition(columnDef);
267 ActiveColumnDefinition columnDef = new ActiveColumnDefinition();
268 columnDef.setColumnSortable(true);
269 columnDef.setColumnTruncatable(true);
270 columnDef.setHeader(0, new HTML("Active"));
271 columnDef.setHeaderCount(1);
272 columnDef.setHeaderTruncatable(false);
273 columnDef.setCellRenderer(new CellRenderer<UserDTO, Boolean>() {
276 public void renderRowValue(UserDTO rowValue, ColumnDefinition<UserDTO, Boolean> aColumnDef, AbstractCellView<UserDTO> view) {
277 CheckBox check = new CheckBox();
278 check.setValue(aColumnDef.getCellValue(rowValue));
279 check.setEnabled(false);
280 view.setWidget(check);
284 tableDefinition.addColumnDefinition(columnDef);
288 UsernameColumnDefinition columnDef = new UsernameColumnDefinition();
289 columnDef.setColumnSortable(true);
290 columnDef.setColumnTruncatable(true);
291 columnDef.setHeader(0, new HTML("Username"));
292 columnDef.setHeaderCount(1);
293 columnDef.setHeaderTruncatable(false);
294 tableDefinition.addColumnDefinition(columnDef);
297 EmailColumnDefinition columnDef = new EmailColumnDefinition();
298 columnDef.setColumnSortable(true);
299 columnDef.setColumnTruncatable(true);
300 columnDef.setHeader(0, new HTML("Email"));
301 columnDef.setHeaderCount(1);
302 columnDef.setHeaderTruncatable(false);
303 tableDefinition.addColumnDefinition(columnDef);
306 FullNameColumnDefinition columnDef = new FullNameColumnDefinition();
307 columnDef.setColumnSortable(true);
308 columnDef.setColumnTruncatable(true);
309 columnDef.setHeader(0, new HTML("Name"));
310 columnDef.setHeaderCount(1);
311 columnDef.setHeaderTruncatable(false);
312 tableDefinition.addColumnDefinition(columnDef);
316 LastLoginColumnDefinition columnDef = new LastLoginColumnDefinition();
317 columnDef.setColumnSortable(true);
318 columnDef.setColumnTruncatable(true);
319 columnDef.setHeader(0, new HTML("Last Login"));
320 columnDef.setHeaderCount(1);
321 columnDef.setHeaderTruncatable(false);
322 tableDefinition.addColumnDefinition(columnDef);
327 return tableDefinition;
331 private class DataSourceTableModel extends MutableTableModel<UserDTO> {
332 private Map<Long, UserDTO> map;
333 private UserSorter sorter = new UserSorter();
334 public void setData(List<UserDTO> list) {
335 // toss the list, index by id in a map.
336 map = new HashMap<Long, UserDTO>(list.size());
337 for(UserDTO m : list)
338 map.put(m.getId(), m);
341 public UserDTO getUserById(long id) {
346 protected boolean onRowInserted(int beforeRow) {
351 protected boolean onRowRemoved(int row) {
356 protected boolean onSetRowValue(int row, UserDTO rowValue) {
362 public void requestRows(
363 final Request request,
364 TableModel.Callback<UserDTO> callback) {
366 callback.onRowsReady(request, new Response<UserDTO>(){
369 public Iterator<UserDTO> getRowValues() {
370 final int col = request.getColumnSortList().getPrimaryColumn();
371 final boolean ascending = request.getColumnSortList().isPrimaryAscending();
373 map = sorter.sort(map, new UserComparator(ascending,0));
375 map = sorter.sort(map, new UserComparator(ascending,col));
376 return map.values().iterator();
383 private final class IdColumnDefinition extends AbstractColumnDefinition<UserDTO, Long> {
385 public Long getCellValue(UserDTO rowValue) {
386 return rowValue.getId();
389 public void setCellValue(UserDTO rowValue, Long cellValue) { }
393 private final class UsernameColumnDefinition extends
394 AbstractColumnDefinition<UserDTO, String> {
396 public String getCellValue(final UserDTO rowValue) {
397 return rowValue.getUsername();
401 public void setCellValue(final UserDTO rowValue, final String cellValue) {}
405 private final class FullNameColumnDefinition extends
406 AbstractColumnDefinition<UserDTO, String> {
408 public String getCellValue(final UserDTO rowValue) {
409 return rowValue.getName();
413 public void setCellValue(final UserDTO rowValue, final String cellValue) {}
417 private final class EmailColumnDefinition extends
418 AbstractColumnDefinition<UserDTO, String> {
420 public String getCellValue(final UserDTO rowValue) {
421 return rowValue.getEmail();
425 public void setCellValue(final UserDTO rowValue, final String cellValue) {}
429 private final class UserClassColumnDefinition extends
430 AbstractColumnDefinition<UserDTO, String> {
432 public String getCellValue(final UserDTO rowValue) {
433 if(rowValue.getUserClass() != null)
434 return rowValue.getUserClass().getName();
439 public void setCellValue(final UserDTO rowValue, final String cellValue) {}
443 private final class ActiveColumnDefinition extends
444 AbstractColumnDefinition<UserDTO, Boolean> {
446 public Boolean getCellValue(final UserDTO rowValue) {
447 if(rowValue.isActive() == null)
449 return rowValue.isActive();
453 public void setCellValue(final UserDTO rowValue, final Boolean cellValue) {
454 rowValue.setActive(cellValue);
459 private final class LastLoginColumnDefinition extends AbstractColumnDefinition<UserDTO, String> {
462 public String getCellValue(final UserDTO rowValue) {
463 if(rowValue.getCurrentLoginDate()==null)
465 return DateTimeFormat.getFormat("dd/MM/yyyy hh:mm:ss tt").format(rowValue.getCurrentLoginDate());
469 public void setCellValue(final UserDTO rowValue, final String cellValue) {}
474 public static class UserSorter {
477 public Map<Long, UserDTO> sort(Map<Long, UserDTO> map, Comparator<UserDTO> comparator) {
478 final List<UserDTO> list = new LinkedList<UserDTO>(map.values());
479 Collections.sort(list, comparator);
480 Map<Long, UserDTO> result = new LinkedHashMap<Long, UserDTO>(list.size());
481 for(UserDTO p : list)
482 result.put(p.getId(), p);
486 public final static class UserComparator implements Comparator<UserDTO> {
488 private final boolean ascending;
489 private int property;
490 public UserComparator(boolean isAscending, int aProperty) {
491 ascending = isAscending;
492 property = aProperty;
496 public int compare(UserDTO m1, UserDTO m2) {
500 return m1.getId().compareTo(m2.getId());
501 return m2.getId().compareTo(m1.getId());
504 return m1.getUserClass().getName().compareTo(m2.getUserClass().getName());
505 return m2.getUserClass().getName().compareTo(m1.getUserClass().getName());
508 return m1.isActive().compareTo(m2.isActive());
509 return m2.isActive().compareTo(m1.isActive());
512 return m1.getUsername().compareTo(m2.getUsername());
513 return m2.getUsername().compareTo(m1.getUsername());
516 return m1.getEmail().compareTo(m2.getEmail());
517 return m2.getEmail().compareTo(m1.getEmail());
520 return m1.getName().compareTo(m2.getName());
521 return m2.getName().compareTo(m1.getName());
523 return new DateComparator(ascending).compare(m2, m1);
527 String s1 = m1.getUsername();
528 String s2 = m2.getUsername();
530 return s1.compareTo(s2);
531 return s2.compareTo(s1);
536 public final static class DateComparator implements Comparator<UserDTO> {
538 private final boolean ascending;
540 public DateComparator(boolean isAscending) {
541 ascending = isAscending;
545 public int compare(UserDTO m1, UserDTO m2) {
546 final Date d1 = m1.getLastLoginDate();
547 final Date d2 = m2.getLastLoginDate();
548 if(d1==null && d2==null)
550 if(d1==null && d2 != null)
552 if(d2==null && d1 != null)
555 return d1.compareTo(d2);
556 return d2.compareTo(d1);