| 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.Arrays; | 
  | 20 |  |  import java.util.HashMap; | 
  | 21 |  |  import java.util.List; | 
  | 22 |  |  import java.util.Map; | 
  | 23 |  |   | 
  | 24 |  |  import org.kuali.student.common.assembly.data.Data; | 
  | 25 |  |  import org.kuali.student.common.assembly.data.Data.DataValue; | 
  | 26 |  |  import org.kuali.student.common.ui.client.application.Application; | 
  | 27 |  |  import org.kuali.student.common.ui.client.application.KSAsyncCallback; | 
  | 28 |  |  import org.kuali.student.common.ui.client.configurable.mvc.SectionTitle; | 
  | 29 |  |  import org.kuali.student.common.ui.client.mvc.Callback; | 
  | 30 |  |  import org.kuali.student.common.ui.client.service.DataSaveResult; | 
  | 31 |  |  import org.kuali.student.common.ui.client.util.DebugIdUtils; | 
  | 32 |  |  import org.kuali.student.common.ui.client.util.UtilConstants; | 
  | 33 |  |  import org.kuali.student.common.ui.client.widgets.DataHelper; | 
  | 34 |  |  import org.kuali.student.common.ui.client.widgets.KSButton; | 
  | 35 |  |  import org.kuali.student.common.ui.client.widgets.KSDropDown; | 
  | 36 |  |  import org.kuali.student.common.ui.client.widgets.KSItemLabel; | 
  | 37 |  |  import org.kuali.student.common.ui.client.widgets.KSLabel; | 
  | 38 |  |  import org.kuali.student.common.ui.client.widgets.KSLightBox; | 
  | 39 |  |  import org.kuali.student.common.ui.client.widgets.KSTextBox; | 
  | 40 |  |  import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle; | 
  | 41 |  |  import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonGroup; | 
  | 42 |  |  import org.kuali.student.common.ui.client.widgets.buttongroups.ButtonEnumerations.ButtonEnum; | 
  | 43 |  |  import org.kuali.student.common.ui.client.widgets.buttonlayout.ButtonRow; | 
  | 44 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; | 
  | 45 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.LabelPanel; | 
  | 46 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; | 
  | 47 |  |  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.FieldLayoutComponent; | 
  | 48 |  |  import org.kuali.student.common.ui.client.widgets.focus.FocusGroup; | 
  | 49 |  |  import org.kuali.student.common.ui.client.widgets.list.ListItems; | 
  | 50 |  |  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler; | 
  | 51 |  |  import org.kuali.student.common.ui.client.widgets.notification.KSNotification; | 
  | 52 |  |  import org.kuali.student.common.ui.client.widgets.notification.KSNotifier; | 
  | 53 |  |  import org.kuali.student.common.ui.client.widgets.progress.BlockingTask; | 
  | 54 |  |  import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator; | 
  | 55 |  |  import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox; | 
  | 56 |  |  import org.kuali.student.common.ui.client.widgets.suggestbox.SearchSuggestOracle; | 
  | 57 |  |  import org.kuali.student.common.ui.client.widgets.suggestbox.SuggestPicker; | 
  | 58 |  |  import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcService; | 
  | 59 |  |  import org.kuali.student.lum.common.client.lo.rpc.LoCategoryRpcServiceAsync; | 
  | 60 |  |  import org.kuali.student.lum.common.client.lu.LUUIConstants; | 
  | 61 |  |  import org.kuali.student.lum.lo.dto.LoCategoryInfo; | 
  | 62 |  |  import org.kuali.student.lum.lo.dto.LoCategoryTypeInfo; | 
  | 63 |  |   | 
  | 64 |  |  import com.google.gwt.core.client.GWT; | 
  | 65 |  |  import com.google.gwt.event.dom.client.BlurHandler; | 
  | 66 |  |  import com.google.gwt.event.dom.client.ClickEvent; | 
  | 67 |  |  import com.google.gwt.event.dom.client.ClickHandler; | 
  | 68 |  |  import com.google.gwt.event.dom.client.FocusHandler; | 
  | 69 |  |  import com.google.gwt.event.logical.shared.CloseEvent; | 
  | 70 |  |  import com.google.gwt.event.logical.shared.CloseHandler; | 
  | 71 |  |  import com.google.gwt.event.logical.shared.ValueChangeEvent; | 
  | 72 |  |  import com.google.gwt.event.logical.shared.ValueChangeHandler; | 
  | 73 |  |  import com.google.gwt.event.shared.GwtEvent; | 
  | 74 |  |  import com.google.gwt.event.shared.HandlerRegistration; | 
  | 75 |  |  import com.google.gwt.user.client.Window; | 
  | 76 |  |  import com.google.gwt.user.client.ui.Anchor; | 
  | 77 |  |  import com.google.gwt.user.client.ui.Composite; | 
  | 78 |  |  import com.google.gwt.user.client.ui.FlexTable; | 
  | 79 |  |  import com.google.gwt.user.client.ui.FlowPanel; | 
  | 80 |  |  import com.google.gwt.user.client.ui.HTMLPanel; | 
  | 81 |  |  import com.google.gwt.user.client.ui.HasValue; | 
  | 82 |  |  import com.google.gwt.user.client.ui.HorizontalPanel; | 
  | 83 |  |  import com.google.gwt.user.client.ui.VerticalPanel; | 
  | 84 |  |   | 
  | 85 |  |   | 
  | 86 |  |   | 
  | 87 |  |   | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 | 0 |  public class LOCategoryBuilder extends Composite implements HasValue<List<LoCategoryInfo>> {         | 
  | 93 |  |   | 
  | 94 |  |      private String type; | 
  | 95 |  |      private String state; | 
  | 96 |  |      private String repoKey; | 
  | 97 |  |      private String messageGroup; | 
  | 98 |  |   | 
  | 99 |  |      private LoCategoryRpcServiceAsync loCatRpcServiceAsync; | 
  | 100 |  |      private LOCategoryPicker picker; | 
  | 101 | 0 |      private AbbrButton help = new AbbrButton(AbbrButtonType.HELP); | 
  | 102 |  |   | 
  | 103 |  |      LOCategoryListNew categoryList; | 
  | 104 |  |      Map<String, LoCategoryTypeInfo> categoryTypeMap; | 
  | 105 |  |   | 
  | 106 | 0 |      VerticalPanel root = new VerticalPanel(); | 
  | 107 |  |   | 
  | 108 | 0 |      private KSButton addButton = new KSButton("Add", ButtonStyle.SECONDARY); | 
  | 109 |  |   | 
  | 110 |  |      private KSLightBox createCategoryWindow; | 
  | 111 |  |      Anchor browseCategoryLink; | 
  | 112 | 0 |      private final BlockingTask saving = new BlockingTask("Saving"); | 
  | 113 |  |   | 
  | 114 |  |      public LOCategoryBuilder(String messageGroup, String type, String state, String loRepoKey) { | 
  | 115 | 0 |          super(); | 
  | 116 |  |   | 
  | 117 | 0 |          this.type = type; | 
  | 118 | 0 |          this.state = state; | 
  | 119 | 0 |          this.repoKey = loRepoKey; | 
  | 120 | 0 |          this.messageGroup = messageGroup; | 
  | 121 |  |   | 
  | 122 | 0 |          loCatRpcServiceAsync = GWT.create(LoCategoryRpcService.class); | 
  | 123 | 0 |          picker = new LOCategoryPicker(); | 
  | 124 | 0 |          categoryList = new LOCategoryListNew(); | 
  | 125 |  |   | 
  | 126 | 0 |          initWidget(root); | 
  | 127 |  |   | 
  | 128 | 0 |          final FlowPanel selectedPanel = new FlowPanel(); | 
  | 129 | 0 |          selectedPanel.setStyleName("KS-LOSelectedCategories"); | 
  | 130 | 0 |          addButton.addClickHandler(new ClickHandler() { | 
  | 131 |  |              @Override | 
  | 132 |  |              public void onClick(ClickEvent event) { | 
  | 133 | 0 |                  addEnteredCategory(); | 
  | 134 | 0 |              } | 
  | 135 |  |          }); | 
  | 136 | 0 |          browseCategoryLink = new Anchor(getLabelText(LUUIConstants.LO_CATEGORY_BROWSE_LABEL_KEY)); | 
  | 137 | 0 |          browseCategoryLink.addClickHandler(new ClickHandler() { | 
  | 138 |  |              @Override | 
  | 139 |  |              public void onClick(ClickEvent event) { | 
  | 140 |  |                  | 
  | 141 | 0 |                  List<LoCategoryInfo> categoriesInPicker = categoryList.getValue(); | 
  | 142 |  |                   | 
  | 143 |  |   | 
  | 144 |  |   | 
  | 145 | 0 |                  final CategoryManagement categoryManagement = new CategoryManagement(true, true, categoriesInPicker);         | 
  | 146 | 0 |                  categoryManagement.setDeleteButtonEnabled(false); | 
  | 147 | 0 |                  categoryManagement.setInsertButtonEnabled(false); | 
  | 148 | 0 |                  categoryManagement.setUpdateButtonEnabled(false); | 
  | 149 |  |   | 
  | 150 | 0 |                  final KSLightBox pop = new KSLightBox(); | 
  | 151 | 0 |                  pop.setSize(750, 600); | 
  | 152 | 0 |                  KSButton addButton = new KSButton("Add"); | 
  | 153 | 0 |                  KSButton cancelButton = new KSButton("Cancel", ButtonStyle.ANCHOR_LARGE_CENTERED); | 
  | 154 |  |   | 
  | 155 | 0 |                  pop.addButton(addButton); | 
  | 156 | 0 |                  pop.addButton(cancelButton); | 
  | 157 |  |   | 
  | 158 | 0 |                  pop.setNonCaptionHeader(SectionTitle.generateH2Title(getLabelText(LUUIConstants.LO_CATEGORY_BROWSE_POPUP_LABEL_KEY))); | 
  | 159 | 0 |                  FlowPanel mainPanel = new FlowPanel(); | 
  | 160 | 0 |                  mainPanel.add(categoryManagement); | 
  | 161 |  |   | 
  | 162 | 0 |                  addButton.addClickHandler(new ClickHandler() { | 
  | 163 |  |                      @Override | 
  | 164 |  |                      public void onClick(ClickEvent event) { | 
  | 165 | 0 |                          List<LoCategoryInfo> list = categoryManagement.getSelectedCategoryList(); | 
  | 166 | 0 |                          for (LoCategoryInfo info : list) { | 
  | 167 | 0 |                              addCategory(info);         | 
  | 168 |  |                          } | 
  | 169 | 0 |                          pop.hide(); | 
  | 170 | 0 |                      } | 
  | 171 |  |                  }); | 
  | 172 |  |   | 
  | 173 | 0 |                  cancelButton.addClickHandler(new ClickHandler() { | 
  | 174 |  |                      @Override | 
  | 175 |  |                      public void onClick(ClickEvent event) { | 
  | 176 | 0 |                          pop.hide(); | 
  | 177 | 0 |                      } | 
  | 178 |  |                  }); | 
  | 179 |  |   | 
  | 180 |  |   | 
  | 181 | 0 |                  pop.setWidget(mainPanel); | 
  | 182 | 0 |                  pop.show(); | 
  | 183 |  |   | 
  | 184 |  |   | 
  | 185 | 0 |              } | 
  | 186 |  |          }); | 
  | 187 |  |   | 
  | 188 | 0 |          VerticalPanel main = new VerticalPanel(); | 
  | 189 | 0 |          HorizontalPanel suggestPanel = new HorizontalPanel(); | 
  | 190 | 0 |          suggestPanel.add(picker); | 
  | 191 | 0 |          suggestPanel.add(addButton); | 
  | 192 |  |   | 
  | 193 | 0 |          VerticalPanel suggestAndBrowsePanel = new VerticalPanel(); | 
  | 194 | 0 |          suggestAndBrowsePanel.add(suggestPanel); | 
  | 195 | 0 |          suggestAndBrowsePanel.add(browseCategoryLink); | 
  | 196 |  |   | 
  | 197 |  |   | 
  | 198 | 0 |          selectedPanel.add(categoryList); | 
  | 199 |  |   | 
  | 200 | 0 |          String fieldHTMLId = HTMLPanel.createUniqueId(); | 
  | 201 | 0 |          String title = getLabelText(LUUIConstants.LO_CATEGORY_KEY); | 
  | 202 | 0 |          String helpText = getLabelText(LUUIConstants.LO_CATEGORY_KEY + FieldLayoutComponent.HELP_MESSAGE_KEY); | 
  | 203 | 0 |          LabelPanel fieldTitle = new LabelPanel(title, fieldHTMLId); | 
  | 204 |  |   | 
  | 205 | 0 |          if (helpText != null) { | 
  | 206 | 0 |              setHelp(helpText); | 
  | 207 |  |          } else { | 
  | 208 | 0 |              help.setVisible(false); | 
  | 209 |  |          } | 
  | 210 |  |   | 
  | 211 | 0 |          fieldTitle.add(help); | 
  | 212 | 0 |          main.add(fieldTitle); | 
  | 213 | 0 |          main.add(suggestAndBrowsePanel); | 
  | 214 | 0 |          main.add(selectedPanel); | 
  | 215 | 0 |          root.add(main); | 
  | 216 |  |   | 
  | 217 | 0 |          main.addStyleName("KS-LOCategoryPicker"); | 
  | 218 | 0 |          addButton.addStyleName("KS-LOCategoryPicker-Button"); | 
  | 219 |  |   | 
  | 220 | 0 |      } | 
  | 221 |  |   | 
  | 222 |  |      public void setHelp(final String html) { | 
  | 223 | 0 |          if (html != null && !html.trim().equals("")) { | 
  | 224 | 0 |              help.setVisible(true); | 
  | 225 | 0 |              help.setHoverHTML(html); | 
  | 226 |  |               | 
  | 227 |  |   | 
  | 228 |  |   | 
  | 229 |  |   | 
  | 230 |  |   | 
  | 231 |  |   | 
  | 232 |  |   | 
  | 233 |  |   | 
  | 234 |  |   | 
  | 235 |  |          } else { | 
  | 236 | 0 |              help.setVisible(false); | 
  | 237 |  |          } | 
  | 238 | 0 |      } | 
  | 239 |  |   | 
  | 240 |  |      private void addEnteredCategory() { | 
  | 241 |  |   | 
  | 242 | 0 |          if (categoryList == null) | 
  | 243 | 0 |              categoryList = new LOCategoryListNew(); | 
  | 244 |  |   | 
  | 245 | 0 |          String selectedId = picker.getSelectedId(); | 
  | 246 | 0 |          if (selectedId.trim().equals("") || selectedId.equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) { | 
  | 247 | 0 |              showNewCategoryWindow(); | 
  | 248 |  |          } else { | 
  | 249 | 0 |              loCatRpcServiceAsync.getData(picker.getSelectedId(), new KSAsyncCallback<Data>() { | 
  | 250 |  |   | 
  | 251 |  |                  @Override | 
  | 252 |  |                  public void handleFailure(Throwable caught) { | 
  | 253 | 0 |                      Window.alert("getLoCategory failed " + caught.getMessage()); | 
  | 254 | 0 |                  } | 
  | 255 |  |   | 
  | 256 |  |                  @Override | 
  | 257 |  |                  public void onSuccess(Data result) { | 
  | 258 | 0 |                      final LoCategoryInfo newCategory = CategoryDataUtil.toLoCategoryInfo(result); | 
  | 259 | 0 |                      addCategory(newCategory); | 
  | 260 |  |   | 
  | 261 | 0 |                  } | 
  | 262 |  |              }); | 
  | 263 |  |          } | 
  | 264 | 0 |      } | 
  | 265 |  |   | 
  | 266 |  |      private void showNewCategoryWindow() { | 
  | 267 |  |   | 
  | 268 | 0 |          final VerticalPanel main = new VerticalPanel(); | 
  | 269 | 0 |          final KSDropDown typesDropDown = new KSDropDown(); | 
  | 270 | 0 |          createCategoryWindow = new KSLightBox(); | 
  | 271 |  |   | 
  | 272 | 0 |          FlexTable layoutTable = new FlexTable(); | 
  | 273 | 0 |          final KSTextBox nameTextBox = new KSTextBox(); | 
  | 274 | 0 |          nameTextBox.setText(picker.getText()); | 
  | 275 |  |   | 
  | 276 | 0 |          layoutTable.setWidget(0, 0, new KSLabel("Category")); | 
  | 277 | 0 |          layoutTable.setWidget(0, 1, new KSLabel("Type")); | 
  | 278 | 0 |          layoutTable.setWidget(1, 0, nameTextBox); | 
  | 279 | 0 |          layoutTable.setWidget(1, 1, typesDropDown); | 
  | 280 |  |   | 
  | 281 | 0 |          SectionTitle sectionTitle = SectionTitle.generateH2Title("Create New Category"); | 
  | 282 |  |           | 
  | 283 | 0 |          createCategoryWindow.setNonCaptionHeader(sectionTitle); | 
  | 284 | 0 |          main.add(layoutTable); | 
  | 285 |  |   | 
  | 286 | 0 |          loCatRpcServiceAsync.getLoCategoryTypes(new KSAsyncCallback<List<LoCategoryTypeInfo>>() { | 
  | 287 |  |   | 
  | 288 |  |              @Override | 
  | 289 |  |              public void handleFailure(Throwable caught) { | 
  | 290 | 0 |                  Window.alert("getLoCategoryTypes failed " + caught.getMessage()); | 
  | 291 | 0 |              } | 
  | 292 |  |   | 
  | 293 |  |              @Override | 
  | 294 |  |              public void onSuccess(List<LoCategoryTypeInfo> result) { | 
  | 295 | 0 |                  final LOCategoryTypeInfoList list = new LOCategoryTypeInfoList(result); | 
  | 296 | 0 |                  typesDropDown.setListItems(list); | 
  | 297 | 0 |                  if (categoryTypeMap == null) { | 
  | 298 | 0 |                      loadCategoryTypes(result); | 
  | 299 |  |                  } | 
  | 300 |  |   | 
  | 301 | 0 |                  CreateLoCancelGroup buttonPanel = new CreateLoCancelGroup(new Callback<LoCancelEnum>() { | 
  | 302 |  |   | 
  | 303 |  |                      @Override | 
  | 304 |  |                      public void exec(LoCancelEnum result) { | 
  | 305 | 0 |                          switch (result) { | 
  | 306 |  |                              case CREATE: | 
  | 307 |  |   | 
  | 308 | 0 |                                  LoCategoryInfoHelper catHelper = new LoCategoryInfoHelper(new Data()); | 
  | 309 | 0 |                                  catHelper.setName(nameTextBox.getText()); | 
  | 310 | 0 |                                  catHelper.setState("active"); | 
  | 311 | 0 |                                  catHelper.setLoRepository(repoKey); | 
  | 312 | 0 |                                  catHelper.setType(typesDropDown.getSelectedItem()); | 
  | 313 |  |   | 
  | 314 | 0 |                                  loCatRpcServiceAsync.saveData(catHelper.getData(), new KSAsyncCallback<DataSaveResult>() { | 
  | 315 |  |                                      @Override | 
  | 316 |  |                                      public void handleFailure(Throwable caught) { | 
  | 317 | 0 |                                          Window.alert("Create LO Category failed: " + caught.getMessage()); | 
  | 318 | 0 |                                      } | 
  | 319 |  |                                       | 
  | 320 |  |                                      @Override | 
  | 321 |  |                                      public void handleVersionMismatch(Throwable caught) { | 
  | 322 | 0 |                                          super.handleVersionMismatch(caught); | 
  | 323 | 0 |                                          KSBlockingProgressIndicator.removeTask(saving); | 
  | 324 | 0 |                                      } | 
  | 325 |  |   | 
  | 326 |  |                                      @Override | 
  | 327 |  |                                      public void onSuccess(DataSaveResult result) { | 
  | 328 | 0 |                                          KSBlockingProgressIndicator.removeTask(saving); | 
  | 329 |  |   | 
  | 330 | 0 |                                          if (result.getValidationResults() != null && !result.getValidationResults().isEmpty()) { | 
  | 331 | 0 |                                              Window.alert("Create LO Category failed: " + result.getValidationResults().get(0).getMessage()); | 
  | 332 |  |                                          } else { | 
  | 333 |  |   | 
  | 334 | 0 |                                              final LoCategoryInfo newCategory = CategoryDataUtil.toLoCategoryInfo(result.getValue()); | 
  | 335 | 0 |                                              addCategory(newCategory); | 
  | 336 | 0 |                                              createCategoryWindow.hide(); | 
  | 337 | 0 |                                              KSNotifier.add(new KSNotification("Create LO Category Successful", false, 3000)); | 
  | 338 |  |                                          } | 
  | 339 | 0 |                                      } | 
  | 340 |  |                                  }); | 
  | 341 | 0 |                                  break; | 
  | 342 |  |                              case CANCEL: | 
  | 343 | 0 |                                  createCategoryWindow.hide(); | 
  | 344 |  |                                  break; | 
  | 345 |  |                          } | 
  | 346 | 0 |                      } | 
  | 347 |  |                  }); | 
  | 348 |  |   | 
  | 349 | 0 |                  main.add(buttonPanel); | 
  | 350 | 0 |                  main.setPixelSize(300, 300); | 
  | 351 | 0 |                  createCategoryWindow.setWidget(main); | 
  | 352 | 0 |                  createCategoryWindow.show(); | 
  | 353 | 0 |              } | 
  | 354 |  |   | 
  | 355 |  |   | 
  | 356 |  |          }); | 
  | 357 | 0 |      } | 
  | 358 |  |   | 
  | 359 |  |      private void loadCategoryTypes(List<LoCategoryTypeInfo> categoryTypes) { | 
  | 360 | 0 |          if (categoryTypeMap == null) { | 
  | 361 | 0 |              categoryTypeMap = new HashMap<String, LoCategoryTypeInfo>(); | 
  | 362 |  |          } | 
  | 363 | 0 |          if (categoryTypes != null) { | 
  | 364 | 0 |              for (LoCategoryTypeInfo i : categoryTypes) { | 
  | 365 | 0 |                  categoryTypeMap.put(i.getId(), i); | 
  | 366 |  |              } | 
  | 367 |  |          } | 
  | 368 | 0 |      } | 
  | 369 |  |   | 
  | 370 |  |       | 
  | 371 |  |   | 
  | 372 |  |   | 
  | 373 |  |   | 
  | 374 |  |   | 
  | 375 |  |   | 
  | 376 |  |   | 
  | 377 |  |   | 
  | 378 |  |      private boolean isCategoryAlreadyAddedToPicker(LoCategoryInfo categoryToCheck){ | 
  | 379 |  |           | 
  | 380 | 0 |          List<LoCategoryInfo> categoriesInPicker = categoryList.getValue(); | 
  | 381 | 0 |          if (categoriesInPicker != null && categoryToCheck != null){ | 
  | 382 | 0 |              for (LoCategoryInfo pickerCategory : categoriesInPicker) { | 
  | 383 | 0 |                  boolean namesMatch = pickerCategory.getName().equalsIgnoreCase(categoryToCheck.getName()); | 
  | 384 | 0 |                  boolean typesMatch = pickerCategory.getType().equalsIgnoreCase(categoryToCheck.getType()); | 
  | 385 | 0 |                  if (namesMatch && typesMatch){ | 
  | 386 | 0 |                      return true; | 
  | 387 |  |                  } | 
  | 388 | 0 |              } | 
  | 389 |  |          } | 
  | 390 | 0 |          return false; | 
  | 391 |  |      } | 
  | 392 |  |      private void addCategory(final LoCategoryInfo category) { | 
  | 393 | 0 |          if (categoryTypeMap == null) { | 
  | 394 | 0 |              categoryTypeMap = new HashMap<String, LoCategoryTypeInfo>(); | 
  | 395 |  |          } | 
  | 396 |  |   | 
  | 397 | 0 |          if (categoryTypeMap.containsKey(category.getType())) { | 
  | 398 |  |               | 
  | 399 | 0 |              if (!isCategoryAlreadyAddedToPicker(category)){ | 
  | 400 | 0 |                  categoryList.addItem(category); | 
  | 401 |  |              } | 
  | 402 | 0 |              picker.reset(); | 
  | 403 |  |          } else { | 
  | 404 | 0 |              loCatRpcServiceAsync.getLoCategoryType(category.getType(), new KSAsyncCallback<LoCategoryTypeInfo>() { | 
  | 405 |  |   | 
  | 406 |  |                  @Override | 
  | 407 |  |                  public void handleFailure(Throwable caught) { | 
  | 408 | 0 |                      Window.alert("getLoCategoryType failed " + caught.getMessage()); | 
  | 409 | 0 |                  } | 
  | 410 |  |   | 
  | 411 |  |                  @Override | 
  | 412 |  |                  public void onSuccess(LoCategoryTypeInfo result) { | 
  | 413 | 0 |                      categoryTypeMap.put(result.getId(), result); | 
  | 414 |  |                       | 
  | 415 | 0 |                      if (!isCategoryAlreadyAddedToPicker(category)){ | 
  | 416 | 0 |                           categoryList.addItem(category); | 
  | 417 |  |                      }  | 
  | 418 | 0 |                      picker.reset(); | 
  | 419 |  |   | 
  | 420 | 0 |                  } | 
  | 421 |  |   | 
  | 422 |  |              }); | 
  | 423 |  |          } | 
  | 424 |  |   | 
  | 425 | 0 |      } | 
  | 426 |  |   | 
  | 427 |  |      private String getLabelText(String labelKey) { | 
  | 428 | 0 |          return Application.getApplicationContext().getUILabel(messageGroup, type, state, labelKey); | 
  | 429 |  |      } | 
  | 430 |  |   | 
  | 431 |  |      @Override | 
  | 432 |  |      public void setValue(List<LoCategoryInfo> categories) { | 
  | 433 | 0 |          categoryList.setValue(categories); | 
  | 434 |  |   | 
  | 435 | 0 |      } | 
  | 436 |  |   | 
  | 437 |  |      @Override | 
  | 438 |  |      public void setValue(List<LoCategoryInfo> value, boolean fireEvents) { | 
  | 439 | 0 |          setValue(value); | 
  | 440 | 0 |      } | 
  | 441 |  |   | 
  | 442 |  |       | 
  | 443 |  |   | 
  | 444 |  |   | 
  | 445 |  |      @Override | 
  | 446 |  |      public List<LoCategoryInfo> getValue() { | 
  | 447 | 0 |          return categoryList.getValue(); | 
  | 448 |  |      } | 
  | 449 |  |   | 
  | 450 |  |       | 
  | 451 |  |   | 
  | 452 |  |   | 
  | 453 |  |      @Override | 
  | 454 |  |      public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<LoCategoryInfo>> handler) { | 
  | 455 | 0 |          return categoryList.addValueChangeHandler(handler); | 
  | 456 |  |      } | 
  | 457 |  |   | 
  | 458 |  |       | 
  | 459 |  |   | 
  | 460 |  |   | 
  | 461 |  |   | 
  | 462 |  |   | 
  | 463 | 0 |      private class LOCategoryPicker extends Composite implements SuggestPicker { | 
  | 464 |  |   | 
  | 465 |  |           | 
  | 466 |  |   | 
  | 467 | 0 |          final SearchSuggestOracle loSearchOracle = new SearchSuggestOracle( | 
  | 468 |  |                  "lo.search.loCategories", | 
  | 469 |  |                  "lo.queryParam.loOptionalCategoryName", | 
  | 470 |  |                  "lo.queryParam.loCategoryId", | 
  | 471 |  |                  "lo.resultColumn.categoryId", | 
  | 472 |  |                  "lo.resultColumn.categoryNameAndType"); | 
  | 473 |  |   | 
  | 474 | 0 |          final KSSuggestBox suggestBox = new KSSuggestBox(loSearchOracle); | 
  | 475 |  |   | 
  | 476 | 0 |          private final FocusGroup focus = new FocusGroup(this); | 
  | 477 |  |   | 
  | 478 | 0 |          private VerticalPanel main = new VerticalPanel(); | 
  | 479 |  |   | 
  | 480 | 0 |          protected LOCategoryPicker() { | 
  | 481 | 0 |              super(); | 
  | 482 | 0 |              init(); | 
  | 483 | 0 |          } | 
  | 484 |  |   | 
  | 485 |  |          public String getSelectedId() { | 
  | 486 | 0 |              return suggestBox.getSelectedId(); | 
  | 487 |  |          } | 
  | 488 |  |   | 
  | 489 |  |          private void init() { | 
  | 490 | 0 |              focus.addWidget(suggestBox); | 
  | 491 | 0 |              loSearchOracle.setTextWidget(suggestBox.getTextBox()); | 
  | 492 | 0 |              main.add(suggestBox); | 
  | 493 | 0 |              initWidget(main); | 
  | 494 | 0 |          } | 
  | 495 |  |   | 
  | 496 |  |          @Override | 
  | 497 |  |          public String getValue() { | 
  | 498 | 0 |              return suggestBox.getSelectedId(); | 
  | 499 |  |          } | 
  | 500 |  |   | 
  | 501 |  |          @Override | 
  | 502 |  |          public void setValue(String value) { | 
  | 503 | 0 |              setValue(value, true); | 
  | 504 | 0 |          } | 
  | 505 |  |   | 
  | 506 |  |          @Override | 
  | 507 |  |          public void setValue(String value, boolean fireEvents) { | 
  | 508 | 0 |              suggestBox.reset(); | 
  | 509 | 0 |              suggestBox.setValue(value, fireEvents); | 
  | 510 | 0 |          } | 
  | 511 |  |   | 
  | 512 |  |   | 
  | 513 |  |          @Override | 
  | 514 |  |          public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { | 
  | 515 | 0 |              return suggestBox.addValueChangeHandler(handler); | 
  | 516 |  |          } | 
  | 517 |  |   | 
  | 518 |  |          @Override | 
  | 519 |  |          public void fireEvent(GwtEvent<?> event) { | 
  | 520 | 0 |              super.fireEvent(event); | 
  | 521 | 0 |          } | 
  | 522 |  |   | 
  | 523 |  |          public void reset() { | 
  | 524 | 0 |              suggestBox.reset(); | 
  | 525 | 0 |          } | 
  | 526 |  |   | 
  | 527 |  |          @Override | 
  | 528 |  |          public HandlerRegistration addFocusHandler(FocusHandler handler) { | 
  | 529 | 0 |              return focus.addFocusHandler(handler); | 
  | 530 |  |          } | 
  | 531 |  |   | 
  | 532 |  |          @Override | 
  | 533 |  |          public HandlerRegistration addBlurHandler(BlurHandler handler) { | 
  | 534 | 0 |              return focus.addBlurHandler(handler); | 
  | 535 |  |          } | 
  | 536 |  |   | 
  | 537 |  |          public String getText() { | 
  | 538 | 0 |              return suggestBox.getText(); | 
  | 539 |  |          } | 
  | 540 |  |   | 
  | 541 |  |          @Override | 
  | 542 |  |          public HandlerRegistration addSelectionChangeHandler(SelectionChangeHandler handler) { | 
  | 543 | 0 |              return suggestBox.addSelectionChangeHandler(handler); | 
  | 544 |  |          } | 
  | 545 |  |   | 
  | 546 |  |          @Override | 
  | 547 |  |          protected void onEnsureDebugId(String baseID) { | 
  | 548 | 0 |              super.onEnsureDebugId(baseID); | 
  | 549 | 0 |              suggestBox.getTextBox().ensureDebugId(baseID); | 
  | 550 | 0 |          } | 
  | 551 |  |           | 
  | 552 |  |      } | 
  | 553 |  |   | 
  | 554 |  |      private class LOCategoryTypeInfoList implements ListItems { | 
  | 555 | 0 |          Map<String, LoCategoryTypeInfo> loTypeMap = new HashMap<String, LoCategoryTypeInfo>(); | 
  | 556 |  |   | 
  | 557 | 0 |          public LOCategoryTypeInfoList(List<LoCategoryTypeInfo> loTypes) { | 
  | 558 | 0 |              for (LoCategoryTypeInfo type : loTypes) { | 
  | 559 | 0 |                  loTypeMap.put(type.getId(), type); | 
  | 560 |  |              } | 
  | 561 | 0 |          } | 
  | 562 |  |   | 
  | 563 |  |          public List<String> getAttrKeys() { | 
  | 564 | 0 |              return Arrays.asList("Name"); | 
  | 565 |  |          } | 
  | 566 |  |   | 
  | 567 |  |          public String getItemAttribute(String id, String attrkey) { | 
  | 568 | 0 |              LoCategoryTypeInfo lo = loTypeMap.get(id); | 
  | 569 |  |   | 
  | 570 | 0 |              if (attrkey.equals("Name")) { | 
  | 571 | 0 |                  return lo.getName(); | 
  | 572 |  |              } | 
  | 573 |  |   | 
  | 574 | 0 |              return null; | 
  | 575 |  |          } | 
  | 576 |  |   | 
  | 577 |  |          public int getItemCount() { | 
  | 578 | 0 |              return loTypeMap.size(); | 
  | 579 |  |          } | 
  | 580 |  |   | 
  | 581 |  |          public List<String> getItemIds() { | 
  | 582 | 0 |              List<String> keys = new ArrayList<String>(); | 
  | 583 |  |   | 
  | 584 | 0 |              for (String s : loTypeMap.keySet()) { | 
  | 585 | 0 |                  keys.add(s); | 
  | 586 |  |              } | 
  | 587 |  |   | 
  | 588 | 0 |              return keys; | 
  | 589 |  |          } | 
  | 590 |  |   | 
  | 591 |  |          public String getItemText(String id) { | 
  | 592 | 0 |              return ((LoCategoryTypeInfo) loTypeMap.get(id)).getName(); | 
  | 593 |  |          } | 
  | 594 |  |      } | 
  | 595 |  |   | 
  | 596 | 0 |      public class LOCategoryListNew extends Composite implements HasValue<List<LoCategoryInfo>> { | 
  | 597 |  |          private static final String CATEGORY_TYPE_SEPARATOR = " - "; | 
  | 598 |  |          private VerticalPanel listPanel; | 
  | 599 | 0 |          private VerticalPanel main = new VerticalPanel(); | 
  | 600 | 0 |          protected List<LoCategoryInfo> categories = new ArrayList<LoCategoryInfo>(); | 
  | 601 |  |   | 
  | 602 | 0 |          final CloseHandler<KSItemLabel> deleteHandler = new CloseHandler<KSItemLabel>() { | 
  | 603 |  |              @Override | 
  | 604 |  |              public void onClose(CloseEvent<KSItemLabel> event) { | 
  | 605 | 0 |                  KSItemLabel itemLabel = event.getTarget(); | 
  | 606 | 0 |                  String itemText = itemLabel.getDeletedKey(); | 
  | 607 | 0 |                  categoryList.removeItem(itemText); | 
  | 608 | 0 |                  categoryList.redraw(); | 
  | 609 | 0 |              } | 
  | 610 |  |          }; | 
  | 611 |  |   | 
  | 612 | 0 |          public LOCategoryListNew() { | 
  | 613 | 0 |              listPanel = new VerticalPanel(); | 
  | 614 | 0 |              main.add(listPanel); | 
  | 615 | 0 |              super.initWidget(main); | 
  | 616 | 0 |          } | 
  | 617 |  |   | 
  | 618 |  |          public HandlerRegistration addValueChangeHandler(ValueChangeHandler<List<LoCategoryInfo>> handler) { | 
  | 619 | 0 |              return addHandler(handler, ValueChangeEvent.getType()); | 
  | 620 |  |          } | 
  | 621 |  |   | 
  | 622 |  |          private void fireChangeEvent() { | 
  | 623 | 0 |              ValueChangeEvent.fire(this, categories); | 
  | 624 | 0 |          } | 
  | 625 |  |   | 
  | 626 |  |          public void redraw() { | 
  | 627 |  |   | 
  | 628 | 0 |              if (null == categoryTypeMap || categoryTypeMap.isEmpty()) { | 
  | 629 |  |   | 
  | 630 | 0 |                  loCatRpcServiceAsync.getLoCategoryTypes(new KSAsyncCallback<List<LoCategoryTypeInfo>>() { | 
  | 631 |  |   | 
  | 632 |  |                      @Override | 
  | 633 |  |                      public void handleFailure(Throwable caught) { | 
  | 634 | 0 |                          Window.alert("getLoCategoryTypes failed " + caught.getMessage()); | 
  | 635 | 0 |                      } | 
  | 636 |  |   | 
  | 637 |  |                      @Override | 
  | 638 |  |                      public void onSuccess(List<LoCategoryTypeInfo> result) { | 
  | 639 | 0 |                          if (categoryTypeMap == null) { | 
  | 640 | 0 |                              loadCategoryTypes(result); | 
  | 641 |  |                          } | 
  | 642 | 0 |                          redrawCategoryTable(); | 
  | 643 | 0 |                      } | 
  | 644 |  |   | 
  | 645 |  |                  }); | 
  | 646 |  |              } else { | 
  | 647 | 0 |                  redrawCategoryTable(); | 
  | 648 |  |              } | 
  | 649 | 0 |          } | 
  | 650 |  |   | 
  | 651 |  |          private void redrawCategoryTable() { | 
  | 652 | 0 |              listPanel.clear(); | 
  | 653 |  |   | 
  | 654 | 0 |              for (int i = 0; i < categories.size(); i++) { | 
  | 655 | 0 |                  String name = categories.get(i).getName(); | 
  | 656 | 0 |                  String typeKey = categories.get(i).getType(); | 
  | 657 |  |                   | 
  | 658 | 0 |                  KSItemLabel newItemLabel = new KSItemLabel(true, new CategoryDataParser()); | 
  | 659 | 0 |                  Data categoryData = CategoryDataUtil.toData(categories.get(i)); | 
  | 660 | 0 |                  newItemLabel.setValue(new DataValue(categoryData)); | 
  | 661 | 0 |                  newItemLabel.addCloseHandler(deleteHandler); | 
  | 662 |  |   | 
  | 663 | 0 |                  listPanel.add(newItemLabel); | 
  | 664 |  |   | 
  | 665 |  |   | 
  | 666 |  |   | 
  | 667 |  |   | 
  | 668 |  |   | 
  | 669 |  |   | 
  | 670 |  |   | 
  | 671 |  |   | 
  | 672 |  |   | 
  | 673 |  |   | 
  | 674 |  |   | 
  | 675 |  |   | 
  | 676 |  |   | 
  | 677 |  |   | 
  | 678 |  |              } | 
  | 679 | 0 |          } | 
  | 680 |  |   | 
  | 681 |  |          public List<LoCategoryInfo> getValue() { | 
  | 682 | 0 |              return categories; | 
  | 683 |  |          } | 
  | 684 |  |   | 
  | 685 |  |          public void setValue(List<LoCategoryInfo> categories) { | 
  | 686 | 0 |              this.categories = categories; | 
  | 687 | 0 |              redraw(); | 
  | 688 | 0 |          } | 
  | 689 |  |   | 
  | 690 |  |          @Override | 
  | 691 |  |          public void setValue(List<LoCategoryInfo> value, boolean fireEvents) { | 
  | 692 | 0 |              setValue(value); | 
  | 693 | 0 |          } | 
  | 694 |  |   | 
  | 695 |  |   | 
  | 696 |  |          public void removeItem(String text) { | 
  | 697 |  |   | 
  | 698 | 0 |              int a = text.indexOf(CATEGORY_TYPE_SEPARATOR); | 
  | 699 | 0 |              text = text.substring(0, a); | 
  | 700 |  |   | 
  | 701 | 0 |              int i = 0; | 
  | 702 | 0 |              for (LoCategoryInfo catInfo : categories) { | 
  | 703 | 0 |                  String name = catInfo.getName(); | 
  | 704 |  |   | 
  | 705 | 0 |                  if (name.equals(text)) { | 
  | 706 | 0 |                      categories.remove(i); | 
  | 707 | 0 |                      fireChangeEvent(); | 
  | 708 | 0 |                      break; | 
  | 709 |  |                  } | 
  | 710 | 0 |                  i++; | 
  | 711 | 0 |              } | 
  | 712 | 0 |              redraw(); | 
  | 713 | 0 |          } | 
  | 714 |  |   | 
  | 715 |  |          public void addItem(LoCategoryInfo category) { | 
  | 716 | 0 |              categories.add(category); | 
  | 717 | 0 |              fireChangeEvent(); | 
  | 718 | 0 |              redraw(); | 
  | 719 | 0 |          } | 
  | 720 |  |      } | 
  | 721 |  |   | 
  | 722 |  |      public class CategoryDataParser implements DataHelper { | 
  | 723 |  |   | 
  | 724 | 0 |          public CategoryDataParser() { | 
  | 725 | 0 |          } | 
  | 726 |  |   | 
  | 727 |  |          ; | 
  | 728 |  |   | 
  | 729 |  |          @Override | 
  | 730 |  |          public String getKey(Data data) { | 
  | 731 | 0 |              return parse(data); | 
  | 732 |  |          } | 
  | 733 |  |   | 
  | 734 |  |          @Override | 
  | 735 |  |          public String parse(Data data) { | 
  | 736 | 0 |              String result = null; | 
  | 737 | 0 |              if (data != null) { | 
  | 738 | 0 |                  LoCategoryInfo loCategoryInfo = CategoryDataUtil.toLoCategoryInfo(data); | 
  | 739 | 0 |                  String typeName = "ERROR: uninitialized categoryTypeMap"; | 
  | 740 | 0 |                  if (null != categoryTypeMap) { | 
  | 741 | 0 |                      typeName = categoryTypeMap.get(loCategoryInfo.getType()).getName(); | 
  | 742 |  |                  } | 
  | 743 | 0 |                  result = loCategoryInfo.getName() + | 
  | 744 |  |                          LOCategoryBuilder.LOCategoryListNew.CATEGORY_TYPE_SEPARATOR + typeName; | 
  | 745 | 0 |              } else { | 
  | 746 | 0 |                  result = ""; | 
  | 747 |  |              } | 
  | 748 | 0 |              return result; | 
  | 749 |  |          } | 
  | 750 |  |   | 
  | 751 |  |      } | 
  | 752 |  |   | 
  | 753 | 0 |      public class CreateLoCancelGroup extends ButtonGroup<LoCancelEnum> { | 
  | 754 | 0 |          public CreateLoCancelGroup(Callback<LoCancelEnum> callback) { | 
  | 755 | 0 |              layout = new ButtonRow(); | 
  | 756 | 0 |              this.addCallback(callback); | 
  | 757 |  |   | 
  | 758 | 0 |              addButton(LoCancelEnum.CANCEL); | 
  | 759 | 0 |              addButtonToSecondaryGroup(LoCancelEnum.CREATE); | 
  | 760 |  |   | 
  | 761 | 0 |              this.initWidget(layout); | 
  | 762 | 0 |          } | 
  | 763 |  |   | 
  | 764 |  |          private void addButton(final LoCancelEnum type) { | 
  | 765 | 0 |              KSButton button = new KSButton(type.getText(), new ClickHandler() { | 
  | 766 |  |   | 
  | 767 |  |                  @Override | 
  | 768 |  |                  public void onClick(ClickEvent event) { | 
  | 769 | 0 |                      sendCallbacks(type); | 
  | 770 | 0 |                  } | 
  | 771 |  |              }); | 
  | 772 | 0 |              layout.addButton(button); | 
  | 773 | 0 |              buttonMap.put(type, button); | 
  | 774 | 0 |          } | 
  | 775 |  |   | 
  | 776 |  |          private void addButtonToSecondaryGroup(final LoCancelEnum type) { | 
  | 777 | 0 |              KSButton button = new KSButton(type.getText(), new ClickHandler() { | 
  | 778 |  |   | 
  | 779 |  |                  @Override | 
  | 780 |  |                  public void onClick(ClickEvent event) { | 
  | 781 | 0 |                      sendCallbacks(type); | 
  | 782 | 0 |                  } | 
  | 783 |  |              }); | 
  | 784 | 0 |              ((ButtonRow) layout).addButtonToSecondaryGroup(button); | 
  | 785 | 0 |              buttonMap.put(type, button); | 
  | 786 | 0 |          } | 
  | 787 |  |      } | 
  | 788 |  |   | 
  | 789 | 0 |      public static enum LoCancelEnum implements ButtonEnum { | 
  | 790 | 0 |          CREATE, CANCEL; | 
  | 791 |  |   | 
  | 792 |  |          @Override | 
  | 793 |  |          public ButtonEnum getActionType() { | 
  | 794 | 0 |              return CREATE; | 
  | 795 |  |          } | 
  | 796 |  |   | 
  | 797 |  |          @Override | 
  | 798 |  |          public ButtonEnum getCancelType() { | 
  | 799 | 0 |              return CANCEL; | 
  | 800 |  |          } | 
  | 801 |  |   | 
  | 802 |  |          @Override | 
  | 803 |  |          public ButtonStyle getStyle() { | 
  | 804 | 0 |              return ButtonStyle.PRIMARY; | 
  | 805 |  |          } | 
  | 806 |  |   | 
  | 807 |  |          @Override | 
  | 808 |  |          public String getText() { | 
  | 809 | 0 |              switch (this) { | 
  | 810 |  |                  case CREATE: | 
  | 811 | 0 |                      return "Create"; | 
  | 812 |  |                  case CANCEL: | 
  | 813 | 0 |                      return "Cancel"; | 
  | 814 |  |              } | 
  | 815 | 0 |              return null; | 
  | 816 |  |          } | 
  | 817 |  |      } | 
  | 818 |  |   | 
  | 819 |  |      @Override | 
  | 820 |  |      protected void onEnsureDebugId(String baseID) { | 
  | 821 | 0 |          super.onEnsureDebugId(baseID); | 
  | 822 | 0 |          picker.ensureDebugId(baseID); | 
  | 823 | 0 |          addButton.ensureDebugId(baseID + "-Add"); | 
  | 824 | 0 |          browseCategoryLink.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(baseID + "-" + browseCategoryLink.getText())); | 
  | 825 | 0 |      } | 
  | 826 |  |       | 
  | 827 |  |       | 
  | 828 |  |  } |