001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @author <a href="mailto:oliver.wehrens@aei.mpg.de">Oliver Wehrens</a>
004: * @version $Id: ListBoxItemTag.java 6385 2007-10-25 14:02:26Z wehrens $
005: */
006: package org.gridsphere.provider.portletui.tags;
007:
008: import org.gridsphere.provider.portletui.beans.BaseComponentBean;
009: import org.gridsphere.provider.portletui.beans.ListBoxBean;
010: import org.gridsphere.provider.portletui.beans.ListBoxItemBean;
011:
012: import javax.servlet.jsp.JspException;
013: import java.util.List;
014: import java.util.Locale;
015: import java.util.ResourceBundle;
016:
017: /**
018: * A <code>ListBoxItemtag</code> represents a list box element
019: */
020: public class ListBoxItemTag extends BaseComponentTag {
021:
022: protected ListBoxItemBean listboxitem = null;
023: protected boolean selected = false;
024:
025: /**
026: * Sets the element to be selected
027: *
028: * @param selected is true if the element is selected
029: */
030: public void setSelected(boolean selected) {
031: this .selected = selected;
032: }
033:
034: /**
035: * Return true if this element is selected
036: *
037: * @return true if this element is selected, false otherwise
038: */
039: public boolean getSelected() {
040: return selected;
041: }
042:
043: /**
044: * Checks to see if listbox tag already contains this list item
045: *
046: * @param listboxTag the listbox tag
047: * @return true if the listbox tag conatins this list item
048: */
049: protected boolean itemExists(ListBoxTag listboxTag) {
050: ListBoxBean listbox = listboxTag.getListBoxBean();
051: List<BaseComponentBean> beans = listbox.getBeans();
052: for (BaseComponentBean listboxitem : beans) {
053: if (name != null) {
054: if (name.equals(listboxitem.getName())) {
055: listboxitem.setValue(value);
056: return true;
057: }
058: }
059: if (value.equals(listboxitem.getValue())) {
060: return true;
061: }
062: }
063: return false;
064: }
065:
066: /**
067: * Deselects an element if not multiple selection and an existing list item is already selected
068: *
069: * @param listboxTag the list box tag
070: */
071: protected void checkSelectedEntries(ListBoxTag listboxTag) {
072: if (selected) {
073: if (!listboxTag.getMultiple()) {
074: ListBoxBean listbox = listboxTag.getListBoxBean();
075: List<BaseComponentBean> beans = listbox.getBeans();
076: for (BaseComponentBean bean : beans) {
077: ListBoxItemBean listboxitem = (ListBoxItemBean) bean;
078: if (listboxitem.isSelected()) {
079: selected = false;
080: }
081: }
082: }
083: }
084: }
085:
086: public int doEndTag() throws JspException {
087:
088: ListBoxTag listboxTag = (ListBoxTag) getParent();
089: if (listboxTag != null) {
090: //System.err.println("Setting action param bean: " + name + " " + value);
091: ListBoxItemBean listboxitem = new ListBoxItemBean();
092: if (key != null) {
093: Locale locale = pageContext.getRequest().getLocale();
094: ResourceBundle bundle = ResourceBundle.getBundle(
095: "Portlet", locale);
096: value = bundle.getString(key);
097: listboxitem.setValue(value);
098: }
099: this .setBaseComponentBean(listboxitem);
100: // check that item doesn't already exist
101: if (!itemExists(listboxTag)) {
102: checkSelectedEntries(listboxTag);
103: listboxitem.setSelected(selected);
104: listboxTag.addTagBean(listboxitem);
105: }
106: }
107:
108: return EVAL_PAGE;
109: }
110:
111: }
|