⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select.java

📁 这是一个mvc模式
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jsp.tags.dapact.tags.html;

import java.io.IOException;
import java.util.AbstractList;
import java.util.HashMap;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import jsp.tags.dapact.BaseDataTagSupport;
import jsp.tags.dapact.conf.UserClassFactory;
import jsp.tags.dapact.lookup.BaseLookupValue;
import jsp.tags.dapact.lookup.BaseSelectOptionInf;
import jsp.tags.dapact.tags.data.DataException;
import jsp.tags.dapact.tags.data.RetrieveValueByNameIteratorInf;
import jsp.tags.dapact.util.TagUtil;

/**
 * Title:        Data Aware Processing And Control Tags
 * Description:  Tag library for the processing and controlling the input and output of data.
 * Copyright:    LGPL (http://www.gnu.org/copyleft/lesser.html)
 * Compile Date: @compile_date@
 * @author Allen M Servedio
 * @amp_sign@version @VERSION@
 */

/**
 * Implements a data aware HTML <select> tag.
 */
public class Select extends BaseDataTagSupport
{
  /**
   * Default constructor.
   */
  public Select()
  {
  }

  /**
   * Called when the starting tag is found - writes the start tag and its attributes.
   *
   * @return EVAL_BODY_INCLUDE so that it processes what ever is in the body.
   */
  public int doStartTag() throws JspException
  {
    // The super class will lookup and set the new value in the new value property.
    super.doStartTag();

    Object temp = getNewValue();
    if (temp != null)
    {
      String[] newVal = null;
      if (temp instanceof String[])
      {
        newVal = (String[])temp;
      }
      else
      {
        newVal = new String[1];
        newVal[0] = temp.toString();
      }
      setValue(newVal);
    }

    try
    {
      JspWriter out = pageContext.getOut();
      out.write("<select");
      out.write(TagUtil.getAllAttrAsStr(getValues(), this));
      out.write(">");
      String list = getOptionsList();
      if (list != null)
      {
        out.write(list);
      }
    }
    catch (IOException e)
    {
      throw new JspException("JspWriter not there: " + e);
    }
    return EVAL_BODY_INCLUDE;
  }

  /**
   * <p>Retrieve the option values for the select list. Three other properties will
   * effect the output of this function: getAdddefaultsel(), getValue(), and getType().
   * getAdddefaultsel() will be used to determine if a default selection should be
   * added to the list (the lookup class should attend to this by placing the default
   * value as the first return value). getValue() will specify what the currently selected value in
   * the list is. getType() will specify what type of options list to generate.</p>
   *
   * <p>This now supports objects that implement the jsp.tags.dapact.tags.data.RetrieveValueByNameIteratorInf
   * interface. It has two properties (optionKeyName and optionValueName) that allow
   * users to specify what names to send to the data object to retrieve the key
   * and value for the options. If no names are supplied, it will try a key name of
   * <b>key</b> and a value name of <b>value</b>.</p>
   *
   * <p>When this makes its request to the BaseLookupValue instance, it expects to
   * get back an array of BaseSelectOptionInf's or an java.util.AbstractList of BaseSelectOptionInf's.</p>
   *
   * @return an option list of values and text (display) labels or null if there are none.
   */
  protected String getOptionsList() throws JspException
  {
    String result = null;

    // Retrieve the lookup object to get the list of options.
    BaseLookupValue lookup = UserClassFactory.getLookupValueInstance(getLookupValueName());

    try
    {
      String[] values = getValue();
      Object selectedVals = lookup.lookupValue(getType(), values, this, pageContext);

      if (selectedVals != null)
      {
        BaseSelectOptionInf[] selectedValsArr = null;
        AbstractList selectedValsArrList = null;
        RetrieveValueByNameIteratorInf selectedValsIterator = null;
        int len = 0;
        if (selectedVals instanceof BaseSelectOptionInf[])
        {
          selectedValsArr = (BaseSelectOptionInf[])selectedVals;
          len = selectedValsArr.length;
        }
        else if (selectedVals instanceof RetrieveValueByNameIteratorInf)
        {
          selectedValsIterator = (RetrieveValueByNameIteratorInf)selectedVals;
          len = 1;
        }
        else
        {
          selectedValsArrList = (AbstractList)selectedVals;
          len = selectedValsArrList.size();
        }
        if (selectedVals != null)
        {
          HashMap table = new HashMap(((values != null) ? values.length : 0));
          if (values != null)
          {
            for (int i=0; i < values.length; i++)
            {
              String value = values[i];
              table.put(value, value);
            }
          }

          // Try to initialize the buffer to something close to its final value. It
          //  multiplies the length of the option size by two to try to estimate the value
          //  and text data.
          StringBuffer buff = new StringBuffer((EST_OPTION_SIZE*len*2));

          // If the selected values is and iterator use this.
          if (selectedValsIterator != null)
          {
            String keyName = getOptionKeyName();
            if (keyName == null)
            {
              keyName = DEFAULT_KEY_NAME;
            }
            String valueName = getOptionValueName();
            if (valueName == null)
            {
              valueName = DEFAULT_VALUE_NAME;
            }
            try
            {
              boolean more = true;
              while (more)
              {
                Object key = selectedValsIterator.retrieveValueByName(this, keyName);
                Object value = selectedValsIterator.retrieveValueByName(this, valueName);
                if ((key != null) && (value != null))
                {
                  buff.append(START_OPTION);
                  buff.append(key);
                  if (table.containsKey(key))
                  {
                    buff.append(MIDDLE_WITH_SELECTION_OPTION);
                  }
                  else
                  {
                    buff.append(MIDDLE_OPTION);
                  }
                  buff.append(value);
                  buff.append(END_OPTION);
                }
                more = selectedValsIterator.next();
              }
            }
            catch (DataException e)
            {
              UserClassFactory.getLogger().log("Exception while processing keys and values for select options.", e);
            }
          }
          // Else, look through the array.
          else
          {
            for (int i=0; i < len; i++)
            {
              BaseSelectOptionInf selectOptionObj = null;
              if (selectedValsArr != null)
              {
                selectOptionObj = selectedValsArr[i];
              }
              else
              {
                selectOptionObj = (BaseSelectOptionInf)selectedValsArrList.get(i);
              }
              if (selectOptionObj != null)
              {
                String value = selectOptionObj.getValue();
                buff.append(START_OPTION);
                buff.append(value);
                if (table.containsKey(value))
                {
                  buff.append(MIDDLE_WITH_SELECTION_OPTION);
                }
                else
                {
                  buff.append(MIDDLE_OPTION);
                }
                buff.append(selectOptionObj.getText());
                buff.append(END_OPTION);
              }
            }
          }
          result = buff.toString();
        }
      }
    }
    catch (ClassCastException e)
    {
      UserClassFactory.getLogger().log("Exception while casting the option list.", e);
    }
    return result;
  }

