columntag.java

来自「displaytag-1.0修正版」· Java 代码 · 共 787 行 · 第 1/2 页

JAVA
787
字号
    public void setNowrap(String value)
    {
        this.attributeMap.put(TagConstants.ATTRIBUTE_NOWRAP, "nowrap");
    }

    /**
     * setter for the "valign" tag attribute.
     * @param value attribute value
     * @deprecated use css in "class" or "style"
     */
    public void setValign(String value)
    {
        this.attributeMap.put(TagConstants.ATTRIBUTE_VALIGN, value);
    }

    /**
     * setter for the "style" tag attribute.
     * @param value attribute value
     */
    public void setStyle(String value)
    {
        this.attributeMap.put(TagConstants.ATTRIBUTE_STYLE, value);
    }

    /**
     * setter for the "class" tag attribute.
     * @param value attribute value
     */
    public void setClass(String value)
    {
        this.attributeMap.put(TagConstants.ATTRIBUTE_CLASS, new MultipleHtmlAttribute(value));
    }

    /**
     * Adds a css class to the class attribute (html class suports multiple values).
     * @param value attribute value
     */
    public void addClass(String value)
    {
        Object classAttributes = this.attributeMap.get(TagConstants.ATTRIBUTE_CLASS);

        if (classAttributes == null)
        {
            this.attributeMap.put(TagConstants.ATTRIBUTE_CLASS, new MultipleHtmlAttribute(value));
        }
        else
        {
            ((MultipleHtmlAttribute) classAttributes).addAttributeValue(value);
        }
    }

    /**
     * setter for the "headerClass" tag attribute.
     * @param value attribute value
     */
    public void setHeaderClass(String value)
    {
        this.headerAttributeMap.put(TagConstants.ATTRIBUTE_CLASS, new MultipleHtmlAttribute(value));
    }

    /**
     * setter for the "decorator" tag attribute.
     * @param value attribute value
     */
    public void setDecorator(String value)
    {
        this.decorator = value;
    }

    /**
     * setter for the "sortProperty" tag attribute.
     * @param value attribute value
     */
    public void setSortProperty(String value)
    {
        this.sortProperty = value;
    }

    /**
     * Is this column configured for the media type?
     * @param mediaType the currentMedia type
     * @return true if the column should be displayed for this request
     */
    public boolean availableForMedia(MediaTypeEnum mediaType)
    {
        if (supportedMedia == null)
        {
            return true;
        }

        return this.supportedMedia.contains(mediaType);
    }

    /**
     * Tag setter.
     * @param media the space delimited list of supported types
     */
    public void setMedia(String media)
    {
        if (StringUtils.isBlank(media) || media.toLowerCase().indexOf("all") > -1)
        {
            this.supportedMedia = null;
            return;
        }
        this.supportedMedia = new ArrayList();
        String[] values = StringUtils.split(media);
        for (int i = 0; i < values.length; i++)
        {
            String value = values[i];
            if (!StringUtils.isBlank(value))
            {
                MediaTypeEnum type = MediaTypeEnum.fromName(value.toLowerCase());
                if (type == null)
                {
                    log.warn("Unrecognized value for attribute \"media\" value=\"" + value + "\"");
                }
                else
                {
                    this.supportedMedia.add(type);
                }
            }
        }
    }

    /**
     * Passes attribute information up to the parent TableTag.
     * <p>
     * When we hit the end of the tag, we simply let our parent (which better be a TableTag) know what the user wants to
     * do with this column. We do that by simple registering this tag with the parent. This tag's only job is to hold
     * the configuration information to describe this particular column. The TableTag does all the work.
     * </p>
     * @return int
     * @throws JspException if this tag is being used outside of a &lt;display:list...&gt; tag.
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException
    {
        TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);

        MediaTypeEnum currentMediaType = (MediaTypeEnum) this.pageContext.findAttribute(TableTag.PAGE_ATTRIBUTE_MEDIA);
        if (currentMediaType != null && !availableForMedia(currentMediaType))
        {
            if (log.isDebugEnabled())
            {
                log.debug("skipping column body, currentMediaType=" + currentMediaType);
            }
            return SKIP_BODY;
        }

        // add column header only once
        if (tableTag.isFirstIteration())
        {
            addHeaderToTable(tableTag);
        }

        if (!tableTag.isIncludedRow())
        {
            return super.doEndTag();
        }

        Cell cell;
        if (this.property == null)
        {

            Object cellValue;

            if (this.bodyContent != null)
            {
                String value = this.bodyContent.getString();

                if (value == null && this.nulls)
                {
                    value = TagConstants.EMPTY_STRING;
                }

                cellValue = value;
            }
            // BodyContent will be null if the body was not eval'd, eg an empty list.
            else
            {
                cellValue = Cell.EMPTY_CELL;
            }
            cell = new Cell(cellValue);

        }
        else
        {
            cell = Cell.EMPTY_CELL;
        }

        tableTag.addCell(cell);

        // cleanup non-attribute variables
        this.alreadySorted = false;

        return super.doEndTag();
    }

    /**
     * Adds the current header to the table model calling addColumn in the parent table tag. This method should be
     * called only at first iteration.
     * @param tableTag parent table tag
     * @throws DecoratorInstantiationException for error during column decorator instantiation
     * @throws ObjectLookupException for errors in looking up values
     */
    private void addHeaderToTable(TableTag tableTag) throws DecoratorInstantiationException, ObjectLookupException
    {
        // don't modify "title" directly
        String evalTitle = this.title;

        // title has precedence over titleKey
        if (evalTitle == null && (this.titleKey != null || this.property != null))
        {
            // handle title i18n
            evalTitle = tableTag.getProperties().geResourceProvider().getResource(
                this.titleKey,
                this.property,
                tableTag,
                this.pageContext);
        }

        HeaderCell headerCell = new HeaderCell();
        headerCell.setHeaderAttributes((HtmlAttributeMap) this.headerAttributeMap.clone());
        headerCell.setHtmlAttributes((HtmlAttributeMap) this.attributeMap.clone());
        headerCell.setTitle(evalTitle);
        headerCell.setSortable(this.sortable);
        headerCell.setColumnDecorator(DecoratorFactory.loadColumnDecorator(this.decorator));
        headerCell.setBeanPropertyName(this.property);
        headerCell.setShowNulls(this.nulls);
        headerCell.setMaxLength(this.maxLength);
        headerCell.setMaxWords(this.maxWords);
        headerCell.setAutoLink(this.autolink);
        headerCell.setGroup(this.group);
        headerCell.setSortProperty(this.sortProperty);

        // href and parameter, create link
        if (this.href != null)
        {
            Href colHref;

            // empty base url, use href with parameters from parent table
            if (StringUtils.isEmpty(this.href.getBaseUrl()))
            {
                colHref = new Href(tableTag.getBaseHref());
            }
            else
            {
                colHref = new Href(this.href);
            }

            if (this.paramId != null)
            {
                // parameter value is in a different object than the iterated one
                if (this.paramName != null || this.paramScope != null)
                {
                    // create a complete string for compatibility with previous version before expression evaluation.
                    // this approach is optimized for new expressions, not for previous property/scope parameters
                    StringBuffer expression = new StringBuffer();

                    // append scope
                    if (StringUtils.isNotBlank(this.paramScope))
                    {
                        expression.append(this.paramScope).append("Scope.");
                    }

                    // base bean name
                    if (this.paramId != null)
                    {
                        expression.append(this.paramName);
                    }
                    else
                    {
                        expression.append(tableTag.getName());
                    }

                    // append property
                    if (StringUtils.isNotBlank(this.paramProperty))
                    {
                        expression.append('.').append(this.paramProperty);
                    }

                    // evaluate expression.
                    // note the value is fixed, not based on any object created during iteration
                    // this is here for compatibility with the old version mainly
                    Object paramValue = tableTag.evaluateExpression(expression.toString());

                    // add parameter
                    colHref.addParameter(this.paramId, paramValue);
                }
                else
                {
                    // set id
                    headerCell.setParamName(this.paramId);

                    // set property
                    headerCell.setParamProperty(this.paramProperty);
                }
            }

            // sets the base href
            headerCell.setHref(colHref);

        }

        tableTag.addColumn(headerCell);

        if (log.isDebugEnabled())
        {
            log.debug("columnTag.addHeaderToTable() :: first iteration - adding header " + headerCell);
        }
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#release()
     */
    public void release()
    {
        super.release();
        this.attributeMap.clear();
        this.autolink = false;
        this.decorator = null;
        this.group = -1;
        this.headerAttributeMap.clear();
        this.href = null;
        this.maxLength = 0;
        this.maxWords = 0;
        this.nulls = false;
        this.paramId = null;
        this.paramName = null;
        this.paramProperty = null;
        this.paramScope = null;
        this.property = null;
        this.sortable = false;
        this.supportedMedia = null;
        this.title = null;
        this.titleKey = null;
        this.sortProperty = null;
    }

    /**
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException
    {
        TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
        if (tableTag == null)
        {
            throw new TagStructureException(getClass(), "column", "table");
        }

        // If the list is empty, do not execute the body; may result in NPE
        if (tableTag.isEmpty() || !tableTag.isIncludedRow())
        {
            return SKIP_BODY;
        }

        MediaTypeEnum currentMediaType = (MediaTypeEnum) this.pageContext.findAttribute(TableTag.PAGE_ATTRIBUTE_MEDIA);
        if (!availableForMedia(currentMediaType))
        {
            return SKIP_BODY;
        }

        return super.doStartTag();
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        return new ToStringBuilder(this, ShortToStringStyle.SHORT_STYLE) //
            .append("bodyContent", this.bodyContent) //$NON-NLS-1$
            .append("group", this.group) //$NON-NLS-1$
            .append("maxLength", this.maxLength) //$NON-NLS-1$
            .append("decorator", this.decorator) //$NON-NLS-1$
            .append("href", this.href) //$NON-NLS-1$
            .append("title", this.title) //$NON-NLS-1$
            .append("paramScope", this.paramScope) //$NON-NLS-1$
            .append("property", this.property) //$NON-NLS-1$
            .append("paramProperty", this.paramProperty) //$NON-NLS-1$
            .append("headerAttributeMap", this.headerAttributeMap) //$NON-NLS-1$
            .append("paramName", this.paramName) //$NON-NLS-1$
            .append("autolink", this.autolink) //$NON-NLS-1$
            .append("nulls", this.nulls) //$NON-NLS-1$
            .append("maxWords", this.maxWords) //$NON-NLS-1$
            .append("attributeMap", this.attributeMap) //$NON-NLS-1$
            .append("sortable", this.sortable) //$NON-NLS-1$
            .append("paramId", this.paramId) //$NON-NLS-1$
            .append("alreadySorted", this.alreadySorted) //$NON-NLS-1$
            .append("sortProperty", this.sortProperty) //$NON-NLS-1$
            .toString();
    }
}

⌨️ 快捷键说明

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