All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zkoss.zk.ui.event.SelectEvent Maven / Gradle / Ivy

There is a newer version: 10.0.0-jakarta
Show newest version
/* SelectEvent.java

	Purpose:
		
	Description:
		
	History:
		Thu Jun 16 18:05:51     2005, Created by tomyeh

Copyright (C) 2005 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
	This program is distributed under LGPL Version 2.1 in the hope that
	it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.zk.ui.event;

import java.util.Set;
import java.util.Collections;
import java.util.Map;
import java.util.List;

import static org.zkoss.lang.Generics.cast;

import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.au.AuRequest;
import org.zkoss.zk.au.AuRequests;

/**
 * Represents an event cause by user's the list selection is changed
 * at the client.
 * 
 * @author tomyeh
 */
@SuppressWarnings("serial")
public class SelectEvent extends Event {
	private final Set _selectedItems;
	private final Set _prevSelectedItems;
	private final Set _unselectedItems;
	private final Set _prevSelectedObjects;
	private final Set _selectedObjects;
	private final Set _unselectedObjects;
	private final T _ref;
	private final int _keys;

	/** Indicates whether the Alt key is pressed.
	 * It might be returned as part of {@link #getKeys}.
	 */
	public static final int ALT_KEY = MouseEvent.ALT_KEY;
	/** Indicates whether the Ctrl key is pressed.
	 * It might be returned as part of {@link #getKeys}.
	 */
	public static final int CTRL_KEY = MouseEvent.CTRL_KEY;
	/** Indicates whether the Shift key is pressed.
	 * It might be returned as part of {@link #getKeys}.
	 */
	public static final int SHIFT_KEY = MouseEvent.SHIFT_KEY;

	/** Converts an AU request to a select event.
	 * @since 5.0.0
	 */
	public static final  SelectEvent 
			getSelectEvent(AuRequest request) {
		return getSelectEvent(request, null);
	}
	
	/** Converts an AU request to a select event.
	 * @since 6.0.0
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static final  SelectEvent 
			getSelectEvent(AuRequest request, SelectedObjectHandler handler) {
		final Map data = request.getData();
		final Desktop desktop = request.getDesktop();
		final List sitems = cast((List)data.get("items"));
		final Set items = AuRequests.convertToItems(desktop, sitems);
		final Set prevSelectedItems = (Set) (handler == null ? null : handler.getPreviousSelectedItems());
		final Set unselectedItems = (Set) (handler == null ? null : handler.getUnselectedItems());
		final Set prevSelectedObjects = (Set) (handler == null ? null : handler.getPreviousSelectedObjects());
		final Set unselectedObjects = (Set) (handler == null ? null : handler.getUnselectedObjects());
		final Set objs = (Set) (handler == null ? null : handler.getObjects(items));
		return new SelectEvent(request.getCommand(), request.getComponent(),
			items, prevSelectedItems, unselectedItems, objs, prevSelectedObjects, unselectedObjects, (T) desktop.getComponentByUuidIfAny((String)data.get("reference")),
			null, AuRequests.parseKeys(data));
	}
	
	/**
	 * A handle to retrieve selected objects from selected items (components)
	 * if possible.
	 */
	public interface SelectedObjectHandler {
		/**
		 * Return selected objects from selected items if possible.
		 */
		public Set getObjects(Set items);
		/**
		 * Return the previous selected items from the target component.
		 * @since 7.0.0
		 */		
		public Set getPreviousSelectedItems();
		
		/**
		 * Return the unselected items from the target component.
		 * @since 7.0.1
		 */
		public Set getUnselectedItems();
		
		/**
		 * Return the previous selected objects from the target component.
		 * @since 7.0.1
		 */
		public Set getPreviousSelectedObjects();
		
		/**
		 * Return the unselected objects from the target component.
		 * @since 7.0.1
		 */
		public Set getUnselectedObjects();
	}
	
	/** Constructs a selection event.
	 * @param selectedItems a set of items that shall be selected.
	 */
	public SelectEvent(String name, Component target, Set selectedItems) {
		this(name, target, selectedItems, null, 0);
	}

