@Override protected void setDefaultValues(MultiselectData opt) { super.setDefaultValues(opt); @SuppressWarnings("unchecked") TemplateMultiselectData<T> opt2 = (TemplateMultiselectData<T>) opt; if (defaultItems != null) { opt2.update(defaultItems); } if (defaultSelectedItems != null) { opt2.selectMultipleItems(defaultSelectedItems); } }
@Override public TemplateMultiselectData<T> createNewSession() { return new TemplateMultiselectData<>(this); }
public boolean addItem(T item) { String[] valLab = getValueAndLabel(item); writeLock(); try { if (listOptions.containsKey(item)) return false; addOption(valLab[1], valLab[0], false); listOptions.put(item,false); } finally { writeUnlock(); } return true; }
public boolean removeItem(T item) { String[] valLab = getValueAndLabel(item); writeLock(); try { removeOption(valLab[0]); return listOptions.remove(item) != null; } finally { writeUnlock(); } }
public void selectItem(T item) { String[] valLab = getValueAndLabel(item); writeLock(); try { selectSingleOption(valLab[0]); // locks listOptions.put(item, true); } finally { writeUnlock(); } }
public Map<T, Boolean> getSelectionItems() { readLock(); try { return new LinkedHashMap<>(listOptions); } finally { readUnlock(); } }
@Override public void selectMultipleOptions(Collection<String> selectedOptions) { writeLock(); try { super.selectMultipleOptions(selectedOptions); Collection<String> selected = getSelectedValues(); for (Map.Entry<T, Boolean> entry: listOptions.entrySet()) { boolean sel = selected.contains(getValueAndLabel(entry.getKey())[0]); entry.setValue(sel); } } finally { writeUnlock(); } }
public void selectMultipleItems(Collection<T> items) { List<String> selected = new ArrayList<>(); writeLock(); try { for (T it: items) { String[] valLab = getValueAndLabel(it); selected.add(valLab[0]); if (listOptions.containsKey(valLab)) { listOptions.put(it, true); } } selectMultipleOptions(selected); } finally { writeUnlock(); } }
@Override public void changeSelection(String value, boolean newState) { writeLock(); try { super.changeSelection(value, newState); for (Map.Entry<T, Boolean> entry: listOptions.entrySet()) { if (value.equals(getValueAndLabel(entry.getKey())[0])) { entry.setValue(newState); break; } } } finally { writeUnlock(); } }
private void setResourceOptions() { List<? extends R> resources; if (resourceType == null) { resources = Collections.emptyList(); } else { resources = ra.getResources(resourceType); } super.update(resources); }
/** * Returns a map with all items as keys. The values indicate whether the respective item * is selected. * @param req * @return */ public Map<T, Boolean> getSelectionItems(OgemaHttpRequest req) { return getData(req).getSelectionItems(); }
public boolean addItem(T item, OgemaHttpRequest req) { return getData(req).addItem(item); }
public List<T> getItems(OgemaHttpRequest req) { return getData(req).getItems(); }
protected String[] getValueAndLabel(T item) { @SuppressWarnings("unchecked") DisplayTemplate<T> template = ((TemplateMultiselect<T>) widget).template; String label = template.getLabel(item, (getInitialRequest() != null ? getInitialRequest().getLocale() : OgemaLocale.ENGLISH)); String value = template.getId(item); Objects.requireNonNull(label); Objects.requireNonNull(value); return new String[]{value, label}; }
public JSONObject onPOST(String json, OgemaHttpRequest req) { JSONObject result; writeLock(); try { result = super.onPOST(json, req); Collection<String> selected = getSelectedValues(); for (Map.Entry<T, Boolean> entry: listOptions.entrySet()) { String val = getValueAndLabel(entry.getKey())[0]; boolean sel = selected.contains(val); entry.setValue(sel); } } finally { writeUnlock(); } return result; }
public List<T> getItems() { readLock(); try { return new ArrayList<>(listOptions.keySet()); } finally { readUnlock(); } }
public void update(Collection<T> items, OgemaHttpRequest req) { getData(req).update(items); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void update(Collection<? extends T> items) { final Collection<T> list; final Comparator<? super T> comparator = ((TemplateMultiselect<T>) widget).comparator; if (comparator == null) list = (Collection) items; else { list = new ArrayList<T>(items); Collections.sort((List) list, comparator); } Map<String,String> map = new LinkedHashMap<>(); for (T item: list) { String[] valLab = getValueAndLabel(item); map.put(valLab[0], valLab[1]); } writeLock(); try { update(map); Collection<String> selected = getSelectedValues(); listOptions.clear(); for (T item: items) { listOptions.put(item, selected.contains(getValueAndLabel(item)[0])); } } finally { writeUnlock(); } }