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