MapPicker dataProvider for Vaue Picker

/*Please, please, please, just use the latest ExtLib*/

package org.openntf.pickerdata;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.extlib.component.picker.data.IPickerEntry;
import com.ibm.xsp.extlib.component.picker.data.IPickerOptions;
import com.ibm.xsp.extlib.component.picker.data.IPickerResult;
import com.ibm.xsp.extlib.component.picker.data.IValuePickerData;
import com.ibm.xsp.extlib.component.picker.data.SimplePickerResult;

public class MapPicker implements IValuePickerData, Serializable {
	private static final long serialVersionUID = 1L;

	private Map<String, String> options_ = null;

	public MapPicker() {
	}

	// Mapo should be label / value
	public MapPicker(Map<String, String> options) {
		options_ = options;
	}

	public String[] getSourceLabels() {
		return null;
	}

	public boolean hasCapability(int capability) {
		return false;
	}

	/*
	 * This method appears to be the one that gets the entries for the picker
	 *
	 * (non-Javadoc)
	 * @see com.ibm.xsp.extlib.component.picker.data.IPickerData#readEntries(com.ibm.xsp.extlib.component.picker.data.IPickerOptions)
	 */
	public IPickerResult readEntries(IPickerOptions options) {
		String startKey = options.getStartKey();
		String key = options.getKey();
		int start = options.getStart();
		int count = options.getCount();
		int searchIndex = 0;
		LinkedHashMap<String, String> opts = filteredOptions(key, startKey, start, searchIndex);
		List<IPickerEntry> entries = new ArrayList<IPickerEntry>();
		Iterator<String> it = opts.keySet().iterator();
		while (it.hasNext()) {
			String mapKey = it.next();
			entries.add(new SimplePickerResult.Entry(opts.get(mapKey), mapKey));
		}

		return new SimplePickerResult(entries, -1);
	}

	private LinkedHashMap<String, String> filteredOptions(String key, String startKey, int start, int searchIndex) {
		LinkedHashMap<String, String> retVal = new LinkedHashMap<String, String>();
		if (StringUtil.isNotEmpty(key)) {
			// We've got a typeahead key passed in, filter to entries beginning with that key
			Iterator<String> it = options_.keySet().iterator();
			while (it.hasNext()) {
				String mapKey = it.next();
				if (StringUtil.startsWithIgnoreCase(mapKey, key)) {
					retVal.put(mapKey, options_.get(mapKey));
				}
			}
		} else if (StringUtil.isNotEmpty(startKey)) {
			// We've got a search key passed in, jump to that entry and add all remaining entries
			Iterator<String> it = options_.keySet().iterator();
			boolean found = false;
			while (it.hasNext()) {
				String mapKey = it.next();
				if (found || StringUtil.startsWithIgnoreCase(mapKey, startKey)) {
					retVal.put(mapKey, options_.get(mapKey));
					found = true;
				}
			}
		} else {
			retVal.putAll(options_);
		}
		return retVal;
	}

	/*
	 * This method appears to be the one that is used for validation, to get an entry based on a value or values in the relevant component.
	 * The ArrayList only has values, so check values passed in and return those that exist in the options
	 *
	 * (non-Javadoc)
	 * @see com.ibm.xsp.extlib.component.picker.data.IPickerData#loadEntries(java.lang.Object[], java.lang.String[])
	 */
	public List<IPickerEntry> loadEntries(Object[] values, String[] attributes) {
		List<IPickerEntry> entries = new ArrayList<IPickerEntry>();
		if (null != values) {
			for (int i = 0; i < values.length; i++) {
				String checkStr = values[i].toString();
				if (StringUtil.isNotEmpty(checkStr)) {
					Iterator<String> it = options_.keySet().iterator();
					while (it.hasNext()) {
						String mapKey = it.next();
						if (StringUtil.equals(checkStr, options_.get(mapKey))) {
							entries.add(new SimplePickerResult.Entry(options_.get(mapKey), mapKey));
							break;
						}
					}
				}
			}
		}
		return entries;
	}
}





Corresponding dataProvider to http://openntf.org/XSnippets.nsf/snippet.xsp?id=listpicker-dataprovider-for-value-picker for passing in label-value pairs (Map key is the label, Map value is the value).

**PLEASE, PLEASE, PLEASE, JUST USE THE LATEST EXTLIB. THIS WAS INCORPORATED INTO XPAGES EXTENSION LIBRARY 9.0.1v12**

<xp:dataContext var="myValuesMap" value="${myBean.keyTree}"></xp:dataContext>

This uses EL on page load for better performance, mapping to the getKeyTree() method.
Then on the Value Picker, compute the dataProvider property of the Value Picker, like so:

dataProvider="${javascript:new org.openntf.pickerdata.ListPicker(myValuesMap)}"

Java
Paul S Withers
November 15, 2013 7:41 AM
Rating
27

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...