| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 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.ui.client.application.Application; | 
  | 22 |  |  import org.kuali.student.common.ui.client.configurable.mvc.CanProcessValidationResults; | 
  | 23 |  |  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; | 
  | 24 |  |  import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; | 
  | 25 |  |  import org.kuali.student.common.ui.client.configurable.mvc.sections.VerticalSection; | 
  | 26 |  |  import org.kuali.student.common.ui.client.widgets.KSButton; | 
  | 27 |  |  import org.kuali.student.common.ui.client.widgets.KSLabel; | 
  | 28 |  |  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; | 
  | 29 |  |  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent; | 
  | 30 |  |  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; | 
  | 31 |  |  import org.kuali.student.common.ui.client.widgets.search.KSPicker; | 
  | 32 |  |  import org.kuali.student.core.assembly.data.Metadata; | 
  | 33 |  |  import org.kuali.student.core.validation.dto.ValidationResultInfo; | 
  | 34 |  |  import org.kuali.student.core.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 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 |  |   | 
  | 53 |  |   | 
  | 54 |  |   | 
  | 55 |  |   | 
  | 56 |  |   | 
  | 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.add(searchMainPanel); | 
  | 111 | 0 |          this.add(instructions); | 
  | 112 | 0 |          this.addSection(loList); | 
  | 113 | 0 |          } | 
  | 114 |  |   | 
  | 115 |  |           | 
  | 116 |  |   | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |          @Override | 
  | 120 |  |          public void setValue(List<OutlineNode<LOPicker>> value, boolean fireEvents) { | 
  | 121 | 0 |                  setValue(value); | 
  | 122 | 0 |          } | 
  | 123 |  |   | 
  | 124 |  |           | 
  | 125 |  |   | 
  | 126 |  |   | 
  | 127 |  |          @Override | 
  | 128 |  |          public void setValue(List<OutlineNode<LOPicker>> data) { | 
  | 129 | 0 |                  loList.setValue(data); | 
  | 130 | 0 |          } | 
  | 131 |  |   | 
  | 132 |  |           | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |          @Override | 
  | 136 |  |          public List<OutlineNode<LOPicker>> getValue() { | 
  | 137 | 0 |                  return loList.getValue(); | 
  | 138 |  |          } | 
  | 139 |  |   | 
  | 140 |  |           | 
  | 141 |  |   | 
  | 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 |  |   | 
  | 155 |  |   | 
  | 156 |  |          public static String getType() { | 
  | 157 | 0 |                  return type; | 
  | 158 |  |          } | 
  | 159 |  |   | 
  | 160 |  |           | 
  | 161 |  |   | 
  | 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 |  |   | 
  | 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 |  |                                   | 
  | 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 |  |                   | 
  | 261 |  |                   | 
  | 262 |  |                   | 
  | 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 |  |                                   | 
  | 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);  | 
  | 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 |  |  } |