View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.users.search;
5   
6   import java.util.List;
7   
8   import org.appfuse.webapp.client.application.Application;
9   import org.appfuse.webapp.client.application.base.activity.AbstractProxySearchActivity;
10  import org.appfuse.webapp.client.application.utils.tables.CustomColumn;
11  import org.appfuse.webapp.client.application.utils.tables.LocalColumnSortHandler;
12  import org.appfuse.webapp.client.proxies.UserProxy;
13  import org.appfuse.webapp.client.proxies.UsersSearchCriteriaProxy;
14  import org.appfuse.webapp.client.requests.UserRequest;
15  
16  import com.google.gwt.event.shared.EventBus;
17  import com.google.gwt.user.cellview.client.Column;
18  import com.google.gwt.user.cellview.client.ColumnSortEvent;
19  import com.google.gwt.user.cellview.client.ColumnSortEvent.Handler;
20  import com.google.gwt.user.cellview.client.ColumnSortList;
21  import com.google.gwt.user.client.ui.AcceptsOneWidget;
22  import com.google.gwt.view.client.HasData;
23  import com.google.gwt.view.client.Range;
24  import com.google.inject.Inject;
25  import com.google.web.bindery.requestfactory.shared.Request;
26  import com.google.web.bindery.requestfactory.shared.RequestContext;
27  
28  /**
29   * @author ivangsa
30   *
31   */
32  public class UsersSearchActivity extends AbstractProxySearchActivity<UserProxy, UsersSearchCriteriaProxy> {
33  
34      private final UsersSearchView view;
35      private Handler sortHandler;
36  
37      @Inject
38      public UsersSearchActivity(final Application application, final UsersSearchView view) {
39          super(application, view, UsersSearchCriteriaProxy.class);
40          this.view = view;
41          setTitle(i18n.userList_title());
42      }
43  
44      @Override
45      public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
46          view.setDelegate(this);
47          // Configure local/remote sorting
48          // sortHandler = createLocalColumnSortHandler(view.asHasData());
49          sortHandler = new ColumnSortEvent.AsyncHandler(view.asHasData());
50          view.addColumnSortHandler(sortHandler);
51  
52          super.start(panel, eventBus);
53      }
54  
55      /**
56       * @param hasData
57       */
58      private Handler createLocalColumnSortHandler(final HasData hasData) {
59          return new LocalColumnSortHandler<UserProxy>(hasData) {
60              @Override
61              public List<UserProxy> getList() {
62                  return (List<UserProxy>) hasData.getVisibleItems();
63              }
64          };
65      }
66  
67      private String getPropertyNameForColumn(final Column column) {
68          if (column instanceof CustomColumn) {
69              return ((CustomColumn) column).getPropertyName();
70          }
71          return null;
72      }
73  
74      @Override
75      protected RequestContext createRequestContext() {
76          return requests.userRequest();
77      }
78  
79      @Override
80      protected Request<Long> createCountRequest(final RequestContext requestContext, final UsersSearchCriteriaProxy searchCriteria) {
81          return ((UserRequest) requestContext).countUsers(searchCriteria);
82      }
83  
84      @Override
85      protected Request<List<UserProxy>> createSearchRequest(
86              final RequestContext requestContext, final UsersSearchCriteriaProxy searchCriteria,
87              final Range range, final ColumnSortList columnSortList) {
88  
89          String sortProperty = null;
90          boolean ascending = true;
91          if (columnSortList.size() > 0) {
92              final Column sortColumn = columnSortList.get(0).getColumn();
93              sortProperty = getPropertyNameForColumn(sortColumn);
94              ascending = columnSortList.get(0).isAscending();
95          }
96  
97          return ((UserRequest) requestContext).searchUsers(searchCriteria,
98                  range.getStart(), range.getLength(),
99                  sortProperty, ascending);
100     }
101 
102     @Override
103     public void onStop() {
104         // XXX view.removeColumnSortHandler(sortHandle);
105         sortHandler = null;
106         super.onStop();
107     }
108 }