Coverage Report - org.kuali.student.common.ui.client.widgets.filter.KSFilterOptions
 
Classes in this File Line Coverage Branch Coverage Complexity
KSFilterOptions
0%
0/53
0%
0/12
2
KSFilterOptions$1
0%
0/6
0%
0/2
2
KSFilterOptions$KSFilterItem
0%
0/13
0%
0/2
2
KSFilterOptions$KSFilterItem$1
0%
0/12
0%
0/2
2
KSFilterOptions$KSFilterItem$1$1
0%
0/9
0%
0/6
2
 
 1  
 package org.kuali.student.common.ui.client.widgets.filter;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 
 8  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 9  
 import org.kuali.student.common.search.dto.SearchRequest;
 10  
 import org.kuali.student.common.search.dto.SearchResult;
 11  
 import org.kuali.student.common.ui.client.application.KSAsyncCallback;
 12  
 import org.kuali.student.common.ui.client.service.SearchRpcService;
 13  
 import org.kuali.student.common.ui.client.service.SearchRpcServiceAsync;
 14  
 import org.kuali.student.common.ui.client.util.SearchUtils;
 15  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 16  
 import org.kuali.student.common.ui.client.widgets.KSCheckBox;
 17  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 18  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 19  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 20  
 import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
 21  
 import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems;
 22  
 import org.kuali.student.common.ui.client.widgets.notification.LoadingDiv;
 23  
 import org.kuali.student.common.ui.client.widgets.search.CollapsablePanel;
 24  
 
 25  
 import com.google.gwt.core.client.GWT;
 26  
 import com.google.gwt.event.dom.client.ClickEvent;
 27  
 import com.google.gwt.event.dom.client.ClickHandler;
 28  
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 29  
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 30  
 import com.google.gwt.user.client.ui.Composite;
 31  
 
 32  
 /**
 33  
  * This widget can be used to display a list of filter items with a list of options.
 34  
  * A selection of filter option will fire a filter select/deselect event which can 
 35  
  * be inspected to update a results widget to reflect the filter option selected.
 36  
  * 
 37  
  */
 38  0
 public class KSFilterOptions extends Composite{
 39  0
     private VerticalFlowPanel filterPanel = new VerticalFlowPanel();
 40  0
     private VerticalFlowPanel filterTitlePanel = new VerticalFlowPanel();
 41  0
     private KSLabel filterTitle = new KSLabel();
 42  0
     private KSLabel filterDescription = new KSLabel();
 43  
 
 44  0
     private VerticalFlowPanel filterContainer = new VerticalFlowPanel();
 45  0
     private LoadingDiv loading = new LoadingDiv();
 46  0
     List<KSFilterItem> filterItems = new ArrayList<KSFilterItem>();
 47  
         
 48  0
     private int itemsIntializing = 0;
 49  0
     private int itemsSelected = 0;
 50  
 
 51  0
     private SearchRpcServiceAsync searchRpcService = GWT.create(SearchRpcService.class);
 52  
     
 53  0
     public KSFilterOptions(List<LookupMetadata> lookups){
 54  0
         filterTitlePanel.add(filterTitle);
 55  0
         filterTitlePanel.add(filterDescription);
 56  0
         filterContainer.add(filterTitlePanel);
 57  0
         filterContainer.add(filterPanel);
 58  
 
 59  0
         filterContainer.addStyleName("KS-Filter-Options-Parent-Container");
 60  0
         filterTitlePanel.addStyleName("KS-Filter-Options-Title-Panel");
 61  
 
 62  0
         filterTitle.addStyleName("KS-Filter-Options-Title-Label");
 63  0
         filterTitle.addStyleName("KS-Indent" + "-1");
 64  
 
 65  0
         filterDescription.addStyleName("KS-Basic-Menu-Desc-Label");
 66  0
         filterDescription.addStyleName("KS-Indent" + "-1");
 67  0
         filterDescription.addStyleName("KS-Indent" + "-1");
 68  
         
 69  0
         KSButton resetButton = new KSButton("Reset", ButtonStyle.DEFAULT_ANCHOR);
 70  0
         filterTitlePanel.add(resetButton);
 71  
         
 72  0
         resetButton.addClickHandler(new ClickHandler(){
 73  
                         @Override
 74  
                         public void onClick(ClickEvent event) {
 75  0
                                 for (KSFilterItem filterItem:filterItems){
 76  0
                                         filterItem.reset();                                        
 77  
                                 }
 78  0
                                 KSFilterOptions.this.fireEvent(new FilterResetEvent());
 79  0
                                 itemsSelected = 0;
 80  0
                         }                
 81  
         });
 82  0
         init(lookups);
 83  0
         this.initWidget(filterContainer);
 84  0
     }
 85  
     
 86  
     protected void init(List<LookupMetadata> lookups){
 87  0
             for (LookupMetadata lookup:lookups){
 88  0
                     itemsIntializing ++;
 89  0
                     KSFilterItem filterItem = new KSFilterItem(lookup);
 90  0
                     filterPanel.add(filterItem);
 91  0
             }            
 92  0
     }
 93  
         
 94  
     /**
 95  
      * Add a filter event handler. A filter event gets fired anytime the user checks or unchecks a filter option.
 96  
      * A filter event will not get fired if an uncheck of filter item causes results in none of the checkboxes
 97  
      * being selected, instead filter reset event will get fired.
 98  
      * 
 99  
      * @param handler
 100  
      */
 101  
     public void addFilterEventHandler(FilterEventHandler handler){
 102  0
             this.addHandler(handler, FilterEvent.TYPE);
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Add a reset handler. A reset event will get fired anytime all the filter checkboxes are not selected, either
 107  
      * via click of Reset or unchecking all checkboxes.
 108  
      * 
 109  
      * @param handler
 110  
      */
 111  
     public void addFilterResetEventHandler(FilterResetEventHandler handler){
 112  0
             this.addHandler(handler, FilterResetEvent.TYPE);
 113  0
     }
 114  
 
 115  
     private class KSFilterItem extends CollapsablePanel{
 116  
             SpanPanel itemContent;
 117  
             String itemKey;
 118  
             
 119  0
             List<KSCheckBox> checkboxes = new ArrayList<KSCheckBox>();
 120  
             
 121  0
             public KSFilterItem (final LookupMetadata lookup){
 122  0
                     itemContent = new SpanPanel();
 123  0
                     this.init(new KSLabel(lookup.getTitle()), itemContent, true, true, ImagePosition.ALIGN_LEFT);
 124  0
                     this.addStyleName("KS-Filter-Item");
 125  0
                     super.linkPanel.addStyleName("KS-Filter-Item-Label");
 126  
                    
 127  0
                     itemKey = lookup.getId();
 128  
                     
 129  0
                     SearchRequest sr = SearchUtils.initializeSearchRequest(lookup);
 130  
                     
 131  0
                     searchRpcService.search(sr, new KSAsyncCallback<SearchResult>(){
 132  
                                 @Override
 133  
                                 public void onSuccess(SearchResult result) {
 134  0
                                         SearchResultListItems items = new SearchResultListItems(result.getRows(), lookup);
 135  0
                                         for (String id:items.getItemIds()){
 136  0
                                 final KSCheckBox checkbox = new KSCheckBox(items.getItemText(id));
 137  0
                                 checkbox.setFormValue(id);
 138  0
                                 checkboxes.add(checkbox);
 139  0
                                 itemContent.add(checkbox);
 140  
                                 
 141  0
                                 checkbox.addValueChangeHandler(new ValueChangeHandler<Boolean>(){
 142  
 
 143  
                                                         @Override
 144  
                                                         public void onValueChange(ValueChangeEvent<Boolean> event) {
 145  0
                                                                 if (checkbox.getValue()){
 146  0
                                                                         itemsSelected++;
 147  
                                                                 } else {
 148  0
                                                                         itemsSelected--;
 149  
                                                                 }
 150  0
                                                                 if (itemsSelected == 0){
 151  0
                                                                         KSFilterOptions.this.fireEvent(new FilterResetEvent());
 152  
                                                                 } else {
 153  0
                                                                         Map<String, List<String>> selectionMap = getSelectionMap();
 154  0
                                                                         KSFilterOptions.this.fireEvent(new FilterEvent(selectionMap, checkbox.getValue(), itemsSelected == 1, itemKey, checkbox.getFormValue()));                                                                        
 155  
                                                                 }
 156  
 
 157  0
                                                         }
 158  
                                         
 159  
                                 });
 160  0
                             }
 161  0
                                         itemsIntializing--;
 162  0
                         filterItems.add(KSFilterItem.this);
 163  0
                                 }
 164  
                     });                    
 165  0
             }
 166  
             
 167  
             /**
 168  
              * Reset all checkboxes for this filter item.
 169  
              * 
 170  
              */
 171  
             public void reset(){
 172  0
                         for (KSCheckBox checkbox:checkboxes){
 173  0
                                 checkbox.setValue(false);
 174  
                         }
 175  0
                 }
 176  
 
 177  
     }
 178  
 
 179  
     /**
 180  
      * Use to reset all filter checkboxes to unselected
 181  
      */
 182  
     public void reset(){
 183  0
                 for (KSFilterItem filterItem:filterItems){
 184  0
                         filterItem.reset();
 185  
                 }
 186  0
                 itemsSelected = 0;
 187  0
     }
 188  
     
 189  
     /** 
 190  
      * @return All the currently selected filter options
 191  
      */
 192  
     protected Map<String, List<String>> getSelectionMap(){
 193  0
             Map<String, List<String>> selectionMap = new HashMap<String, List<String>>();
 194  0
                 for (KSFilterItem filterItem:filterItems){
 195  0
                         List<String> itemValues = new ArrayList<String>();
 196  0
                         for (KSCheckBox itemCheckbox:filterItem.checkboxes){
 197  0
                                 if (itemCheckbox.getValue()){
 198  0
                                         itemValues.add(itemCheckbox.getFormValue());
 199  
                                 }
 200  
                         }
 201  0
                         if (!itemValues.isEmpty()){
 202  0
                                 selectionMap.put(filterItem.itemKey, itemValues);
 203  
                         }
 204  0
                 }
 205  
                 
 206  0
                 return selectionMap;
 207  
     }
 208  
 
 209  
 }