Coverage Report - org.kuali.student.common.ui.client.configurable.mvc.sections.RequiredContainer
 
Classes in this File Line Coverage Branch Coverage Complexity
RequiredContainer
0%
0/67
0%
0/32
2
RequiredContainer$1
0%
0/10
0%
0/2
2
RequiredContainer$ShowAllLink
0%
0/12
N/A
2
 
 1  
 package org.kuali.student.common.ui.client.configurable.mvc.sections;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import org.kuali.student.common.assembly.data.MetadataInterrogator;
 7  
 import org.kuali.student.common.ui.client.application.Application;
 8  
 import org.kuali.student.common.ui.client.application.ApplicationContext;
 9  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 10  
 import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle;
 11  
 import org.kuali.student.common.ui.client.mvc.Callback;
 12  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 13  
 import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayout;
 14  
 import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout;
 15  
 
 16  
 import com.google.gwt.event.dom.client.ClickEvent;
 17  
 import com.google.gwt.event.dom.client.ClickHandler;
 18  
 import com.google.gwt.user.client.ui.Anchor;
 19  
 import com.google.gwt.user.client.ui.Composite;
 20  
 import com.google.gwt.user.client.ui.FlowPanel;
 21  
 import com.google.gwt.user.client.ui.Widget;
 22  
 
 23  
 /**
 24  
  * This class creates a toggle that is used to hide or show all non-required fields.
 25  
  * 
 26  
  * @author SW Genis
 27  
  */
 28  0
 public class RequiredContainer extends WarnContainer {
 29  
 
 30  0
     final static ApplicationContext context = Application.getApplicationContext();
 31  
 
 32  0
     private boolean showAll = false;
 33  
     private Section mainSection;
 34  
 
 35  
     private KSLabel label;
 36  
     private Anchor link;
 37  
 
 38  0
     private List<Callback<Boolean>> callbacks = new ArrayList<Callback<Boolean>>();
 39  0
     private List<FieldDescriptor> excludedDescriptors = new ArrayList<FieldDescriptor>();
 40  
 
 41  
     public RequiredContainer() {
 42  0
         super();
 43  0
         label = new KSLabel(context.getMessage("showingRequiredFields"));
 44  0
         link = new Anchor(context.getMessage("showAllFields"));
 45  
 
 46  0
         this.showWarningLayout(true);
 47  0
         this.addHandler();
 48  
 
 49  0
         this.addWarnWidget(label);
 50  0
         this.addWarnWidget(link);
 51  0
     }
 52  
 
 53  
     private void addHandler() {
 54  
 
 55  0
         link.addClickHandler(new ClickHandler() {
 56  
 
 57  
             @Override
 58  
             public void onClick(ClickEvent event) {
 59  0
                 if (showAll) {
 60  0
                     label.setText(context.getMessage("showingRequiredFields"));
 61  0
                     link.setText(context.getMessage("showAllFields"));
 62  0
                     showAll = false;
 63  
                 } else {
 64  0
                     label.setText(context.getMessage("showingAllFields"));
 65  0
                     link.setText(context.getMessage("showRequiredFields"));
 66  0
                     showAll = true;
 67  
                 }
 68  0
                 processMainSection();
 69  0
             }
 70  
         });
 71  0
     }
 72  
 
 73  
     public boolean isShowAll() {
 74  0
         return showAll;
 75  
     }
 76  
 
 77  
     public void setShowAll(boolean showAll) {
 78  0
         this.showAll = showAll;
 79  0
     }
 80  
 
 81  
     public Section getMainSection() {
 82  0
         return mainSection;
 83  
     }
 84  
 
 85  
     public void setMainSection(Section mainSection) {
 86  0
         this.mainSection = mainSection;
 87  0
         this.processMainSection();
 88  0
     }
 89  
 
 90  
     public void addCallback(Callback<Boolean> callback) {
 91  0
         callbacks.add(callback);
 92  0
     }
 93  
 
 94  
     public List<Callback<Boolean>> getCallbacks() {
 95  0
         return callbacks;
 96  
     }
 97  
 
 98  
     public void registerExclusion(FieldDescriptor descriptor) {
 99  0
         excludedDescriptors.add(descriptor);
 100  0
     }
 101  
 
 102  
     /**
 103  
      * This method loop through all the sections on the main section, process them accordingly and sets the "Show All" link
 104  
      * to visible if there is no components displayed on the section and visa versa.
 105  
      */
 106  
     public void processMainSection() {
 107  
 
 108  0
         for (Section innerSection : mainSection.getSections()) {
 109  0
             boolean hasComponents = processInnerSection(innerSection, showAll);
 110  0
             setLinkVisibility(innerSection, !hasComponents);
 111  0
         }
 112  
 
 113  0
         for (Callback<Boolean> c : callbacks) {
 114  0
             c.exec(showAll);
 115  
         }
 116  
 
 117  0
     }
 118  
 
 119  
     /**
 120  
      * This method loop through all the fields and sections on a section and sets the visibility accordingly.
 121  
      * 
 122  
      * @param section
 123  
      * @param showAll
 124  
      * @return
 125  
      */
 126  
     public boolean processInnerSection(Section section, boolean showAll) {
 127  0
         boolean hasComponents = false;
 128  0
         for (FieldDescriptor field : section.getUnnestedFields()) {
 129  0
             if (processFieldDescriptor(field, showAll)) {
 130  0
                 hasComponents = true;
 131  
             }
 132  
         }
 133  
         
 134  0
         for (Section innerSection : section.getSections()) {
 135  0
             if (processInnerSection(innerSection, showAll)) {
 136  0
                 hasComponents = true;
 137  0
                 setSectionVisibility(innerSection, true);
 138  
             } else {
 139  0
                 setSectionVisibility(innerSection, false);
 140  
             }
 141  
         }
 142  
 
 143  0
         return hasComponents;
 144  
     }
 145  
 
 146  
     /**
 147  
      * This method hides or show the "Show All" link.
 148  
      * 
 149  
      * @param section
 150  
      * @param visibility
 151  
      */
 152  
     private void setLinkVisibility(Section section, boolean visibility) {
 153  0
         if (section instanceof VerticalSection) {
 154  0
             if (((VerticalSection) section).getShowAllLink() != null) {
 155  0
                 ((VerticalSection) section).getShowAllLink().setVisible(visibility);
 156  
             }
 157  
         }
 158  0
     }
 159  
 
 160  
     /**
 161  
      * This method hide or show a section. Used for sections within other sections that does not have any required components
 162  
      * to show.
 163  
      * 
 164  
      * @param section
 165  
      * @param visibility
 166  
      */
 167  
     private void setSectionVisibility(Section section, boolean visibility) {
 168  0
         if (section instanceof VerticalSection) {
 169  0
             FieldLayout layout = ((BaseSection) section).getLayout();
 170  0
             if (layout instanceof VerticalFieldLayout){
 171  0
                 FlowPanel flowPanel = ((VerticalFieldLayout)layout).getVerticalLayout();
 172  0
                 flowPanel.setVisible(visibility);
 173  0
             } else {
 174  0
                 SectionTitle sectionTitle = layout.getLayoutTitle();
 175  0
                 if (sectionTitle != null){
 176  0
                     sectionTitle.setVisible(visibility);
 177  
                 }
 178  
             }
 179  
         }
 180  0
        if(section instanceof CollapsableSection)
 181  0
             ((CollapsableSection) section).getLayout().setVisible(visibility);
 182  0
     }
 183  
 
 184  
     /**
 185  
      * This method check if the component is required or not to set all non-required fields to the required visibility.
 186  
      * Returns true if widget is displayed i.e. required or excluded.
 187  
      * 
 188  
      * @param descriptor
 189  
      * @param showAll
 190  
      * @return boolean
 191  
      */
 192  
     private boolean processFieldDescriptor(FieldDescriptor descriptor, boolean showAll) {
 193  
         // Check if this field should be ignored, return true.
 194  0
         if (descriptor.isIgnoreShowRequired()) {
 195  0
             return true;
 196  
         }
 197  
         // Check if field is required or not.
 198  0
         if (!MetadataInterrogator.isRequired(descriptor.getMetadata()) && (!MetadataInterrogator.isRequiredForNextState(descriptor.getMetadata()))) {
 199  0
             descriptor.getFieldElement().setVisible(showAll);
 200  0
             return showAll;
 201  
         }
 202  0
         return true;
 203  
     }
 204  
 
 205  
     public Composite createShowAllLink(ClickHandler handler) {
 206  0
         return new ShowAllLink(handler);
 207  
     }
 208  
 
 209  
     public class ShowAllLink extends Composite {
 210  0
         private FlowPanel layout = new FlowPanel();
 211  
 
 212  0
         public ShowAllLink(ClickHandler handler) {
 213  0
             layout.addStyleName("ks-message-static-margin");
 214  0
             add(new KSLabel(context.getMessage("requiredFields")));
 215  
 
 216  
             // Link
 217  0
             Anchor link = new Anchor(context.getMessage("allFields"));
 218  0
             link.addClickHandler(handler);
 219  0
             add(link);
 220  
 
 221  0
             this.initWidget(layout);
 222  0
         }
 223  
 
 224  
         public void add(Widget w) {
 225  0
             layout.add(w);
 226  0
             w.getElement().setAttribute("style", "display: inline");
 227  0
         }
 228  
     }
 229  
 }