myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r638104 - in /myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit: core/xhtml/ uix/
Date Mon, 17 Mar 2008 22:33:01 GMT
Author: gpetracek
Date: Mon Mar 17 15:32:41 2008
New Revision: 638104

URL: http://svn.apache.org/viewvc?rev=638104&view=rev
Log:
TRINIDAD-812

Modified:
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyListboxRenderer.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyRenderer.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneListboxRenderer.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneRenderer.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/uix/SelectItemSupport.java

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyListboxRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyListboxRenderer.java?rev=638104&r1=638103&r2=638104&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyListboxRenderer.java
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyListboxRenderer.java
Mon Mar 17 15:32:41 2008
@@ -28,6 +28,7 @@
 import javax.faces.convert.Converter;
 
 import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
 
 import org.apache.myfaces.trinidad.bean.FacesBean;
 import org.apache.myfaces.trinidad.bean.PropertyKey;
@@ -86,17 +87,45 @@
 
     int selectedCount = selectedIndices.length;
     int selectedEntry = 0;
+    int counter = 0;
     for (int i = 0; i < count; i++)
     {
-      boolean selected = ((selectedEntry < selectedCount) && 
-                          (i == selectedIndices[selectedEntry]));
-      if (selected)
-        selectedEntry++;
+      boolean selected;
 
       SelectItem item = selectItems.get(i);
-      SimpleSelectOneRenderer.encodeOption(
-           context, arc, component, item, converter,
-           valuePassThru, i, selected);
+
+      if(item instanceof SelectItemGroup)
+      {
+        writer.startElement("optgroup", component);
+        writer.writeAttribute("label", item.getLabel(), null);
+        SelectItem[] items = ((SelectItemGroup)item).getSelectItems();
+
+        for(int j = 0; j < items.length; j++)
+        {
+          selected = ((selectedEntry < selectedCount) &&
+                     (counter == selectedIndices[selectedEntry]));
+          if (selected)
+            selectedEntry++;
+
+          SimpleSelectOneRenderer.encodeOption(
+               context, arc, component, items[j], converter,
+               valuePassThru, counter++, selected);
+
+        }
+        writer.endElement("optgroup");
+      }
+      else
+      {
+        selected = ((selectedEntry < selectedCount) &&
+                   (counter == selectedIndices[selectedEntry]));
+
+        if (selected)
+          selectedEntry++;
+
+        SimpleSelectOneRenderer.encodeOption(
+             context, arc, component, item, converter,
+             valuePassThru, counter++, selected);
+      }
     }
     
     writer.endElement("select");
@@ -150,4 +179,5 @@
   
   private PropertyKey _sizeKey;
 }
+
 

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyRenderer.java?rev=638104&r1=638103&r2=638104&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyRenderer.java
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectManyRenderer.java
Mon Mar 17 15:32:41 2008
@@ -35,6 +35,7 @@
 import javax.faces.el.ValueBinding;
 
 import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
 
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 
@@ -234,7 +235,7 @@
     String[]     values,
     Class<?>     modelClass) throws ConverterException
   {
-    List<SelectItem> selectItems = getSelectItems(component, converter);
+    List<SelectItem> selectItems = getSelectItems(component, converter, true);
 
     // No selectItems automatically means that we failed to convert
     if ((selectItems == null) || (selectItems.isEmpty()))
@@ -312,10 +313,18 @@
   }
 
   protected List<SelectItem> getSelectItems(
-    UIComponent component, 
+    UIComponent component,
     Converter converter)
   {
-    return SelectItemSupport.getSelectItems(component, converter);
+    return getSelectItems(component, converter, false);
+  }
+
+  protected List<SelectItem> getSelectItems(
+    UIComponent component,
+    Converter converter,
+    boolean filteredItems)
+  {
+    return SelectItemSupport.getSelectItems(component, converter, filteredItems);
   }
 
   @Override
@@ -496,6 +505,7 @@
     Converter           converter,
     boolean             valuePassThru)
   {
+    List<SelectItem> selectItemList =  flatItemList(selectItems);
     Object submittedValue = getSubmittedValue(bean);
     // In passthru mode, if there's a submitted value, we just
     // have to turn it into an array of ints and range-check it
@@ -507,7 +517,7 @@
       int[] indices = new int[values.length];
       for (int i = 0; i < values.length; i++)
       {
-        indices[i] = SimpleSelectOneRenderer.__getIndex(values[i], selectItems);
+        indices[i] = SimpleSelectOneRenderer.__getIndex(values[i], selectItemList);
       }
 
       // And sort it, but only if it's not reorderable
@@ -581,17 +591,17 @@
       indices[i] = -1;
     }
 