  /**
   * Called at the end of the tag. Will output the end body tag text and return that
   * it wants to have the rest of the page processed.
   *
   * @exception JspException general JSP exception.
   *
   * @return EVAL_PAGE so that it continues to process the rest of the page.
   */
  public int doEndTag() throws JspException
  {
    super.doEndTag();
    try
    {
      JspWriter out = pageContext.getOut();
      out.write("</select>");
    }
    catch (IOException e)
    {
      throw new JspException("JspWriter not there: " + e);
    }
    return EVAL_PAGE;
  }

  /*=============================================================================*/
  /*                           PROPERTIES/ATTRIBUTES                             */
  /*=============================================================================*/
  /**
   * Constant that specifies the value of the name of the adddefaultsel property.
   */
  public static final String ADDDEFAULTSEL_PROP = TagUtil.HIDE_ATTR_CHAR + "adddefaultsel";
  /**
   * Get the value of the adddefaultsel property - if true, a default selection will be
   * added to the option list.
   *
   * @return the value of the adddefaultsel property.
   */
  public Boolean getAdddefaultsel()
  {
    return (Boolean)getValue(ADDDEFAULTSEL_PROP);
  }
  /**
   * Set the value of the adddefaultsel property - if true, a default selection will be
   * added to the option list.
   *
   * @param value the value of the adddefaultsel property.
   */
  public void setAdddefaultsel(Boolean value)
  {
    setValue(ADDDEFAULTSEL_PROP, value);
  }

  /**
   * Constant that specifies the value of the name of the accesskey property.
   */
  public static final String ACCESSKEY_PROP = "accesskey";
  /**
   * Get the value of the accesskey property.
   *
   * @return the value of the accesskey property.
   */
  public String getAccesskey()
  {
    return (String)getValue(ACCESSKEY_PROP);
  }
  /**
   * Set the value of the accesskey property.
   *
   * @param value the value of the accesskey property.
   */
  public void setAccesskey(String value)
  {
    setValue(ACCESSKEY_PROP, value);
  }

  /**
   * Constant that specifies the value of the name of the _class property.
   */
  public static final String _CLASS_PROP = "class";
  /**
   * Get the value of the _class property.
   *
   * @return the value of the _class property.
   */
  public String get_class()
  {
    return (String)getValue(_CLASS_PROP);
  }
  /**
   * Set the value of the _class property.
   *
   * @param value the value of the _class property.
   */
  public void set_class(String value)
  {
    setValue(_CLASS_PROP, value);
  }

  /**
   * Constant that specifies the value of the name of the dir property.
   */
  public static final String DIR_PROP = "dir";
  /**
   * Get the value of the dir property.
   *
   * @return the value of the dir property.
   */
  public String getDir()
  {
    return (String)getValue(DIR_PROP);
  }
  /**
   * Set the value of the dir property.
   *
   * @param value the value of the dir property.
   */
  public void setDir(String value)
  {
    setValue(DIR_PROP, value);
  }

  /**
   * Constant that specifies the value of the name of the disabled property.
   */
  public static final String DISABLED_PROP = "disabled";
  /**
   * Get the value of the disabled property.
   *
   * @return the value of the disabled property.
   */
  public String getDisabled()
  {
    return (String)getValue(DISABLED_PROP);
  }
  /**
   * Set the value of the disabled property.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -