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