Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.sections.SwapSection
 
Classes in this File Line Coverage Branch Coverage Complexity
SwapSection
0%
0/88
0%
0/34
2.263
SwapSection$1
0%
0/4
N/A
2.263
SwapSection$2
0%
0/13
0%
0/10
2.263
 
 1  
 package org.kuali.student.common.ui.client.configurable.mvc.sections;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.HashMap;
 5  
 import java.util.Iterator;
 6  
 import java.util.List;
 7  
 
 8  
 import org.kuali.student.common.ui.client.application.Application;
 9  
 import org.kuali.student.common.ui.client.widgets.dialog.ConfirmationDialog;
 10  
 import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout;
 11  
 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
 12  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
 13  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
 14  
 
 15  
 import com.google.gwt.event.dom.client.ClickEvent;
 16  
 import com.google.gwt.event.dom.client.ClickHandler;
 17  
 
 18  
 /**
 19  
  * A section that contains sections that can be swapped in based on user selection on a KSSelectItemWidgetAbstract
 20  
  * 
 21  
  * 
 22  
  * @author Kuali Student Team
 23  
  *
 24  
  */
 25  0
 public class SwapSection extends BaseSection implements HasSectionDeletion{
 26  
         
 27  0
         private HashMap<String, Section> swapSectionMap = new HashMap<String, Section>();
 28  
         private KSSelectItemWidgetAbstract selectableWidget;
 29  0
         private List<Section> deleted = new ArrayList<Section>();
 30  
         private ConfirmationDialog dialog; 
 31  0
         private boolean showConfirmation = true;
 32  0
         private List<String> lastSelection = new ArrayList<String>();
 33  
         private List<String> deletionParentKeys;
 34  
         private SwapEventHandler swapEventHandler;
 35  
         
 36  
         /**
 37  
          * Constructor for SwapSection, note that the SelectableWidget passed in is not added to the
 38  
          * UI but is instead used as reference to show different sections.  The widget must still be added
 39  
          * to a layout as a field or a widget to show in the UI.  A default confirmation dialog will be used.
 40  
          * @param selectableWidget
 41  
          */
 42  0
         public SwapSection(KSSelectItemWidgetAbstract selectableWidget){
 43  0
                 this.init(selectableWidget);
 44  0
         }
 45  
         
 46  0
         public SwapSection(KSSelectItemWidgetAbstract selectableWidget, ConfirmationDialog dialog){
 47  0
         this.dialog = dialog;
 48  0
                 this.init(selectableWidget);
 49  0
         }
 50  
         
 51  
         private void init(KSSelectItemWidgetAbstract selectableWidget){
 52  0
                 this.selectableWidget = selectableWidget;
 53  
                 
 54  0
                 if(dialog == null){
 55  0
                         dialog = 
 56  
                                 new ConfirmationDialog(Application.getApplicationContext().getMessage("fieldDeletionTitle"),  
 57  
                                                 Application.getApplicationContext().getMessage("fieldDeletionConfirmMessage"));
 58  
                 }
 59  0
                 dialog.getConfirmButton().addClickHandler(new ClickHandler(){
 60  
 
 61  
                         @Override
 62  
                         public void onClick(ClickEvent event) {
 63  0
                                 handleUserSelection();
 64  0
                                 dialog.hide();
 65  0
                         }
 66  
                 });
 67  
                 
 68  0
                 selectableWidget.addSelectionChangeHandler(new SelectionChangeHandler(){
 69  
 
 70  
                         @Override
 71  
                         public void onSelectionChange(SelectionChangeEvent event) {
 72  0
                                 if(event.isUserInitiated() && showConfirmation){
 73  0
                                         if(SwapSection.this.selectableWidget.getSelectedItems().size() < lastSelection.size()){
 74  0
                                                 dialog.show();
 75  
                                         }
 76  0
                                         else if(!SwapSection.this.selectableWidget.getSelectedItems().containsAll(lastSelection)){
 77  
 //                                                List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems();
 78  
 //                                                for(int i = 0; i < selected.size(); i++){
 79  
 //                                                        String key = selected.get(i);
 80  
 //                                                        Section section = swapSectionMap.get(key);
 81  
 //                                                        if(section!=null){
 82  
 //                                                                if(section.getLayout().isVisible()){
 83  
 //                                                                        dialog.show();
 84  
 //                                                                }
 85  
 //                                                        }
 86  
 //                                                }
 87  0
                                                 dialog.show();
 88  
                                         }
 89  
                                         else{
 90  0
                                                 handleUserSelection();
 91  
                                         }
 92  
                                 }
 93  0
                                 else if(event.isUserInitiated()){
 94  0
                                         handleUserSelection();
 95  
                                 }
 96  
                                 else{
 97  0
                                         handleSelection();
 98  
                                 }
 99  0
                                 lastSelection.clear();
 100  0
                                 lastSelection.addAll(SwapSection.this.selectableWidget.getSelectedItems());
 101  0
                         }
 102  
                 });
 103  0
                 layout = new VerticalFieldLayout();
 104  0
                 this.add(layout);
 105  0
         }
 106  
         
 107  
         private void handleUserSelection(){
 108  0
                 List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems();
 109  0
                 for(int i = 0; i < selected.size(); i++){
 110  0
                         String key = selected.get(i);
 111  0
                         showSwappableSection(key);
 112  
                 }
 113  
                 
 114  0
                 Iterator<String> it = swapSectionMap.keySet().iterator();
 115  0
                 while(it.hasNext()){
 116  0
                         String key = it.next();
 117  0
                         if(!selected.contains(key)){
 118  0
                                 removeSwappableSection(key);
 119  
                         }
 120  0
                 }
 121  
                 //SectionUpdateEvent e = new SectionUpdateEvent();
 122  
                 //e.setSection(this);
 123  
                 //LayoutController.findParentLayout(layout).fireApplicationEvent(e);
 124  0
         }
 125  
         
 126  
         /**
 127  
          * This is handled differently than handleUserSelection because it is assumed that the client
 128  
          * is setting the correct values into the widgets, therefore no need to delete sections
 129  
          * (also reduces chance of actually deleting data before it is even shown)
 130  
          */
 131  
         private void handleSelection(){
 132  0
                 List<String> selected  = SwapSection.this.selectableWidget.getSelectedItems();
 133  0
                 for(int i = 0; i < selected.size(); i++){
 134  0
                         String key = selected.get(i);
 135  0
                         showSwappableSection(key);
 136  
                 }
 137  
                 
 138  0
                 Iterator<String> it = swapSectionMap.keySet().iterator();
 139  0
                 while(it.hasNext()){
 140  0
                         String key = it.next();
 141  0
                         if(!selected.contains(key)){
 142  0
                                 removeSwappableSection(key);
 143  
                         }
 144  0
                 }
 145  0
         }
 146  
         
 147  
         private void showSwappableSection(String key){
 148  0
                 Section section = swapSectionMap.get(key);
 149  0
                 if(section != null){
 150  0
                         if(deleted.contains(section)){
 151  0
                                 deleted.remove(section);
 152  
                         }
 153  0
                         if(!section.getLayout().isVisible()){
 154  0
                                 section.enableValidation(true);
 155  0
                                 section.getLayout().setVisible(true);
 156  
                         }
 157  
                 }
 158  0
                 if (swapEventHandler != null){
 159  0
                     swapEventHandler.onShowSwappableSection(key, section);
 160  
                 }
 161  0
         }
 162  
         
 163  
         private void removeSwappableSection(String key){
 164  0
                 Section section = swapSectionMap.get(key);
 165  0
                 if(section != null){
 166  0
                         if(!deleted.contains(section)){
 167  0
                                 deleted.add(section);
 168  
                         }
 169  0
                         if(section.getLayout().isVisible()){
 170  0
                                 section.enableValidation(false);
 171  0
                                 section.getLayout().setVisible(false);
 172  
                         }
 173  
 
 174  
                 }
 175  0
                 if (swapEventHandler != null){
 176  0
             swapEventHandler.onRemoveSwappableSection(key, section);
 177  
         }
 178  0
         }
 179  
         
 180  
         public void enableConfirmation(boolean enable){
 181  0
                 showConfirmation = enable;
 182  0
         }
 183  
         
 184  
         public String addSection(Section section, String swapKey){
 185  0
                 swapSectionMap.put(swapKey, section);
 186  0
                 String key = layout.addLayout(section.getLayout());
 187  0
                 section.getLayout().setVisible(false);
 188  0
                 if(selectableWidget.getSelectedItems().contains(swapKey)){
 189  0
                         handleSelection();
 190  
                 }
 191  0
                 sections.add(section);
 192  0
                 return key;
 193  
         }
 194  
         
 195  
         public String addSection(String key, Section section, String swapKey){
 196  0
                 swapSectionMap.put(swapKey, section);
 197  0
                 section.getLayout().setKey(key);
 198  0
                 String rkey = layout.addLayout(section.getLayout());
 199  0
                 section.getLayout().setVisible(false);
 200  0
                 if(selectableWidget.getSelectedItems().contains(swapKey)){
 201  0
                         handleSelection();
 202  
                 }
 203  0
                 sections.add(section);
 204  0
                 return rkey;
 205  
         }
 206  
         
 207  
         
 208  
         @Override
 209  
         public String addSection(Section section) {
 210  0
                 throw new UnsupportedOperationException("Sections can be added to swappable section only through " +
 211  
                                 "the addSection(Section section, String swapKey) method");
 212  
                 
 213  
         }
 214  
 
 215  
         @Override
 216  
         public String addSection(String key, Section section) {
 217  0
                 throw new UnsupportedOperationException("Sections can be added to swappable section only through " +
 218  
                                 "the addSection(Section section, String swapKey) method");
 219  
         }
 220  
 
 221  
         @Override
 222  
         public List<Section> getDeletedSections() {
 223  0
                 return deleted;
 224  
         }
 225  
 
 226  
     @Override
 227  
     public List<String> getDeletionParentKeys() {
 228  0
         return deletionParentKeys;
 229  
     }
 230  
 
 231  
     /**
 232  
      * deletionParentKeys is optional and is only needed when you want to delete the
 233  
      * entire structure in addition to individual fields with in deleted sections.
 234  
      * 
 235  
      * @see SectionBinding#setModelValue(Section, org.kuali.student.common.ui.client.mvc.DataModel, String)
 236  
      */
 237  
     @Override
 238  
     public void setDeletionParentKey(List<String> deletionParentKeys) {
 239  0
         this.deletionParentKeys = deletionParentKeys;
 240  0
     }
 241  
 
 242  
     public SwapEventHandler getSwapEventHandler() {
 243  0
         return swapEventHandler;
 244  
     }
 245  
 
 246  
     public void setSwapEventHandler(SwapEventHandler swapEventHandler) {
 247  0
         this.swapEventHandler = swapEventHandler;
 248  0
     }
 249  
     
 250  
 }