Coverage Report - org.kuali.student.common.ui.client.widgets.list.KSSelectedList
 
Classes in this File Line Coverage Branch Coverage Complexity
KSSelectedList
0%
0/165
0%
0/56
1.935
KSSelectedList$1
0%
0/18
0%
0/4
1.935
KSSelectedList$2
0%
0/10
0%
0/4
1.935
KSSelectedList$3
0%
0/6
0%
0/6
1.935
KSSelectedList$4
0%
0/5
0%
0/4
1.935
KSSelectedList$5
0%
0/3
N/A
1.935
KSSelectedList$6
0%
0/3
N/A
1.935
KSSelectedList$7
0%
0/7
N/A
1.935
KSSelectedList$ItemDataHelper
0%
0/9
0%
0/4
1.935
 
 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.common.ui.client.widgets.list;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.Map.Entry;
 24  
 
 25  
 import org.kuali.student.common.assembly.data.Data;
 26  
 import org.kuali.student.common.assembly.data.Data.DataValue;
 27  
 import org.kuali.student.common.assembly.data.Data.Property;
 28  
 import org.kuali.student.common.assembly.data.Data.StringKey;
 29  
 import org.kuali.student.common.assembly.data.Data.Value;
 30  
 import org.kuali.student.common.ui.client.configurable.mvc.WidgetConfigInfo;
 31  
 import org.kuali.student.common.ui.client.mvc.Callback;
 32  
 import org.kuali.student.common.ui.client.mvc.HasCrossConstraints;
 33  
 import org.kuali.student.common.ui.client.mvc.HasDataValue;
 34  
 import org.kuali.student.common.ui.client.mvc.HasFocusLostCallbacks;
 35  
 import org.kuali.student.common.ui.client.mvc.HasWidgetReadyCallback;
 36  
 import org.kuali.student.common.ui.client.mvc.TranslatableValueWidget;
 37  
 import org.kuali.student.common.ui.client.util.UtilConstants;
 38  
 import org.kuali.student.common.ui.client.widgets.DataHelper;
 39  
 import org.kuali.student.common.ui.client.widgets.HasInputWidget;
 40  
 import org.kuali.student.common.ui.client.widgets.KSButton;
 41  
 import org.kuali.student.common.ui.client.widgets.KSButtonAbstract.ButtonStyle;
 42  
 import org.kuali.student.common.ui.client.widgets.KSDropDown;
 43  
 import org.kuali.student.common.ui.client.widgets.KSItemLabel;
 44  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.ValidationProcessable;
 45  
 import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel;
 46  
 import org.kuali.student.common.ui.client.widgets.menus.KSListPanel;
 47  
 import org.kuali.student.common.ui.client.widgets.menus.KSListPanel.ListType;
 48  
 import org.kuali.student.common.ui.client.widgets.search.KSPicker;
 49  
 import org.kuali.student.common.ui.client.widgets.search.SelectedResults;
 50  
 import org.kuali.student.common.ui.client.widgets.suggestbox.KSSuggestBox;
 51  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 52  
 import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel;
 53  
 
 54  
 import com.google.gwt.event.dom.client.ClickEvent;
 55  
 import com.google.gwt.event.dom.client.ClickHandler;
 56  
 import com.google.gwt.event.logical.shared.CloseEvent;
 57  
 import com.google.gwt.event.logical.shared.CloseHandler;
 58  
 import com.google.gwt.event.shared.HandlerRegistration;
 59  
 import com.google.gwt.user.client.Timer;
 60  
 import com.google.gwt.user.client.ui.Composite;
 61  
 import com.google.gwt.user.client.ui.HasName;
 62  
 import com.google.gwt.user.client.ui.HorizontalPanel;
 63  
 import com.google.gwt.user.client.ui.Widget;
 64  
 
 65  0
 public class KSSelectedList extends Composite implements HasDataValue, HasName, HasSelectionChangeHandlers, HasWidgetReadyCallback, TranslatableValueWidget, HasInputWidget, HasFocusLostCallbacks, HasCrossConstraints, ValidationProcessable {
 66  
     private static final String VALUE = "value";
 67  
     private static final String DISPLAY = "display";
 68  
 
 69  
     private String name;
 70  
     private boolean initialized;
 71  
     private VerticalFlowPanel mainPanel;
 72  
     private HorizontalPanel pickerPanel;
 73  
     private KSPicker picker;
 74  
     private KSButton addItemButton;
 75  
     private KSListPanel valuesPanel;
 76  
 //    private List<SelectedValue> selectedValues = new ArrayList<SelectedValue>();
 77  
 //    private List<SelectedValue> removedValues = new ArrayList<SelectedValue>();
 78  
 
 79  0
     private List<KSItemLabel> selectedItems = new ArrayList<KSItemLabel>();
 80  0
     private List<KSItemLabel> removedItems = new ArrayList<KSItemLabel>();
 81  0
     public static ItemDataHelper itemDataHelper = new ItemDataHelper();
 82  
 
 83  0
     private final List<SelectionChangeHandler> selectionChangeHandlers = new ArrayList<SelectionChangeHandler>();
 84  0
     private final List<Callback<Widget>> widgetReadyCallbacks = new ArrayList<Callback<Widget>>();
 85  0
     private boolean hasDetails = false;
 86  
 
 87  
     private WidgetConfigInfo config;
 88  
 
 89  0
     public KSSelectedList(WidgetConfigInfo config, boolean hasDetails) {
 90  0
         init(config, hasDetails);
 91  0
     }
 92  
 
 93  0
     public KSSelectedList(WidgetConfigInfo config) {
 94  0
         init(config, false);
 95  0
     }
 96  
 
 97  
     public WidgetConfigInfo getConfig() {
 98  0
         return config;
 99  
     }
 100  
 
 101  
     private void init(WidgetConfigInfo config, final boolean hasDetails) {
 102  0
         this.config = config;
 103  0
         this.hasDetails = hasDetails;
 104  0
         mainPanel = new VerticalFlowPanel();
 105  0
         initWidget(mainPanel);
 106  0
         if (config.canEdit) {
 107  0
             pickerPanel = new HorizontalPanel();
 108  0
             pickerPanel.addStyleName("ks-selected-list-picker");
 109  0
             addItemButton = new KSButton("Add to list", ButtonStyle.SECONDARY_SMALL);
 110  0
             addItemButton.setEnabled(false);
 111  0
             picker = new KSPicker(config);
 112  0
             picker.setAdvancedSearchCallback(createAdvancedSearchCallback());
 113  0
             addSelectionChangeHandler();
 114  
 
 115  0
             addItemButton.addClickHandler(new ClickHandler() {
 116  
                 @Override
 117  
                 public void onClick(ClickEvent event) {
 118  0
                         Value v = picker.getValue();
 119  0
                         if(v instanceof DataValue){
 120  0
                             Data d = ((DataValue) picker.getValue()).get();
 121  
                             
 122  0
                             Iterator<Property> iter = d.realPropertyIterator();
 123  0
                             while (iter.hasNext()) {
 124  0
                                 Property p = iter.next();
 125  0
                                 String s = p.getValue();
 126  0
                                 KSItemLabel selectedItem = createItem(s, picker.getDisplayValue(),
 127  
                                         hasDetails);
 128  0
                                 addItem(selectedItem);
 129  0
                             }
 130  0
                         }else{
 131  0
                                 String s = v.get();
 132  0
                         KSItemLabel selectedItem = createItem(s, picker.getDisplayValue(),
 133  
                                         hasDetails);
 134  0
                             addItem(selectedItem);
 135  
                         }
 136  0
                         picker.clear();
 137  0
                     addItemButton.setEnabled(false);
 138  0
                 }
 139  
             });
 140  
 
 141  0
             pickerPanel.add(picker);
 142  0
             pickerPanel.add(addItemButton);
 143  
 
 144  
 
 145  0
             mainPanel.add(pickerPanel);
 146  
         }
 147  
 
 148  
 
 149  0
         valuesPanel = new KSListPanel(ListType.UNORDERED);
 150  0
         if (config.canEdit) {
 151  0
             valuesPanel.setStyleName("ks-selected-list");
 152  
         } else {
 153  0
             valuesPanel.setStyleName("ks-selected-list-readOnly");
 154  
         }
 155  0
         mainPanel.add(valuesPanel);
 156  0
         initialized = true;
 157  0
         widgetReady();
 158  0
     }
 159  
 
 160  
     public KSListPanel separateValuesPanel() {
 161  0
         mainPanel.remove(valuesPanel);
 162  0
         return valuesPanel;
 163  
     }
 164  
 
 165  
     private void widgetReady() {
 166  0
         for (Callback<Widget> callback : widgetReadyCallbacks) {
 167  0
             callback.exec(this);
 168  
         }
 169  0
     }
 170  
 
 171  
     private Callback<List<SelectedResults>> createAdvancedSearchCallback() {
 172  0
         Callback<List<SelectedResults>> result = new Callback<List<SelectedResults>>() {
 173  
             public void exec(List<SelectedResults> results) {
 174  0
                 if (results.size() > 0) {
 175  0
                     for (SelectedResults res : results) {
 176  0
                         KSItemLabel item = createItem(res.getReturnKey(), res.getDisplayKey(), hasDetails);
 177  0
                         addItem(item, false);
 178  0
                     }
 179  0
                     picker.clear();
 180  0
                     addItemButton.setEnabled(false);
 181  0
                     selectionChanged();
 182  
                 }
 183  0
             }
 184  
 
 185  
         };
 186  0
         return result;
 187  
     }
 188  
 
 189  
     /**
 190  
      * Adds handler when suggest box selection change event is fired and enables/disables the the
 191  
      * Add item button accordingly.
 192  
      */
 193  
     private void addSelectionChangeHandler() {
 194  0
         if (picker.getInputWidget() instanceof KSSuggestBox) {
 195  0
             KSSuggestBox suggestBox = (KSSuggestBox) picker.getInputWidget();
 196  0
             suggestBox.addSelectionChangeHandler(new SelectionChangeHandler() {
 197  
                 @Override
 198  
                 public void onSelectionChange(SelectionChangeEvent event) {
 199  
                     //Compare the user entered value in picker's input textbox with the current display value
 200  
                     //from the picker suggest list, if they are the same (ie. user has selected item from list)
 201  
                     //then enable the add to list button. NOTE: This comparison should not be required if the picker
 202  
                     //ONLY fired selection change event when an actual selection change took place, but doesn't seem
 203  
                     //to be doing that.
 204  0
                     String userValue = ((KSSuggestBox) picker.getInputWidget()).getText();
 205  0
                     String displayValue = picker.getDisplayValue();
 206  0
                     boolean enabled = displayValue != null && !displayValue.isEmpty() && displayValue.equals(userValue);
 207  0
                     addItemButton.setEnabled(enabled);
 208  0
                 }
 209  
             });
 210  0
         } else if(picker.getInputWidget() instanceof KSDropDown){
 211  0
                 KSDropDown dropDown = (KSDropDown) picker.getInputWidget();
 212  0
                 dropDown.addSelectionChangeHandler(new SelectionChangeHandler() {
 213  
                 @Override
 214  
                 public void onSelectionChange(SelectionChangeEvent event) {
 215  
                         //Enable if not null
 216  0
                     String displayValue = picker.getDisplayValue();
 217  0
                     boolean enabled = displayValue != null && !displayValue.isEmpty();
 218  0
                     addItemButton.setEnabled(enabled);
 219  0
                 }
 220  
             });
 221  
         }
 222  0
     }
 223  
 
 224  
     public void addItem(String value, String display) {
 225  0
         addItem(createItem(value, display, hasDetails));
 226  0
     }
 227  
     
 228  
     public void addItem(final KSItemLabel item) {
 229  0
         addItem(item, true);
 230  0
     }
 231  
 
 232  
     public void addItem(final KSItemLabel item, boolean fireChangeListeners) {
 233  0
         if (removedItems.contains(item)) {
 234  0
             removedItems.remove(item);
 235  
         }
 236  0
         if (!selectedItems.contains(item)) {
 237  0
             selectedItems.add(item);
 238  
         }
 239  0
         valuesPanel.add(item);
 240  0
         if (fireChangeListeners) {
 241  0
             selectionChanged();
 242  
         }
 243  0
         if (config.canEdit && fireChangeListeners) {
 244  0
             item.setHighlighted(true);
 245  0
             new Timer() {
 246  
                 @Override
 247  
                 public void run() {
 248  0
                     item.setHighlighted(false);
 249  0
                 }
 250  
             }.schedule(5000);
 251  
         } else {
 252  0
             item.removeHighlight();
 253  
         }
 254  0
     }
 255  
 
 256  
     private void selectionChanged() {
 257  0
         for (SelectionChangeHandler handler : selectionChangeHandlers) {
 258  0
             handler.onSelectionChange(new SelectionChangeEvent(this));
 259  
         }
 260  0
     }
 261  
 
 262  
     public void clear() {
 263  0
         selectedItems = new ArrayList<KSItemLabel>();
 264  0
         removedItems = new ArrayList<KSItemLabel>();
 265  0
         valuesPanel.clear();
 266  0
     }
 267  
 
 268  
     @Override
 269  
     public String getName() {
 270  
 
 271  0
         return name;
 272  
     }
 273  
 
 274  
     @Override
 275  
     public void setName(String name) {
 276  0
         this.name = name;
 277  0
     }
 278  
 
 279  
 
 280  
     @Override
 281  
     public HandlerRegistration addSelectionChangeHandler(final SelectionChangeHandler handler) {
 282  0
         selectionChangeHandlers.add(handler);
 283  0
         HandlerRegistration result = new HandlerRegistration() {
 284  
             @Override
 285  
             public void removeHandler() {
 286  0
                 selectionChangeHandlers.remove(handler);
 287  0
             }
 288  
         };
 289  0
         return result;
 290  
     }
 291  
 
 292  
     @Override
 293  
     public void addWidgetReadyCallback(Callback<Widget> callback) {
 294  0
         widgetReadyCallbacks.add(callback);
 295  0
     }
 296  
 
 297  
     @Override
 298  
     public boolean isInitialized() {
 299  0
         return initialized;
 300  
     }
 301  
 
 302  
     @Override
 303  
     public void setInitialized(boolean initialized) {
 304  0
         this.initialized = initialized;
 305  0
     }
 306  
 
 307  
 
 308  
     @Override
 309  
     public void addValueChangeCallback(Callback<Value> callback) {
 310  
         // TODO ryan - THIS METHOD NEEDS JAVADOCS
 311  
 
 312  0
     }
 313  
 
 314  
     @Override
 315  
     public Value getValue() {
 316  0
         Data data = new Data();
 317  0
         for (KSItemLabel item : selectedItems) {
 318  0
             data.add(item.getKey());
 319  
         }
 320  0
         if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) {
 321  0
             data.add(UtilConstants.IMPOSSIBLE_CHARACTERS);
 322  
         }
 323  0
         DataValue result = new DataValue(data);
 324  0
         return result;
 325  
     }
 326  
 
 327  
     public List<KSItemLabel> getSelectedItems() {
 328  0
         return selectedItems;
 329  
     }
 330  
 
 331  
     /**
 332  
      * Returns all selected values along with their translations in the _runtime data.
 333  
      *
 334  
      * @return
 335  
      */
 336  
     public Value getValueWithTranslations() {
 337  0
         Data data = new Data();
 338  0
         Data _runtimeData = new Data();
 339  
 
 340  0
         for (KSItemLabel item : selectedItems) {
 341  0
             data.add(item.getKey());
 342  0
             Data displayData = new Data();
 343  0
             displayData.set("id-translation", item.getDisplayText());
 344  0
             _runtimeData.add(displayData);
 345  0
         }
 346  0
         if (picker.getDisplayValue().equals(UtilConstants.IMPOSSIBLE_CHARACTERS)) {
 347  0
             data.add(UtilConstants.IMPOSSIBLE_CHARACTERS);
 348  0
             Data displayData = new Data();
 349  0
             displayData.set("id-translation", UtilConstants.IMPOSSIBLE_CHARACTERS);
 350  0
             _runtimeData.add(displayData);
 351  
         }
 352  0
         data.set(new StringKey("_runtimeData"), _runtimeData);
 353  0
         DataValue result = new DataValue(data);
 354  0
         return result;
 355  
     }
 356  
 
 357  
     private KSItemLabel createItem(String value, String display, boolean hasDetails) {
 358  0
         Data itemData = toItemData(value, display);
 359  0
         DataValue itemDataValue = new DataValue(itemData);
 360  0
         KSItemLabel item = new KSItemLabel(KSSelectedList.this.config.canEdit, hasDetails,
 361  
                 itemDataHelper);
 362  0
         item.setValue(itemDataValue);
 363  0
         item.addCloseHandler(new CloseHandler<KSItemLabel>() {
 364  
             @Override
 365  
             public void onClose(CloseEvent<KSItemLabel> event) {
 366  0
                 KSItemLabel itemToBeDeleted = event.getTarget();
 367  0
                 selectedItems.remove(itemToBeDeleted);
 368  0
                 removedItems.add(itemToBeDeleted);
 369  0
                 valuesPanel.remove(itemToBeDeleted);
 370  0
                 selectionChanged();
 371  0
             }
 372  
         });
 373  0
         return item;
 374  
     }
 375  
 
 376  
     private Data toItemData(String value, String display) {
 377  0
         Data data = new Data();
 378  0
         data.set(VALUE, value);
 379  0
         data.set(DISPLAY, display);
 380  0
         return data;
 381  
     }
 382  
 
 383  
     @Override
 384  
     public void setValue(Value value) {
 385  0
         clear();
 386  0
         if (value != null) {
 387  0
             Data data = ((DataValue) value).get();
 388  0
             Iterator<Property> iter = data.realPropertyIterator();
 389  0
             while (iter.hasNext()) {
 390  0
                 Property p = iter.next();
 391  0
                 String v = (String) p.getValue();
 392  
                 //FIXME: do we need to do a search? is this method ever going to be called?
 393  0
                 KSItemLabel item = createItem(v, "Display: " + v, hasDetails);
 394  0
                 addItem(item);
 395  0
             }
 396  
         }
 397  0
     }
 398  
 
 399  
     @Override
 400  
     public void addFocusLostCallback(Callback<Boolean> callback) {
 401  0
         if (picker != null)
 402  0
             picker.addFocusLostCallback(callback);
 403  0
     }
 404  
 
 405  0
     public static class ItemDataHelper implements DataHelper {
 406  
 
 407  
         @Override
 408  
         public String parse(Data data) {
 409  
             String result;
 410  0
             if (data == null) {
 411  0
                 return null;
 412  
             }
 413  0
             result = (String) data.get(DISPLAY);
 414  0
             return result;
 415  
         }
 416  
 
 417  
         @Override
 418  
         public String getKey(Data data) {
 419  
             String result;
 420  0
             if (data == null) {
 421  0
                 return null;
 422  
             }
 423  0
             result = (String) data.get(VALUE);
 424  0
             return result;
 425  
         }
 426  
 
 427  
     }
 428  
 
 429  
     @Override
 430  
     public void setValue(String id, String translation) {
 431  0
         clear();
 432  0
         if (id != null && translation != null) {
 433  0
             addItem(createItem(id, translation, hasDetails), false);
 434  
         }
 435  0
     }
 436  
 
 437  
     @Override
 438  
     public void setValue(Map<String, String> translations) {
 439  0
         clear();
 440  0
         if (translations != null) {
 441  0
             for (Entry<String, String> e : translations.entrySet()) {
 442  0
                 addItem(createItem(e.getKey(), e.getValue(), hasDetails), false);
 443  
             }
 444  
         }
 445  0
     }
 446  
 
 447  
     @Override
 448  
     public Widget getInputWidget() {
 449  0
         if (picker == null) {
 450  0
             return valuesPanel;
 451  
         }
 452  0
         return picker.getInputWidget();
 453  
     }
 454  
 
 455  
         @Override
 456  
         public HashSet<String> getCrossConstraints() {
 457  0
                 if(picker!=null){
 458  0
                         return picker.getCrossConstraints();
 459  
                 }
 460  0
                 return new HashSet<String>();
 461  
         }
 462  
 
 463  
         @Override
 464  
         public void reprocessWithUpdatedConstraints() {
 465  0
                 if(picker!=null){
 466  0
                         picker.reprocessWithUpdatedConstraints();
 467  
                 }
 468  0
         }
 469  
 
 470  
         public KSButton getAddItemButton() {
 471  0
                 return addItemButton;
 472  
         }
 473  
 
 474  
         public VerticalFlowPanel getMainPanel() {
 475  0
                 return mainPanel;
 476  
         }
 477  
 
 478  
         public KSPicker getPicker() {
 479  0
                 return picker;
 480  
         }
 481  
 
 482  
         @Override
 483  
         public ErrorLevel processValidationResult(ValidationResultInfo vr) {
 484  
                 //Validation results passed to selected list should be in the form foo/bar/1
 485  0
                 String indexNumber=vr.getElement().substring(vr.getElement().lastIndexOf('/')+1);
 486  0
                 int index = Integer.parseInt(indexNumber);
 487  0
                 if(index<getSelectedItems().size()){
 488  0
                         KSItemLabel itemLabel = getSelectedItems().get(index);
 489  0
                         return itemLabel.processValidationResult(vr, vr.getElement().substring(0, vr.getElement().lastIndexOf('/')));
 490  
                 }else{
 491  0
                         return ErrorLevel.OK;
 492  
                 }
 493  
         }
 494  
 
 495  
         @Override
 496  
         public boolean shouldProcessValidationResult(ValidationResultInfo vr) {
 497  
                 //Check if the element ends in a number and is thus a list of primitives (foo/bar/2)
 498  0
                 if(vr.getElement()!=null&&vr.getElement().matches("^\\S+/[0-9]+$")){
 499  0
                         return true;
 500  
                 }
 501  0
                 return false;
 502  
         }
 503  
 
 504  
 }