-    int itemCount = selectItems.size();
+    int itemCount = selectItemList.size();
     int foundCount = 0;
 
     for (int i = 0; i < itemCount; i++)
     {
-      SelectItem item = selectItems.get(i);
+      SelectItem item = selectItemList.get(i);
       if (item == null)
         continue;
 
-      Object itemValue = item.getValue();
-      int index = valueList.indexOf(itemValue);
+      int index = calcIndex(item, valueList);
+
       if (index >= 0)
       {
         // Remove it from the valueList so that if the same
@@ -607,9 +617,9 @@
     }
 
     // If all of our values matched up to something in the
-    // selectItems, then the valueList will now be empty.
+    // selectItemList, then the valueList will now be empty.
     // Otherwise, there's some values in the List that didn't
-    // appear anywhere among our selectItems, so clear
+    // appear anywhere among our selectItemList, so clear
     // out the remainder of the indices (which otherwise would
     // be zero) and log a warning
     if (foundCount < valueListSize)
@@ -631,8 +641,66 @@
     return indices;
   }
 
+  private List<SelectItem> flatItemList(List<SelectItem> selectItems)
+  {
+     List<SelectItem> result = new ArrayList<SelectItem>();
+     for(SelectItem item : selectItems)
+     {
+        List<SelectItem> subresult = collectItems( item );
+
+        for(SelectItem subItem : subresult)
+        {
+           result.add( subItem );
+        }
+     }
+
+     return result;
+  }
 
-  static private void _throwConversionError(
+  private List<SelectItem> collectItems(SelectItem item)
+  {
+     List<SelectItem> result = new ArrayList<SelectItem>();
+     if(item instanceof SelectItemGroup)
+     {
+        for(SelectItem subitem : ((SelectItemGroup)item).getSelectItems())
+        {
+           List<SelectItem> subresult = collectItems( subitem );
+
+           for(SelectItem subItem : subresult)
+           {
+              result.add( subItem );
+           }
+        }
+     }
+     else
+     {
+        result.add( item );
+     }
+     return result;
+  }
+
+  private int calcIndex(SelectItem item, List<Object> valueList)
+  {
+     if(item instanceof SelectItemGroup)
+     {
+        int index = -1;
+        for(SelectItem subItem : ((SelectItemGroup)item).getSelectItems())
+        {
+           index = calcIndex( subItem, valueList );
+           if(index >= 0 )
+           {
+              break;
+           }
+        }
+            return index;
+     }
+     else
+     {
+        return valueList.indexOf(item.getValue());
+     }
+  }
+
+    static private void _throwConversionError(
     FacesContext context, UIComponent component)
       throws ConverterException
   {
@@ -661,5 +729,6 @@
   static private final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
     EditableValueRenderer.class);
 }
+
 
 

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneListboxRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneListboxRenderer.java?rev=638104&r1=638103&r2=638104&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneListboxRenderer.java
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneListboxRenderer.java
Mon Mar 17 15:32:41 2008
@@ -28,6 +28,7 @@
 import javax.faces.convert.Converter;
 
 import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
 
 import org.apache.myfaces.trinidad.bean.FacesBean;
 import org.apache.myfaces.trinidad.bean.PropertyKey;
@@ -113,11 +114,32 @@
                    (selectedIndex < 0));
     }
 
+    int counter = 0;
     for (int i = 0; i < count; i++)
     {
-      SelectItem item = selectItems.get(i);
-      encodeOption(context, arc, component, item, converter,
-                   valuePassThru, i, selectedIndex == i);
+       SelectItem item = selectItems.get(i);
+
+       if(item instanceof SelectItemGroup)
+       {
+          writer.startElement("optgroup", component);
+          writer.writeAttribute("label", item.getLabel(), null);
+          SelectItem[] items = ((SelectItemGroup)item).getSelectItems();
+
+          for(int j = 0; j < items.length; j++)
+          {
+             encodeOption(context, arc, component, items[j], converter,
+                          valuePassThru, counter, selectedIndex == counter);
+             counter++;
+
+          }
+          writer.endElement("optgroup");
+       }
+       else
+       {
+          encodeOption(context, arc, component, item, converter,
+                       valuePassThru, counter, selectedIndex == counter);
+          counter++;
+       }
     }
     
     writer.endElement("select");

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneRenderer.java?rev=638104&r1=638103&r2=638104&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneRenderer.java
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/core/xhtml/SimpleSelectOneRenderer.java
Mon Mar 17 15:32:41 2008
@@ -29,6 +29,7 @@
 import javax.faces.convert.ConverterException;
 
 import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
 
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 
@@ -206,7 +207,7 @@
     if ( converter == null)
       converter = getDefaultConverter(context, bean);
 
