You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ar...@apache.org on 2010/12/28 23:35:27 UTC

svn commit: r1053474 - in /myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main: java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java webapp/components/table_sorting.jspx

Author: arobinson74
Date: Tue Dec 28 22:35:27 2010
New Revision: 1053474

URL: http://svn.apache.org/viewvc?rev=1053474&view=rev
Log:
TRINIDAD-1965 - improve sortable model API to support case insensitive sorting

Added:
    myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java   (with props)
    myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx   (with props)

Added: myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java?rev=1053474&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java (added)
+++ myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java Tue Dec 28 22:35:27 2010
@@ -0,0 +1,177 @@
+package org.apache.myfaces.trinidaddemo.table;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.myfaces.trinidad.model.SortableModel;
+
+
+public class SortableModelBean
+  extends SortableModel
+{
+  public SortableModelBean()
+  {
+    super(_createData());
+
+    setComparator("size", new SizeComparator());
+    setCaseSensitive(true);
+  }
+
+  private static List<FileInfo> _createData()
+  {
+    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
+    try
+    {
+      return Arrays.asList(
+        new FileInfo("Documents", dateFormat.parse("20081123"),
+          dateFormat.parse("20101015"), "folder", ""),
+        new FileInfo("Music", dateFormat.parse("20081123"),
+          dateFormat.parse("20101228"), "folder", ""),
+        new FileInfo(".bashrc", dateFormat.parse("20081124"),
+          dateFormat.parse("20090208"), "shell script", "90 bytes"),
+        new FileInfo("Products.pdf", dateFormat.parse("20100416"),
+          dateFormat.parse("20100416"), "PDF document", "1.2 MB"),
+        new FileInfo("groceries.xls", dateFormat.parse("20101024"),
+          dateFormat.parse("20101228"), "Excel spreadsheet", "38.0 KB"),
+        new FileInfo("demo.mp3", dateFormat.parse("20072312"),
+          dateFormat.parse("20090612"), "MP3 file", "5.4 MB"));
+    }
+    catch (ParseException e)
+    {
+      return Collections.emptyList();
+    }
+  }
+
+  public final void setCaseSensitive(boolean caseSensitive)
+  {
+    _caseSensitive = caseSensitive;
+
+    if (_caseSensitive)
+    {
+      setComparator("filename", null);
+      setComparator("type", null);
+    }
+    else
+    {
+      setCollator("filename", Strength.TERTIARY, null);
+      setCollator("type", Strength.TERTIARY, null);
+    }
+  }
+
+  public final boolean isCaseSensitive()
+  {
+    return _caseSensitive;
+  }
+
+  public static class FileInfo
+  {
+    private FileInfo(
+      String filename,
+      Date   created,
+      Date   modified,
+      String type,
+      String size)
+    {
+      this._filename = filename;
+      this._created = created;
+      this._type = type;
+      this._size = size;
+      this._modified = modified;
+    }
+
+    public final String getFilename()
+    {
+      return _filename;
+    }
+
+    public final String getType()
+    {
+      return _type;
+    }
+
+    public final String getSize()
+    {
+      return _size;
+    }
+
+    public final Date getModified()
+    {
+      return _modified;
+    }
+
+    public final Date getCreated()
+    {
+      return _created;
+    }
+
+    private final String _filename;
+    private final String _type;
+    private final String _size;
+    private final Date _modified;
+    private final Date _created;
+  }
+
+  private static final class SizeComparator
+    implements Comparator<String>
+  {
+    @Override
+    public int compare(
+      String s1,
+      String s2)
+    {
+      if (s1.equals(s2))
+      {
+        return 0;
+      }
+      else if (s1.length() == 0)
+      {
+        return -1;
+      }
+      else if (s2.length() == 0)
+      {
+        return 1;
+      }
+
+      Matcher m1 = _SIZE_PATTERN.matcher(s1);
+      m1.find();
+
+      Matcher m2 = _SIZE_PATTERN.matcher(s2);
+      m2.find();
+
+      String type1 = m1.group(2);
+      String type2 = m2.group(2);
+
+      if (type1.equals(type2))
+      {
+        double d1 = Double.parseDouble(m1.group(1));
+        double d2 = Double.parseDouble(m2.group(1));
+
+        return Double.compare(d1, d2);
+      }
+      else
+      {
+        if ("bytes".equals(type1))
+        {
+          return -1;
+        }
+        else if ("bytes".equals(type2))
+        {
+          return 1;
+        }
+        return type1.compareTo(type2);
+      }
+    }
+
+    private final static Pattern _SIZE_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)?)\\s+(\\w+)");
+  }
+
+  private boolean _caseSensitive;
+}

Propchange: myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/java/org/apache/myfaces/trinidaddemo/table/SortableModelBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx?rev=1053474&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx (added)
+++ myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx Tue Dec 28 22:35:27 2010
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+
+-->
+<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
+          xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
+          xmlns:trh="http://myfaces.apache.org/trinidad/html"
+          xmlns:tr="http://myfaces.apache.org/trinidad">
+  <jsp:directive.page contentType="text/html;charset=utf-8"/>
+  <f:view>
+    <tr:document title="Table Sorting Demo">
+      <tr:form>
+        <tr:panelGroupLayout layout="vertical">
+          <f:facet name="separator">
+            <tr:separator/>
+          </f:facet>
+          <tr:panelGroupLayout layout="horizontal">
+            <tr:commandLink immediate="true" text="Component Guide" action="guide"/>
+            <tr:spacer width="10"/>
+            <tr:goLink destination="http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_table.html"
+                       text="Tag Documentation"/>
+            <tr:spacer width="10"/>
+            <tr:goLink destination="http://myfaces.apache.org/trinidad/skin-selectors.html#table"
+                       text="Skinning Key Documentation"/>
+          </tr:panelGroupLayout>
+          <tr:outputFormatted styleUsage="instruction" value="&lt;b>table sorting&lt;/b>"/>
+          <tr:messages/>
+          <tr:selectBooleanCheckbox value="#{sortableTable.caseSensitive}" autoSubmit="true"
+                                    id="caseSensitive" label="Case sensitive (applies to the filename and type)"/>
+          <tr:table summary="Sortable table" partialTriggers="caseSensitive"
+                    value="#{sortableTable}" var="info" rows="10">
+            <tr:column headerText="Filename" sortable="true" sortProperty="filename">
+              <tr:outputText value="#{info.filename}"/>
+            </tr:column>
+            <tr:column headerText="Type" sortable="true" sortProperty="type">
+              <tr:outputText value="#{info.type}"/>
+            </tr:column>
+            <tr:column headerText="Created" sortable="true" sortProperty="created">
+              <tr:outputText value="#{info.created}"/>
+            </tr:column>
+            <tr:column headerText="Last Modified" sortable="true" sortProperty="modified">
+              <tr:outputText value="#{info.modified}"/>
+            </tr:column>
+            <tr:column headerText="Size" sortable="true" sortProperty="size">
+              <tr:outputText value="#{info.size}"/>
+            </tr:column>
+          </tr:table>
+        </tr:panelGroupLayout>
+      </tr:form>
+    </tr:document>
+  </f:view>
+</jsp:root>

Propchange: myfaces/trinidad/trunk/trinidad-examples/trinidad-demo/src/main/webapp/components/table_sorting.jspx
------------------------------------------------------------------------------
    svn:eol-style = native