Coverage Report - org.kuali.student.common.ui.client.widgets.filter.KSFilterOptions
 
Classes in this File Line Coverage Branch Coverage Complexity
KSFilterOptions
0%
0/57
0%
0/12
2.083
KSFilterOptions$1
0%
0/6
0%
0/2
2.083
KSFilterOptions$KSFilterItem
0%
0/13
0%
0/2
2.083
KSFilterOptions$KSFilterItem$1
0%
0/16
0%
0/6
2.083
KSFilterOptions$KSFilterItem$1$1
0%
0/9
0%
0/6
2.083
 
 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  0
     private int itemsIntializing = 0;
 48  0
     private int itemsSelected = 0;
 49  
 
 50  0
     private SearchRpcServiceAsync searchRpcService = GWT.create(SearchRpcService.class);
 51  
     
 52  0
     public KSFilterOptions(List<LookupMetadata> lookups){
 53  0
         init(lookups,null);
 54  0
         this.initWidget(filterContainer);
 55  0
     }
 56  
     
 57  0
     public KSFilterOptions(List<LookupMetadata> lookups, Map<String,Integer> filterCount){
 58  0
         init(lookups,filterCount);
 59  0
         this.initWidget(filterContainer);
 60  0
     }
 61  
     
 62  
     protected void init(List<LookupMetadata> lookups, Map<String,Integer> filterCount){        
 63  0
             filterTitlePanel.add(filterTitle);
 64  0
         filterTitlePanel.add(filterDescription);
 65  0
         filterContainer.add(filterTitlePanel);
 66  0
         filterContainer.add(filterPanel);
 67  
         
 68  0
         filterContainer.addStyleName("KS-Filter-Options-Parent-Container");
 69  0
         filterTitlePanel.addStyleName("KS-Filter-Options-Title-Panel");
 70  
 
 71  0
         filterTitle.addStyleName("KS-Filter-Options-Title-Label");
 72  0
         filterTitle.addStyleName("KS-Indent" + "-1");
 73  
 
 74  0
         filterDescription.addStyleName("KS-Basic-Menu-Desc-Label");
 75  0
         filterDescription.addStyleName("KS-Indent" + "-1");
 76  0
         filterDescription.addStyleName("KS-Indent" + "-1");
 77  
         
 78  0
         KSButton resetButton = new KSButton("Reset", ButtonStyle.DEFAULT_ANCHOR);
 79  0
         filterTitlePanel.add(resetButton);
 80  
         
 81  0
         resetButton.addClickHandler(new ClickHandler(){
 82  
                         @Override
 83  
                         public void onClick(ClickEvent event) {
 84  0
                                 for (KSFilterItem filterItem:filterItems){
 85  0
                                         filterItem.reset();                                        
 86  
                                 }
 87  0
                                 KSFilterOptions.this.fireEvent(new FilterResetEvent());
 88  0
                                 itemsSelected = 0;
 89  0
                         }                
 90  
         });
 91  0
             for (LookupMetadata lookup:lookups){
 92  0
                     itemsIntializing ++;
 93  0
                     KSFilterItem filterItem = new KSFilterItem(lookup,filterCount);
 94  0
                     filterPanel.add(filterItem);
 95  0
             }            
 96  0
     }
 97  
         
 98  
     /**
 99  
      * Add a filter event handler. A filter event gets fired anytime the user checks or unchecks a filter option.
 100  
      * A filter event will not get fired if an uncheck of filter item causes results in none of the checkboxes
 101  
      * being selected, instead filter reset event will get fired.
 102  
      * 
 103  
      * @param handler
 104  
      */
 105  
     public void addFilterEventHandler(FilterEventHandler handler){
 106  0
             this.addHandler(handler, FilterEvent.TYPE);
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Add a reset handler. A reset event will get fired anytime all the filter checkboxes are not selected, either
 111  
      * via click of Reset or unchecking all checkboxes.
 112  
      * 
 113  
      * @param handler
 114  
      */
 115  
     public void addFilterResetEventHandler(FilterResetEventHandler handler){
 116  0
             this.addHandler(handler, FilterResetEvent.TYPE);
 117  0
     }
 118  
 
 119  
     private class KSFilterItem extends CollapsablePanel{
 120  
             SpanPanel itemContent;
 121  
             String itemKey;
 122  0
             List<KSCheckBox> checkboxes = new ArrayList<KSCheckBox>();
 123  0
             public KSFilterItem (final LookupMetadata lookup, final Map<String,Integer> filterCount){
 124  0
                     itemContent = new SpanPanel();
 125  0
                     this.init(new KSLabel(lookup.getTitle()), itemContent, true, true, ImagePosition.ALIGN_LEFT);
 126  0
                     this.addStyleName("KS-Filter-Item");
 127  0
                     super.linkPanel.addStyleName("KS-Filter-Item-Label");
 128  
                    
 129  0
                     itemKey = lookup.getId();
 130  
                     
 131  0
                     SearchRequest sr = SearchUtils.initializeSearchRequest(lookup);
 132  
                     
 133  0
                     searchRpcService.search(sr, new KSAsyncCallback<SearchResult>(){
 134  
                                 @Override
 135  
                                 public void onSuccess(SearchResult result) {
 136  0
                                         SearchResultListItems items = new SearchResultListItems(result.getRows(), lookup);
 137  0
                                         for (String id:items.getItemIds()){                        
 138  
                                                 final KSCheckBox checkbox;
 139  
 
 140  0
                                                 if(filterCount!=null)
 141  
                                                 {
 142  0
                                                         if(filterCount.get(id)==null)
 143  0
                                                                 checkbox = new KSCheckBox(items.getItemText(id)+" (0)");
 144  
                                                         else
 145  0
                                                                 checkbox = new KSCheckBox(items.getItemText(id)+" ("+filterCount.get(id)+")");
 146  
                                                 }
 147  
                                                 else
 148  0
                                                         checkbox = new KSCheckBox(items.getItemText(id));
 149  
 
 150  0
                                                 checkbox.setFormValue(id);
 151  0
                                                 checkboxes.add(checkbox);
 152  0
                                                 itemContent.add(checkbox);
 153  
 
 154  0
                                                 checkbox.addValueChangeHandler(new ValueChangeHandler<Boolean>(){
 155  
 
 156  
                                                         @Override
 157  
                                                         public void onValueChange(ValueChangeEvent<Boolean> event) {
 158  0
                                                                 if (checkbox.getValue()){
 159  0
                                                                         itemsSelected++;
 160  
                                                                 } else {
 161  0
                                                                         itemsSelected--;
 162  
                                                                 }
 163  0
                                                                 if (itemsSelected == 0){
 164  0
                                                                         KSFilterOptions.this.fireEvent(new FilterResetEvent());
 165  
                                                                 } else {
 166  0
                                                                         Map<String, List<String>> selectionMap = getSelectionMap();
 167  0
                                                                         KSFilterOptions.this.fireEvent(new FilterEvent(selectionMap, checkbox.getValue(), itemsSelected == 1, itemKey, checkbox.getFormValue()));                                                                        
 168  
                                                                 }
 169  
 
 170  0
                                                         }
 171  
                                         
 172  
                                 });
 173  0
                             }
 174  0
                                         itemsIntializing--;
 175  0
                         filterItems.add(KSFilterItem.this);
 176  0
                                 }
 177  
                     });                    
 178  0
             }
 179  
             
 180  
             /**
 181  
              * Reset all checkboxes for this filter item.
 182  
              * 
 183  
              */
 184  
             public void reset(){
 185  0
                         for (KSCheckBox checkbox:checkboxes){
 186  0
                                 checkbox.setValue(false);
 187  
                         }
 188  0
                 }
 189  
 
 190  
     }
 191  
 
 192  
     /**
 193  
      * Use to reset all filter checkboxes to unselected
 194  
      */
 195  
     public void reset(){
 196  0
                 for (KSFilterItem filterItem:filterItems){
 197  0
                         filterItem.reset();
 198  
                 }
 199  0
                 itemsSelected = 0;
 200  0
     }
 201  
     
 202  
     /** 
 203  
      * @return All the currently selected filter options
 204  
      */
 205  
     protected Map<String, List<String>> getSelectionMap(){
 206  0
             Map<String, List<String>> selectionMap = new HashMap<String, List<String>>();
 207  0
                 for (KSFilterItem filterItem:filterItems){
 208  0
                         List<String> itemValues = new ArrayList<String>();
 209  0
                         for (KSCheckBox itemCheckbox:filterItem.checkboxes){
 210  0
                                 if (itemCheckbox.getValue()){
 211  0
                                         itemValues.add(itemCheckbox.getFormValue());
 212  
                                 }
 213  
                         }
 214  0
                         if (!itemValues.isEmpty()){
 215  0
                                 selectionMap.put(filterItem.itemKey, itemValues);
 216  
                         }
 217  0
                 }
 218  
                 
 219  0
                 return selectionMap;
 220  
     }
 221  
     
 222  
 }