-    List<SelectItem> selectItems = getSelectItems(component, converter);
+    List<SelectItem> selectItems = getSelectItems(component, converter, true);
     
     int index = __getIndex(submittedValue, selectItems);
     if (index < 0)
@@ -254,7 +255,7 @@
                       getRequiredMessageKey());
     }
 
-    List<SelectItem> selectItems = getSelectItems(component, converter);
+    List<SelectItem> selectItems = getSelectItems(component, converter, false);
     
     int selectedIndex = _getSelectedIndex(context,
                                           component,
@@ -325,7 +326,7 @@
     // =-=AEW If needed, this could be made more efficient
     // by iterating through the list instead of getting
     // all the items
-    List<SelectItem> selectItems = getSelectItems(component, converter);
+    List<SelectItem> selectItems = getSelectItems(component, converter, false);
     
     int selectedIndex = _getSelectedIndex(context,
                                           component,
@@ -354,12 +355,20 @@
   {
     return UIXSelectOne.REQUIRED_MESSAGE_ID;
   }
-  
+
   protected List<SelectItem> getSelectItems(
-    UIComponent component, 
+    UIComponent component,
     Converter converter)
   {
-    return SelectItemSupport.getSelectItems(component, converter);
+    return getSelectItems(component, converter, false);
+  }
+
+  protected List<SelectItem> getSelectItems(
+    UIComponent component,
+    Converter converter,
+    boolean filteredItems)
+  {
+    return SelectItemSupport.getSelectItems(component, converter, filteredItems);
   }
 
   protected boolean getValuePassThru(FacesBean bean)
@@ -393,7 +402,7 @@
     {
       int index = Integer.parseInt(submittedValue.toString());
 
-      if (( -1 < index) && (selectItems.size() > index))
+      if (( -1 < index) && (countSelectItems(selectItems) > index))
       {
         return index;
       }
@@ -412,6 +421,36 @@
     }
   }
 
+  private static int countSelectItems(List<SelectItem> selectItems)
+  {
+    int count = 0;
+    for(SelectItem item : selectItems)
+    {
+      count += calcItems( item );
+    }
+
+    return count;
+  }
+
+  private static int calcItems(SelectItem item)
+  {
+    if(item instanceof SelectItemGroup)
+    {
+      int count = 0;
+      SelectItem[] items;
+      items = ((SelectItemGroup)item).getSelectItems();
+
+      for(int i = 0; i < items.length; i++)
+      {
+        count += calcItems( items[i] );
+      }
+
+      return count;
+    }
+
+    return 1;
+  }
+
   //
   // Find the selected item in the list
   //
@@ -420,23 +459,51 @@
       List<SelectItem> selectItems)
   {
     int size = selectItems.size();
+    int result;
     for (int i = 0; i < size; i++)
     {
       SelectItem item = selectItems.get(i);
       if (item == null)
         continue;
 
+      result = resolveIndex(item, value, i);
+      if(result >= 0)
+      {
+        return result;
+      }
+    }
+
+    return -1;
+  }
+
+  private int resolveIndex(SelectItem item, Object value, int index)
+  {
+    if(item instanceof SelectItemGroup)
+    {
+      int result;
+      for(SelectItem subItem : ((SelectItemGroup)item).getSelectItems())
+      {
+        result = resolveIndex( subItem, value, index++ );
+
+        if(result >= 0)
+        {
+          return result;
+        }
+      }
+    }
+    else
+    {
       if (value == null)
       {
         Object itemValue = item.getValue();
         // =-=AEW Treat the empty string as if it were null
         if ((itemValue == null) || "".equals(itemValue))
-          return i;
+          return index;
       }
       else
       {
         if (value.equals(item.getValue()) || (value.getClass().isEnum() && item.getValue()
!= null && value.toString().equals( item.getValue().toString() )))
-          return i;
+          return index;
       }
     }
 

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/uix/SelectItemSupport.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/uix/SelectItemSupport.java?rev=638104&r1=638103&r2=638104&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/uix/SelectItemSupport.java
(original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/renderkit/uix/SelectItemSupport.java
Mon Mar 17 15:32:41 2008
@@ -33,6 +33,8 @@
 import javax.faces.convert.Converter;
 import javax.faces.el.ValueBinding;
 import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
+
 import org.apache.myfaces.trinidad.component.UIXSelectItem;
 import org.apache.myfaces.trinidadinternal.convert.ConverterUtils;
 
@@ -42,7 +44,7 @@
   {
   }
 
-   /**
+  /**
    * 
    * @param component  UIComponent
    * @param converter For UISelectItem and UIXSelectItem children of the 
@@ -57,8 +59,30 @@
    */
   @SuppressWarnings("unchecked")
   static public List<SelectItem> getSelectItems(
+           UIComponent  component,
+           Converter    converter)
+  {
+    return getSelectItems( component, converter, false );
+  }
+
+  /**
+   *
+   * @param component  UIComponent
+   * @param converter For UISelectItem and UIXSelectItem children of the
+   *                  component, use the converter to convert itemValue Strings
+   *                  when creating the javax.faces.model.SelectItem Object if
+   *                  the child's value is not an instanceof SelectItem.
+   * @param filteredItems to exclude SelectItemGroup components
+   * @return a List of javax.faces.model.SelectItem Objects that we get or
+   *         create from the component's children.
+   *         OR
+   *         java.util.Collections.emptyList if component has no children or
+   *         the component isn't a javax.faces.component.ValueHolder. else
+   */
+  static public List<SelectItem> getSelectItems(
     UIComponent  component,
-    Converter    converter)
+    Converter    converter,
+    boolean filteredItems)
   { 
     
     int childCount = component.getChildCount();
@@ -90,7 +114,7 @@
       {
         if (items == null)
           items = new ArrayList<SelectItem>(childCount);
-        addSelectItems((UISelectItems) child, items);
+        addSelectItems((UISelectItems) child, items, filteredItems );
       }
       // tr:selectItem
       else if (child instanceof UIXSelectItem)
@@ -258,11 +282,13 @@
   /**
    * Adds SelectItem objects derived from
    * a UISelectItems component into the items List.
+   * @param filteredItems to exclude SelectItemGroup components
    */
   @SuppressWarnings("unchecked")
   static public void addSelectItems(
     UISelectItems uiItems,
-    List<SelectItem> items)
+    List<SelectItem> items,
+    boolean filteredItems)
   {
     if (!uiItems.isRendered())
     {
@@ -280,15 +306,32 @@
       Object[] array = (Object[]) value;
       for (int i = 0; i < array.length; i++)
       {
-        items.add((SelectItem) array[i]);
+         //TODO test - this section is untested
+         if(array[i] instanceof SelectItemGroup && filteredItems)
+         {
+            resolveAndAddItems((SelectItemGroup) array[i], items);
+         }
+         else
+         {
+            items.add((SelectItem) array[i]);
+         }
       }
     }
     else if (value instanceof Collection)
     {
       Iterator<SelectItem> iter = ((Collection<SelectItem>) value).iterator();
+      SelectItem item;
       while (iter.hasNext())
       {
-        items.add(iter.next());
+         item = iter.next();
+         if(item instanceof SelectItemGroup && filteredItems)
+         {
+            resolveAndAddItems((SelectItemGroup) item, items);
+         }
+         else
+         {
+            items.add(item);
+         }
       }
     }
     else if (value instanceof Map)
@@ -300,11 +343,34 @@
           new SelectItem(entry.getValue(),
                          label == null ? (String) null : label.toString());
 
-        items.add(item);
+        //TODO test - this section is untested
+        if(item instanceof SelectItemGroup && filteredItems)
+        {
+           resolveAndAddItems((SelectItemGroup) item, items);
+        }
+        else
+        {
+           items.add(item);
+        }
       }
     }
   }
- 
+
+  private static void resolveAndAddItems(SelectItemGroup group, List<SelectItem> items)
+  {
+     for(SelectItem item: group.getSelectItems())
+     {
+        if(item instanceof SelectItemGroup)
+        {
+           resolveAndAddItems( (SelectItemGroup) item, items );
+        }
+        else
+        {
+           items.add( item );
+        }
+     }
+  }
+
   /**
    * Adds a SelectItem object derived from
    * a UIXSelectItem component into the items List.



Mime
View raw message