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