	/** Constructs a selection event.
	 * @param selectedItems a set of items that shall be selected.
	 */
	public SelectEvent(String name, Component target, Set selectedItems, T ref) {
		this(name, target, selectedItems, ref, 0);
	}
	/** Constructs a selection event.
	 * @param selectedItems a set of items that shall be selected.
	 * @param keys a combination of {@link #CTRL_KEY}, {@link #SHIFT_KEY}
	 * and {@link #ALT_KEY}.
	 * @since 3.6.0
	 */
	public SelectEvent(String name, Component target, Set selectedItems, T ref, int keys) {
		this(name, target, selectedItems, null, null, null, null, null, ref, null, keys);
	}
	
	/** Constructs a selection event containing the data objects that model
	 * provided.
	 * 
	 * @param selectedItems a set of items that shall be selected.
	 * @param selectedObjects a set of data objects that shall be selected.
	 * @param data an arbitrary data
	 * @param keys a combination of {@link #CTRL_KEY}, {@link #SHIFT_KEY}
	 * and {@link #ALT_KEY}.
	 * @since 6.0.0
	 */
	public SelectEvent(String name, Component target, Set selectedItems, Set previousSelectedItems,
			Set unselectedItems, Set selectedObjects, Set prevSelectedObjects, Set unselectedObjects, T ref, Object data, int keys) {
		super(name, target, data);

		if (selectedItems != null)
			_selectedItems = selectedItems;
		else _selectedItems = Collections.emptySet();
		
		if (previousSelectedItems != null)
			_prevSelectedItems = previousSelectedItems;
		else _prevSelectedItems = Collections.emptySet();
		
		if (unselectedItems != null)
			_unselectedItems = unselectedItems;
		else _unselectedItems = Collections.emptySet();
		
		if (selectedObjects != null)
			_selectedObjects = selectedObjects;
		else _selectedObjects = Collections.emptySet();
		
		if (prevSelectedObjects != null)
			_prevSelectedObjects = prevSelectedObjects;
		else _prevSelectedObjects = Collections.emptySet();
		
		if (unselectedObjects != null)
			_unselectedObjects = unselectedObjects;
		else _unselectedObjects = Collections.emptySet();
		
		_ref = ref;
		_keys = keys;
	}
	
	/** Returns the selected items (never null).
	 */
	public final Set getSelectedItems() {
		return _selectedItems;
	}
	
	/** Returns the previous selected items (never null).
	 * @since 7.0.0
	 */
	public final Set getPreviousSelectedItems() {
		return _prevSelectedItems;
	}
	
	/** Returns the previous selected objects. The information is available
	 * only when the target component has a model.
	 * @since 7.0.1
	 */
	public final Set getPreviousSelectedObjects() {
		return _prevSelectedObjects;
	}
	
	/** Returns the unselected items.
	 * @since 7.0.1
	 */
	public final Set getUnselectedItems() {
		return _unselectedItems;
	}
	
	/** Returns the unselected objects. The information is available
	 * only when the target component has a model.
	 * @since 7.0.1
	 */
	public final Set getUnselectedObjects() {
		return _unselectedObjects;
	}
	
	/** Returns the selected objects (never null). The information is available
	 * only when the target component has a model. 
	 * @since 6.0.0
	 */
	public final Set getSelectedObjects() {
		return _selectedObjects;
	}

	/** Returns the reference item that is the component causing the onSelect 
	 * event(select or deselect) to be fired.
	 *
	 * 

It is null, if the onSelect event is not caused by listbox or tree or combobox. * Note: if not multiple, the {@link #getReference} is the same with the first item of {@link #getSelectedItems}. * @since 3.0.2 */ public T getReference() { return _ref; } /** Returns what keys were pressed when the mouse is clicked, or 0 if * none of them was pressed. * It is a combination of {@link #CTRL_KEY}, {@link #SHIFT_KEY} * and {@link #ALT_KEY}. * @since 3.6.0 */ public final int getKeys() { return _keys; } }