Coverage Report - org.kuali.student.lum.common.client.lo.LOBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
LOBuilder
0%
0/45
0%
0/6
1.429
LOBuilder$1
0%
0/4
N/A
1.429
LOBuilder$LearningObjectiveList
0%
0/67
0%
0/18
1.429
LOBuilder$LearningObjectiveList$1
0%
0/3
N/A
1.429
LOBuilder$LearningObjectiveList$2
0%
0/5
N/A
1.429
LOBuilder$LearningObjectiveList$3
0%
0/4
N/A
1.429
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.lum.common.client.lo;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.student.common.assembly.data.Metadata;
 22  
 import org.kuali.student.common.ui.client.application.Application;
 23  
 import org.kuali.student.common.ui.client.configurable.mvc.CanProcessValidationResults;
 24  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 25  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.Section;
 26  
 import org.kuali.student.common.ui.client.configurable.mvc.sections.VerticalSection;
 27  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 28  
 import org.kuali.student.common.ui.client.widgets.KSLabel;
 29  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 30  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
 31  
 import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
 32  
 import org.kuali.student.common.ui.client.widgets.search.KSPicker;
 33  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 34  
 import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel;
 35  
 import org.kuali.student.lum.common.client.lu.LUUIConstants;
 36  
 
 37  
 import com.google.gwt.event.dom.client.ChangeEvent;
 38  
 import com.google.gwt.event.dom.client.ChangeHandler;
 39  
 import com.google.gwt.event.dom.client.ClickEvent;
 40  
 import com.google.gwt.event.dom.client.ClickHandler;
 41  
 import com.google.gwt.event.logical.shared.ValueChangeEvent;
 42  
 import com.google.gwt.event.logical.shared.ValueChangeHandler;
 43  
 import com.google.gwt.event.shared.HandlerRegistration;
 44  
 import com.google.gwt.user.client.ui.HasValue;
 45  
 import com.google.gwt.user.client.ui.HorizontalPanel;
 46  
 
 47  
 /**
 48  
  * This class manages the users interactions when building/updating Learning
 49  
  * Objectives within the context of managing CLUs. It allows the user to type in
 50  
  * LO text directly or execute a search and select one or more of the returned
 51  
  * LOs.
 52  
  * 
 53  
  * Users can then re-organize LOs on the screen including altering the sequence
 54  
  * and creating sub LOs
 55  
  * 
 56  
  * @author Kuali Student Team
 57  
  * 
 58  
  */
 59  0
 public class LOBuilder extends VerticalSection implements HasValue<List<OutlineNode<LOPicker>>>, CanProcessValidationResults {
 60  
 
 61  
         private static String type;
 62  
         private static String state;
 63  
         private static String repoKey;
 64  
         private static String messageGroup;
 65  
     private static String startOfPath;
 66  0
     private static String endOfPath = "loInfo/desc/plain";
 67  0
     private static String middleOfPath = "loDisplayInfoList";
 68  0
         HorizontalPanel searchMainPanel = new HorizontalPanel();
 69  
         KSPicker searchWindow;
 70  
 
 71  
         LearningObjectiveList loList;
 72  
         KSLabel instructions;
 73  
 
 74  0
         protected LOBuilder() {
 75  0
         }
 76  
 
 77  
     public LOBuilder(String luType, String luState, String luGroup, String loRepoKey, String queryPathStart, final Metadata metadata) {
 78  0
                 super();
 79  
 
 80  0
                 type = luType;
 81  0
                 state = luState;
 82  0
                 repoKey = loRepoKey;
 83  0
                 messageGroup = luGroup;
 84  0
         startOfPath = queryPathStart;
 85  
 
 86  0
                  if(metadata.getInitialLookup() != null) {
 87  0
                          searchWindow = new KSPicker(metadata.getInitialLookup(), metadata.getAdditionalLookups());
 88  0
                           searchWindow.addValuesChangeHandler(new ValueChangeHandler<List<String>>() {
 89  
                                   public void onValueChange(ValueChangeEvent<List<String>> event) {
 90  0
                                           List<String> selection = event.getValue();
 91  0
                                                   loList.addSelectedLOs(selection);
 92  0
                                           }
 93  
                               }
 94  
                           );
 95  0
                           searchMainPanel.add(searchWindow);
 96  
                   }
 97  
                                   
 98  
 
 99  0
                 instructions = new KSLabel(getLabel(LUUIConstants.LO_INSTRUCTIONS_KEY));
 100  
 
 101  0
         loList = new LearningObjectiveList();
 102  
 
 103  0
                 searchMainPanel.addStyleName("KS-LOBuilder-Search-Panel");
 104  
 
 105  0
         loList.addStyleName(LUUIConstants.STYLE_SECTION);
 106  0
         loList.addStyleName(LUUIConstants.STYLE_SECTION_DIVIDER);
 107  
 
 108  0
                 instructions.addStyleName("KS-LOBuilder-Instructions");
 109  
 
 110  0
         this.addWidget(searchMainPanel);
 111  0
         this.addWidget(instructions);
 112  0
         this.addSection(loList);
 113  0
         }
 114  
 
 115  
         /**
 116  
          * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object,
 117  
          *      boolean)
 118  
          */
 119  
         @Override
 120  
         public void setValue(List<OutlineNode<LOPicker>> value, boolean fireEvents) {
 121  0
                 setValue(value);
 122  0
         }
 123  
 
 124  
         /**
 125  
          * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
 126  
          */
 127  
         @Override
 128  
         public void setValue(List<OutlineNode<LOPicker>> data) {
 129  0
                 loList.setValue(data);
 130  0
         }
 131  
 
 132  
         /**
 133  
          * @see com.google.gwt.user.client.ui.HasValue#getValue()
 134  
          */
 135  
         @Override
 136  
         public List<OutlineNode<LOPicker>> getValue() {
 137  0
                 return loList.getValue();
 138  
         }
 139  
 
 140  
         /**
 141  
          * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
 142  
          */
 143  
         @Override
 144  
         public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<OutlineNode<LOPicker>>> handler) {
 145  0
                 return loList.addValueChangeHandler(handler);
 146  
         }
 147  
 
 148  
         private static String getLabel(String labelKey) {
 149  0
                 return Application.getApplicationContext().getUILabel(messageGroup,
 150  
                                 type, state, labelKey);
 151  
         }
 152  
 
 153  
         /**
 154  
          * @return the type
 155  
          */
 156  
         public static String getType() {
 157  0
                 return type;
 158  
         }
 159  
 
 160  
         /**
 161  
          * @return the state
 162  
          */
 163  
         public static String getState() {
 164  0
                 return state;
 165  
         }
 166  
 
 167  
         public static String getRepoKey() {
 168  0
                 return repoKey;
 169  
         }
 170  
 
 171  
         /**
 172  
          * @return the messageGroup
 173  
          */
 174  
         public static String getMessageGroup() {
 175  0
                 return messageGroup;
 176  
         }
 177  
 
 178  0
     public static class LearningObjectiveList extends VerticalSection implements HasValue<List<OutlineNode<LOPicker>>> {
 179  0
                 OutlineNodeModel<LOPicker> outlineModel = new OutlineNodeModel<LOPicker>();
 180  
         KSButton addNew;
 181  
         OutlineManager outlineComposite;
 182  
                 
 183  0
                 SelectionChangeHandler loPickerChangeHandler = new SelectionChangeHandler(){
 184  
                         public void onSelectionChange(SelectionChangeEvent event) {
 185  0
                                 fireChangeEvent();
 186  0
                         }                        
 187  
                 };
 188  
                 
 189  0
         public LearningObjectiveList() {
 190  0
             addNew = new KSButton("Add item", ButtonStyle.SECONDARY, new ClickHandler() {
 191  
                                 public void onClick(ClickEvent event) {
 192  0
                                         setValue(getValue());
 193  0
                                         appendLO("");
 194  0
                                         reDraw();
 195  0
                                 }
 196  
                         });
 197  
                         
 198  0
             addNew.addStyleName("KS-LOBuilder-New");
 199  
 
 200  0
                         outlineModel.addChangeHandler(new ChangeHandler() {
 201  
                                 public void onChange(ChangeEvent event) {
 202  0
                     reDraw();
 203  0
                                         fireChangeEvent();
 204  0
                                 }
 205  
                         });
 206  
 
 207  0
             initEmptyLoList();
 208  0
                 }
 209  
 
 210  
                 protected void initEmptyLoList(){
 211  0
                         List<String> list = new ArrayList<String>();
 212  0
                         list.add("");
 213  0
                         list.add("");
 214  0
                         list.add("");
 215  0
                         list.add("");
 216  0
                         list.add("");
 217  0
                         addSelectedLOs(list);                        
 218  0
                 }
 219  
                 
 220  
                 protected void fireChangeEvent(){
 221  0
                         ValueChangeEvent.fire(this, outlineModel.getOutlineNodes());
 222  0
                 }
 223  
                 
 224  
                 public List<OutlineNode<LOPicker>> getValue() {
 225  0
                         return outlineModel.getOutlineNodes();
 226  
                 }
 227  
 
 228  
                 public void setValue(List<OutlineNode<LOPicker>> value) {
 229  0
                         outlineModel.clearNodes();
 230  0
                         outlineModel.getOutlineNodes().addAll(value);
 231  
 
 232  0
                         if (value == null || value.isEmpty()){
 233  0
                                 initEmptyLoList();
 234  
                         } else {
 235  
                                 //Add selection change handler to LOPickers if not set
 236  0
                                 for (OutlineNode<LOPicker> node:value){
 237  0
                                         LOPicker picker = node.getUserObject();
 238  0
                                         if (!picker.hasChangeHandler()){
 239  0
                                                 picker.addSelectionChangeHandler(loPickerChangeHandler);
 240  
                                         }
 241  0
                                 }
 242  
                                 
 243  0
                                 reDraw();
 244  
                         }
 245  0
                 }
 246  
 
 247  
         private void appendLO(String loValue) {
 248  0
                         OutlineNode<LOPicker> aNode = new OutlineNode<LOPicker>();
 249  0
                         LOPicker newPicker = new LOPicker(messageGroup, type, state,
 250  
                                         repoKey);
 251  
 
 252  0
                         newPicker.addSelectionChangeHandler(loPickerChangeHandler);
 253  0
                         newPicker.setLOText(loValue);
 254  0
                         aNode.setUserObject(newPicker);
 255  0
                         aNode.setModel(outlineModel);
 256  
 
 257  0
                         outlineModel.addOutlineNode(aNode);
 258  0
                 }
 259  
 
 260  
                 // add one or more description by going through existing LO box and
 261  
                 // populating the empty ones
 262  
                 // if not enough empty LO boxes then add new ones
 263  
                 public void addSelectedLOs(List<String> loDescription) {
 264  
 
 265  0
                         List<OutlineNode<LOPicker>> existingLOs = outlineModel.getOutlineNodes();
 266  
 
 267  0
                         int ix = existingLOs.size();
 268  0
                         for (String strValue : loDescription) {
 269  
 
 270  0
                                 boolean foundEmptyBox = false;
 271  0
                                 while (ix > 0) {
 272  0
                                         ix--;
 273  0
                                         if (existingLOs.get(ix).getUserObject().getLOText().trim()
 274  
                                                         .length() == 0) {
 275  0
                                                 existingLOs.get(ix).getUserObject().setLOText(strValue);
 276  0
                                                 foundEmptyBox = true;
 277  0
                                                 break;
 278  
                                         }
 279  
                                 }
 280  
 
 281  
                                 // we didn't find empty LO box so add a new one
 282  0
                                 if (foundEmptyBox == false) {
 283  0
                                         appendLO(strValue);
 284  
                                 }
 285  0
                         }
 286  0
                         reDraw();
 287  0
                 }
 288  
 
 289  
                 private void reDraw() {
 290  0
             if (null != outlineComposite) {
 291  0
                 this.removeSection(outlineComposite);
 292  
             }
 293  0
             this.removeWidget(addNew); // no error if it's not currently there
 294  0
             outlineComposite = new OutlineManager(startOfPath, middleOfPath, endOfPath);
 295  0
             outlineComposite.setValue(outlineModel);
 296  0
             this.addSection(outlineComposite);
 297  0
             this.addWidget(addNew);
 298  0
                         outlineComposite.render();
 299  0
                 }
 300  
 
 301  
                 public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<OutlineNode<LOPicker>>> handler) {
 302  0
                         return addHandler(handler, ValueChangeEvent.getType());
 303  
                 }
 304  
                 
 305  
                 public SelectionChangeHandler getChangeHandlerForLOPicker(){
 306  0
                         return loPickerChangeHandler;
 307  
                 }
 308  
 
 309  
                 @Override
 310  
                 public void setValue(List<OutlineNode<LOPicker>> value,
 311  
                                 boolean fireEvents) {
 312  0
                         setValue(value);
 313  0
                 }
 314  
         }
 315  
 
 316  
     @Override
 317  
     public ErrorLevel processValidationResults(FieldDescriptor fd, List<ValidationResultInfo> results) {
 318  0
         return processValidationResults(fd, results, true);
 319  
     }
 320  
 
 321  
     @Override
 322  
     public ErrorLevel processValidationResults(FieldDescriptor fd, List<ValidationResultInfo> results, boolean clearAllValidation) {
 323  
 
 324  0
         ErrorLevel status = ErrorLevel.OK;
 325  
         
 326  0
         for (Section section : getSections()) {
 327  0
             ErrorLevel level = section.processValidationResults(results, clearAllValidation);
 328  0
             if (level.getLevel() > status.getLevel()) {
 329  0
                 status = level;
 330  
             }
 331  0
         }
 332  0
         return status;
 333  
     }
 334  
 
 335  
 }