/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.ajax4jsf.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class OrderingListTestBean {
	private Converter converter;
	private List<Item> items;
	private Collection<Item> selection;
	private Object activeItem;
	
	public OrderingListTestBean() {
		converter = new ItemConverter();
		items = new ArrayList<Item>();
		for (int i = 0; i < 4; i++) {
			items.add(new Item("item" + i));
		}
		selection = new HashSet<Item>();
	}
	
	public Object getActionResult() {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("actionResult");
	}
	
	public Collection<Item> getSelection() {
		return selection;
	}
	
	public void setSelection(Collection<Item> selection) {
		this.selection = selection;
	}

	public Object getActiveItem() {
		return activeItem;
	}
	
	public void setActiveItem(Object activeItem) {
		this.activeItem = activeItem;
	}
	
	public Converter getConverter() {
		return converter;
	}

	public void setConverter(Converter converter) {
		this.converter = converter;
	}
	
	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}
	
	public String getSelectionString() {
		StringBuffer buff = new StringBuffer();
		for (Iterator<Item> it = selection.iterator(); it.hasNext();) {
			Item item = it.next();
			buff.append(item.getName());
			if (it.hasNext()) {
				buff.append(',');
			}
		}
		return buff.toString();
	}


	private class ItemConverter implements Converter {

		/* (non-Javadoc)
		 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
		 */
		public Object getAsObject(FacesContext context, UIComponent component,
				String value) {
			return new Item(value);
		}

		/* (non-Javadoc)
		 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
		 */
		public String getAsString(FacesContext context, UIComponent component,
				Object value) {
			Item optionItem = (Item) value;
			return optionItem.getName();
		}

	}
	
	public class Item implements Serializable {

		private static final long serialVersionUID = 1083694594537030790L;
		
		private String name;
		
		public String action() {
			FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("actionResult", name);
			return null;
		}
		
		public Item(String name) {
			super();
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
		
		public String toString() {
			return this.getClass().getSimpleName() + " [" + name + "]";
		}
		
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}
		
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Item other = (Item) obj;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			
			return true;
		}

	}
	
}
