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