Menu

CSS styled DisplayProvider

2004-11-11
2013-04-17
  • Sebastian Beigel

    Hi,
    regarding modern XHTML/CSS2 templates, I wrote a HtmlCssDisplayProvider which is used to style table headings and their sorting with CSS.

    I adjusted the interface DisplayProvider and changed the method getHeaderCellPreProcess(String attributes) to getHeaderCellPreProcess(ColumnInfo columnInfo, ValueListInfo info) which gives me more flexibility in rendering (I think the changed signature and the resulting refactorings in the existing DisplayProviders are marginal and tolerable :)

    My getHeaderCellPreProcess method looks now like this:

        public String getHeaderCellPreProcess(ColumnInfo columnInfo, ValueListInfo info) {
        String css = "";
        if (columnInfo.getDefaultSort() != null) {
            String column = info.getSortingColumn();
            Integer direction = info.getSortingDirection();
            if (columnInfo.getPropertyName().equals(column)) {
            css = "class=\"sortable " + ((ValueListInfo.ASCENDING.equals(direction) ? "orderAsc" : "orderDesc")) + "\"";
            } else {
            css = "class=\"sortable\"";
            }

        }
        return "<th " + css + ">";
        }

    I removed the images in the getHeaderLabel method, so the resulting HTML is very clean and you have the possibility to complete control over the look via CSS, i.e.:

    #vlh TH.sortable A {
            background-repeat: no-repeat;
            background-position: right;
            display: block;
    }
    #vlh TH.sortable A {
            background-image: url(../pix/arrow_off.png);
    }
    #vlh TH.orderAsc A {
            background-image: url(../pix/arrow_down.png);
    }
    #vlh TH.orderDesc A {
            background-image: url(../pix/arrow_up.png);
    }

    I also adjusted DefaultPagingTag and removed the HTML-table and coverted it to <span> tags etc. to realize a XHTML/CSS2 layout (which are mandatory in WAI e.g.). I'd be happy to post my changes if you like to.

    Is this of any interest to you? I'd be happy to see my changes merged in your codebase :)

    Regards,
    Sebastian

     
    • jlkoidahl

      jlkoidahl - 2004-11-12

      I would love to see the HtmlCssDisplayProvider

       
      • Sebastian Beigel

        Ok, here is the source code for HtmlCssDispalyProvider (you also have to change the DisplayProvider interface and the implementing classes and also DefaultRowTag according to my first post)

        /**
        * Copyright (c) 2003 held jointly by the individual authors.           
        *                                                                         
        * This library is free software; you can redistribute it and/or modify it   
        * under the terms of the GNU Lesser General Public License as published     
        * by the Free Software Foundation; either version 2.1 of the License, or
        * (at your option) any later version.                                           
        *                                                                           
        * This library is distributed in the hope that it will be useful, but
        * WITHOUT ANY WARRANTY; with out 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.             
        *                                                                           
        * > http://www.gnu.org/copyleft/lesser.html                                 
        * > http://www.opensource.org/licenses/lgpl-license.php
        */
        package net.mlw.vlh.web.tag.support;

        import java.util.HashMap;
        import java.util.Map;

        import net.mlw.vlh.ValueListInfo;
        import net.mlw.vlh.web.ValueListConfigBean;
        import net.mlw.vlh.web.tag.TableInfo;

        /**
        * @author Sebastian Beigel
        * @version $Revision: 1.6 $ $Date: 2004/09/29 11:27:07 $
        */
        public class HtmlCssDisplayProvider implements DisplayProvider
        {

            public String getMimeType()
            {
                return null;
            }

            /**
             * Get the HTML that comes before the column text.
             *
             * @return The HTML that comes before the column text.
             */
            public String getHeaderRowPreProcess()
            {
                return "<tr>";
            }

            /**
             * Get the HTML that comes before the column text.
             *
             * @return The HTML that comes before the column text.
             */
            public String getHeaderCellPreProcess(ColumnInfo columnInfo, ValueListInfo info) {
            String attributes = columnInfo.getAttributes();
            String css = "";
            if (columnInfo.getDefaultSort() != null) {
                String column = info.getSortingColumn();
                Integer direction = info.getSortingDirection();
                if (columnInfo.getPropertyName().equals(column)) {
                css = " class=\&quot;sortable " + ((ValueListInfo.ASCENDING.equals(direction) ? "orderAsc" : "orderDesc")) + "\&quot;";
                } else {
                css = " class=\&quot;sortable\&quot;";
                }

            }
            return "<th " + ((attributes == null) ? "" : attributes) + css + ">";
            }
           
            /**
             * Formats the text to be displayed as the header by waraping it in a link if
             * sorting is enabled.
             *
             * @param columnInfo
             *           The ColumnInfo.
             * @param tableInfo
             *           The TableInfo.
             * @param info
             *           The ValueListInfo.
             * @return The formated HTML.
             */
            public String getHeaderLabel(ColumnInfo columnInfo, TableInfo tableInfo, ValueListInfo info, Map includeParameters)
            {
                StringBuffer sb = new StringBuffer();

                ValueListConfigBean config = tableInfo.getConfig();
                Map parameters = new HashMap(includeParameters);

                if (columnInfo.getDefaultSort() != null)
                {
                //Get the current sort column and direction.
                String column = info.getSortingColumn();
                Integer direction = info.getSortingDirection();

                sb.append("<a href=\&quot;").append(tableInfo.getUrl());

                parameters.put(ValueListInfo.PAGING_PAGE + tableInfo.getId(), "1");
                parameters.put(ValueListInfo.SORT_COLUMN + tableInfo.getId(), columnInfo.getPropertyName());
                parameters.put(ValueListInfo.SORT_DIRECTION + tableInfo.getId(), ((columnInfo.getPropertyName().equals(column)) ? (ValueListInfo.ASCENDING
                                                                           .equals(direction) ? ValueListInfo.DESCENDING : ValueListInfo.ASCENDING) : columnInfo.getDefaultSort()));

                sb.append(config.getLinkEncoder().encode(tableInfo.getPageContext(), parameters));

                sb.append("\&quot;>").append(columnInfo.getTitle()).append("</a>");

                }
                else
                {
                sb.append(columnInfo.getTitle());
                }

                return sb.toString();
            }

            /**
             * Get the HTML that comes after the column text.
             *
             * @return The HTML that comes after the column text.
             */
            public String getHeaderCellPostProcess()
            {
                return "</th>\n";
            }

        /**
             * Get the HTML that comes before the column text.
             *
             * @return The HTML that comes before the column text.
             */
            public String getHeaderRowPostProcess()
            {
                return "</tr>\n";
            }

            public String getRowPreProcess(Attributes attributes)
            {
                return (attributes == null) ? "<tr>" : "<tr " + attributes.getCellAttributesAsString() + ">";
            }

            public String getCellPreProcess(Attributes attributes)
            {
                return (attributes == null) ? "<td>" : "<td " + attributes.getCellAttributesAsString() + ">";
            }

            public String getCellPostProcess()
            {
                return "</td>\n";
            }

            public String getRowPostProcess()
            {
                return "</tr>\n";
            }

            public boolean doesIncludeBodyContent()
            {
                return true;
            }
        }

         
    • Matthew Wilson

      Matthew Wilson - 2004-11-15

      I will get these changes in the 0.1.7 build, thanks:)

       

Log in to post a comment.