You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2006/10/29 22:55:33 UTC

svn commit: r469011 - in /myfaces/tobago/trunk: core/src/main/java/org/apache/myfaces/tobago/renderkit/ core/src/main/java/org/apache/myfaces/tobago/renderkit/html/ theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/st...

Author: bommel
Date: Sun Oct 29 13:55:32 2006
New Revision: 469011

URL: http://svn.apache.org/viewvc?view=rev&rev=469011
Log:
[TOBAGO-165] SelectOneChoiceRenderer, SelectOneListboxRenderer and SelectManyListboxRenderer should support SelectItemGroup

Modified:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/RenderUtil.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlAttributes.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlRendererUtil.java
    myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectManyListboxRenderer.java
    myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneChoiceRenderer.java
    myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneListboxRenderer.java

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/RenderUtil.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/RenderUtil.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/RenderUtil.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/RenderUtil.java Sun Oct 29 13:55:32 2006
@@ -22,10 +22,11 @@
 import org.apache.myfaces.tobago.component.UILayout;
 
 import javax.faces.component.UIComponent;
+import javax.faces.component.ValueHolder;
 import javax.faces.context.FacesContext;
-import javax.servlet.http.HttpServletRequest;
+import javax.faces.convert.ConverterException;
+import javax.faces.convert.Converter;
 import java.io.IOException;
-import java.util.Iterator;
 
 public class RenderUtil {
 
@@ -37,21 +38,14 @@
     if (list == null) {
       return false;
     }
-    for (int i = 0; i < list.length; i++) {
-      if (list[i] != null && list[i].equals(value)) {
+    for (Object aList : list) {
+      if (aList != null && aList.equals(value)) {
         return true;
       }
     }
     return false;
   }
 
-  public static UIComponent getComponent(HttpServletRequest request) {
-
-    UIComponent component
-        = (UIComponent) request.getAttribute(COMPONENT_IN_REQUEST);
-    return component;
-  }
-
   public static void encodeChildren(FacesContext facesContext,
       UIComponent panel)
       throws IOException {
@@ -60,8 +54,8 @@
     if (layout != null) {
       layout.encodeChildrenOfComponent(facesContext, panel);
     } else {
-      for (Iterator i = panel.getChildren().iterator(); i.hasNext();) {
-        UIComponent child = (UIComponent) i.next();
+      for (Object o : panel.getChildren()) {
+        UIComponent child = (UIComponent) o;
         encode(facesContext, child);
       }
     }
@@ -81,9 +75,8 @@
       if (component.getRendersChildren()) {
         component.encodeChildren(facesContext);
       } else {
-        Iterator kids = component.getChildren().iterator();
-        while (kids.hasNext()) {
-          UIComponent kid = (UIComponent) kids.next();
+        for (Object o : component.getChildren()) {
+          UIComponent kid = (UIComponent) o;
           encode(facesContext, kid);
         }
       }
@@ -105,4 +98,44 @@
     return onClick;
   }
 
+  public static String getFormattedValue(
+      FacesContext facesContext, UIComponent component){
+    Object value = null;
+    if (component instanceof ValueHolder) {
+      value = ((ValueHolder) component).getLocalValue();
+      if (value == null) {
+        value =  ((ValueHolder) component).getValue();
+      }
+    }
+    return getFormattedValue(facesContext, component, value);
+  }
+
+  public static String getFormattedValue(
+      FacesContext context, UIComponent component, Object currentValue)
+      throws ConverterException {
+
+    if (currentValue == null) {
+      return "";
+    }
+
+    if (!(component instanceof ValueHolder)) {
+      return currentValue.toString();
+    }
+
+    Converter converter = ((ValueHolder) component).getConverter();
+
+    if (converter == null) {
+      if (currentValue instanceof String) {
+        return (String) currentValue;
+      }
+      Class converterType = currentValue.getClass();
+      converter = context.getApplication().createConverter(converterType);
+    }
+
+    if (converter == null) {
+      return currentValue.toString();
+    } else {
+      return converter.getAsString(context, component, currentValue);
+    }
+  }
 }

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlAttributes.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlAttributes.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlAttributes.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlAttributes.java Sun Oct 29 13:55:32 2006
@@ -71,5 +71,6 @@
   public static final String ONKEYUP = "onkeyup";
   public static final String CLASS = "class";
   public static final String MAXLENGTH = "maxlength";
+  public static final String LABEL = "label";
 }
 

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlConstants.java Sun Oct 29 13:55:32 2006
@@ -50,6 +50,7 @@
   public static final String FORM = "form";
   public static final String SCRIPT = "script";
   public static final String META = "meta";
+  public static final String OPTGROUP = "optgroup";
 
   private HtmlConstants() {
 

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlRendererUtil.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlRendererUtil.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlRendererUtil.java (original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/renderkit/html/HtmlRendererUtil.java Sun Oct 29 13:55:32 2006
@@ -48,17 +48,26 @@
 import org.apache.myfaces.tobago.context.Theme;
 import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
 import org.apache.myfaces.tobago.renderkit.RendererBase;
+import org.apache.myfaces.tobago.renderkit.RenderUtil;
+import org.apache.myfaces.tobago.renderkit.HtmlUtils;
 import org.apache.myfaces.tobago.util.LayoutUtil;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
+import org.apache.myfaces.tobago.TobagoConstants;
 
 import javax.faces.component.UIComponent;
+import javax.faces.component.UIInput;
+import javax.faces.component.ValueHolder;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
+import javax.faces.model.SelectItem;
+import javax.faces.model.SelectItemGroup;
 import java.io.IOException;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.List;
+import java.util.Arrays;
 
-/**
+/*
  * User: weber
  * Date: Jan 11, 2005
  * Time: 4:59:36 PM
@@ -555,6 +564,79 @@
     return title;
   }
 
+  public static void renderSelectItems(UIInput component, List<SelectItem> items, Object[] values,
+      TobagoResponseWriter writer, FacesContext facesContext) throws IOException {
+
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("value = '" + values + "'");
+    }
+    for (SelectItem item : items) {
+      if (item instanceof SelectItemGroup) {
+        writer.startElement(HtmlConstants.OPTGROUP, null);
+        writer.writeAttribute(HtmlAttributes.LABEL, item.getLabel(), null);
+        SelectItem[] selectItems = ((SelectItemGroup) item).getSelectItems();
+        renderSelectItems(component, Arrays.asList(selectItems), values, writer, facesContext);
+        writer.endElement(HtmlConstants.OPTGROUP);
+      } else {
+        writer.startElement(HtmlConstants.OPTION, null);
+        final Object itemValue = item.getValue();
+        String formattedValue
+            = RenderUtil.getFormattedValue(facesContext, component, itemValue);
+        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, null);
+        if (RenderUtil.contains(values, item.getValue())) {
+          writer.writeAttribute(HtmlAttributes.SELECTED, HtmlAttributes.SELECTED, null);
+        }
+        writer.writeText(item.getLabel(), null);
+        writer.endElement(HtmlConstants.OPTION);
+      }
+    }
+  }
+
+  public static String createOnClick(FacesContext facesContext,
+      UIComponent component) {
+    //String type = (String) component.getAttributes().get(ATTR_TYPE);
+    //String command = (String) component.getAttributes().get(ATTR_ACTION_STRING);
+    String clientId = component.getClientId(facesContext);
+    boolean defaultCommand = ComponentUtil.getBooleanAttribute(component,
+        TobagoConstants.ATTR_DEFAULT_COMMAND);
+    String onclick;
+
+    if (component.getAttributes().get(TobagoConstants.ATTR_ACTION_LINK)!=null) {
+      onclick = "Tobago.navigateToUrl('"
+          + HtmlUtils.generateUrl(facesContext, (String) component.getAttributes().get(TobagoConstants.ATTR_ACTION_LINK)) + "');";
+      // FIXME !!
+      //} else if (COMMAND_TYPE_RESET.equals(type)) {
+    //  onclick = null;
+    } else if (component.getAttributes().get(TobagoConstants.ATTR_ACTION_ONCLICK)!=null) {
+      onclick = (String) component.getAttributes().get(TobagoConstants.ATTR_ACTION_ONCLICK);
+    } else if (defaultCommand) {
+      ComponentUtil.findPage(component).setDefaultActionId(clientId);
+//      onclick = "Tobago.setAction('" + clientId + "');";
+      onclick = null;
+    } else {
+      onclick = "Tobago.submitAction('" + clientId + "');";
+    }
+    return onclick;
+  }
+
+  public static String appendConfirmationScript(String onclick,
+      UIComponent component, FacesContext facesContext) {
+    ValueHolder confirmation
+        = (ValueHolder) component.getFacet(TobagoConstants.FACET_CONFIRMATION);
+    if (confirmation != null) {
+      if (onclick != null) {
+        onclick = "confirm('" + confirmation.getValue() + "') && " + onclick;
+      } else {
+        if (LOG.isWarnEnabled()) {
+          LOG.warn("Facet '" + TobagoConstants.FACET_CONFIRMATION + "' is not supported for "
+              + "this type of button. id = '"
+              + component.getClientId(facesContext) + "'");
+        }
+      }
+    }
+    return onclick;
+  }
+
   public static void main(String[] args) {
     System.out.println(removeTobagoClasses("bla bla bla tobago-test-inline bla bla", "test"));
     System.out.println(removeTobagoClasses("tobago-test-inline blablubber bla", "test"));
@@ -566,5 +648,4 @@
     System.out.println(removeTobagoClasses("", "test"));
     System.out.println(removeTobagoClasses("hallo", "test"));
   }
-
 }

Modified: myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectManyListboxRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectManyListboxRenderer.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectManyListboxRenderer.java (original)
+++ myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectManyListboxRenderer.java Sun Oct 29 13:55:32 2006
@@ -28,10 +28,10 @@
 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_STYLE;
 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TIP;
 import org.apache.myfaces.tobago.component.ComponentUtil;
-import org.apache.myfaces.tobago.renderkit.RenderUtil;
 import org.apache.myfaces.tobago.renderkit.SelectManyRendererBase;
 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
+import org.apache.myfaces.tobago.renderkit.html.HtmlRendererUtil;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
 
 import javax.faces.component.UIComponent;
@@ -96,33 +96,18 @@
         ComponentUtil.getBooleanAttribute(component, ATTR_DISABLED));
     writer.writeAttribute(HtmlAttributes.STYLE, null, ATTR_STYLE);
     writer.writeComponentClass();
-    writer.writeAttribute(HtmlAttributes.MULTIPLE, "multiple", null);
+    writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, null);
     writer.writeAttribute(HtmlAttributes.TITLE, null, ATTR_TIP);
 
     Object[] values = component.getSelectedValues();
     if (LOG.isDebugEnabled()) {
       LOG.debug("values = '" + values + "'");
     }
-    for (SelectItem item : items) {
-
-      writer.startElement(HtmlConstants.OPTION, null);
-      String formattedValue
-          = getFormattedValue(facesContext, component, item.getValue());
-      writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, null);
-      if (RenderUtil.contains(values, item.getValue())) {
-        writer.writeAttribute(HtmlAttributes.SELECTED, "selected", null);
-      }
-      writer.writeText(item.getLabel(), null);
-      writer.endElement(HtmlConstants.OPTION);
-//    LOG.debug("item-value" + item.getValue());
-    }
-
+    HtmlRendererUtil.renderSelectItems(component, items, values, writer, facesContext);
 
     writer.endElement(HtmlConstants.SELECT);
     checkForCommandFacet(component, facesContext, writer);
   }
-
-// ///////////////////////////////////////////// bean getter + setter
 
 }
 

Modified: myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneChoiceRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneChoiceRenderer.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneChoiceRenderer.java (original)
+++ myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneChoiceRenderer.java Sun Oct 29 13:55:32 2006
@@ -32,6 +32,7 @@
 import org.apache.myfaces.tobago.renderkit.SelectOneRendererBase;
 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
+import org.apache.myfaces.tobago.renderkit.html.HtmlRendererUtil;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
 
 import javax.faces.component.UIComponent;
@@ -73,20 +74,10 @@
     if (onchange != null) {
       writer.writeAttribute(HtmlAttributes.ONCHANGE, onchange, null);
     }
+    Object[] values = { component.getValue() };
+
+    HtmlRendererUtil.renderSelectItems(component, items, values, writer, facesContext);
 
-    Object value = component.getValue();
-    for (SelectItem item : items) {
-      final Object itemValue = item.getValue();
-      writer.startElement(HtmlConstants.OPTION, null);
-      String formattedValue
-          = getFormattedValue(facesContext, component, itemValue);
-      writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, null);
-      if (itemValue.equals(value)) {
-        writer.writeAttribute(HtmlAttributes.SELECTED, "selected", null);
-      }
-      writer.writeText(item.getLabel(), null);
-      writer.endElement(HtmlConstants.OPTION);
-    }
     writer.endElement(HtmlConstants.SELECT);
     super.encodeEndTobago(facesContext, component);
     checkForCommandFacet(component, facesContext, writer);

Modified: myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneListboxRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneListboxRenderer.java?view=diff&rev=469011&r1=469010&r2=469011
==============================================================================
--- myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneListboxRenderer.java (original)
+++ myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/SelectOneListboxRenderer.java Sun Oct 29 13:55:32 2006
@@ -27,10 +27,12 @@
 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_HEIGHT;
 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_REQUIRED;
 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TIP;
+import static org.apache.myfaces.tobago.TobagoConstants.ATTR_STYLE;
 import org.apache.myfaces.tobago.component.ComponentUtil;
 import org.apache.myfaces.tobago.renderkit.SelectOneRendererBase;
 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
+import org.apache.myfaces.tobago.renderkit.html.HtmlRendererUtil;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
 
 import javax.faces.component.UIComponent;
@@ -44,7 +46,6 @@
 
   private static final Log LOG = LogFactory.getLog(SelectOneListboxRenderer.class);
 
-
   public boolean getRendersChildren() {
     return true;
   }
@@ -52,6 +53,7 @@
   public int getComponentExtraWidth(FacesContext facesContext, UIComponent component) {
     return 0;
   }
+
   public int getFixedHeight(FacesContext facesContext, UIComponent component) {
     int fixedHeight = -1;
     String height = (String) component.getAttributes().get(ATTR_HEIGHT);
@@ -81,7 +83,7 @@
     writer.writeIdAttribute(clientId);
     writer.writeAttribute(HtmlAttributes.DISABLED,
         ComponentUtil.getBooleanAttribute(component, ATTR_DISABLED));
-    writer.writeAttribute(HtmlAttributes.STYLE, null, "style");
+    writer.writeAttribute(HtmlAttributes.STYLE, null, ATTR_STYLE);
     writer.writeComponentClass();
     writer.writeAttribute(HtmlAttributes.TITLE, null, ATTR_TIP);
     writer.writeAttribute(HtmlAttributes.SIZE, 2, null); // should be greater 1
@@ -90,25 +92,9 @@
       writer.writeAttribute(HtmlAttributes.ONCLICK, "Tobago.selectOneListboxClick(this)", null);
     }
 
-    Object value = component.getValue();
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("value = '" + value + "'");
-    }
-    for (SelectItem item : items) {
-
-      writer.startElement(HtmlConstants.OPTION, null);
-      final Object itemValue = item.getValue();
-      String formattedValue
-          = getFormattedValue(facesContext, component, itemValue);
-      writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, null);
-      if (itemValue.equals(value)) {
-        writer.writeAttribute(HtmlAttributes.SELECTED, "selected", null);
-      }
-      writer.writeText(item.getLabel(), null);
-      writer.endElement(HtmlConstants.OPTION);
-//    LOG.debug("item-value" + itemValue);
-    }
+    Object[] values = { component.getValue() };
 
+    HtmlRendererUtil.renderSelectItems(component, items, values, writer, facesContext);
 
     writer.endElement(HtmlConstants.SELECT);
     super.encodeEndTobago(facesContext, component);