[jboss-cvs] jboss-seam/ui/src/main/java/org/jboss/seam/ui/component ...

Peter Muir peter at bleepbleep.org.uk
Fri Jan 4 13:23:19 EST 2008


  User: pmuir   
  Date: 08/01/04 13:23:19

  Modified:    ui/src/main/java/org/jboss/seam/ui/component 
                        UISelectItems.java
  Log:
  Refactor
  
  Revision  Changes    Path
  1.10      +103 -67   jboss-seam/ui/src/main/java/org/jboss/seam/ui/component/UISelectItems.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UISelectItems.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/ui/src/main/java/org/jboss/seam/ui/component/UISelectItems.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -b -r1.9 -r1.10
  --- UISelectItems.java	16 Nov 2007 18:49:02 -0000	1.9
  +++ UISelectItems.java	4 Jan 2008 18:23:19 -0000	1.10
  @@ -1,24 +1,3 @@
  -/**
  - * License Agreement.
  - *
  - * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
  - *
  - * Copyright (C) 2007 Exadel, Inc.
  - *
  - * This library is free software; you can redistribute it and/or
  - * modify it under the terms of the GNU Lesser General Public
  - * License version 2.1 as published by the Free Software Foundation.
  - *
  - * This library is distributed in the hope that it will be useful,
  - * but WITHOUT ANY WARRANTY; without 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  - */
  -
   package org.jboss.seam.ui.component;
   
   import static org.jboss.seam.util.Strings.emptyIfNull;
  @@ -41,7 +20,7 @@
   
   
   /**
  - * JSF component class
  + * @auth Pete Muir
    *
    */
   public abstract class UISelectItems extends javax.faces.component.UISelectItems {
  @@ -68,6 +47,55 @@
   
      }
   
  +   private abstract class ContextualSelectItem {
  +      
  +      private Object varValue;
  +      
  +      public ContextualSelectItem(Object varValue)
  +      {
  +         if (varValue == null)
  +         {
  +            throw new FacesException("var attribute must be set");
  +         }
  +         this.varValue = varValue;
  +      }
  +      
  +      /**
  +       * @return the varValue
  +       */
  +      protected Object getVarValue()
  +      {
  +         return this.varValue;
  +      }
  +      
  +      private void setup()
  +      {
  +         getFacesContext().getExternalContext().getRequestMap().put(getVar(), varValue);
  +      }
  +      
  +      private void cleanup()
  +      {
  +         getFacesContext().getExternalContext().getRequestMap().remove(getVar());
  +      }
  +      
  +      protected abstract Object getSelectItemValue();
  +      protected abstract String getSelectItemLabel();
  +      protected abstract Boolean getSelectItemDisabled();
  +      
  +      protected javax.faces.model.SelectItem create()
  +      {
  +         try
  +         {
  +            setup();
  +            return new javax.faces.model.SelectItem(this.getSelectItemValue(), this.getSelectItemLabel(), "", this.getSelectItemDisabled());
  +         }
  +         finally
  +         {
  +            cleanup();
  +         }
  +      }
  +   }
  +
      private static final String NO_SELECTION_VALUE = null;
   
      /* Kinder impl of get/setLabel */
  @@ -169,29 +197,68 @@
      
      private List<javax.faces.model.SelectItem> asSelectItems(Iterable iterable) 
      {
  -      List<javax.faces.model.SelectItem> selectItems =  new ArrayList<javax.faces.model.SelectItem>();
  +      final List<javax.faces.model.SelectItem> selectItems =  new ArrayList<javax.faces.model.SelectItem>();
         javax.faces.model.SelectItem noSelectionLabel = noSelectionLabel();
         if (noSelectionLabel != null) 
         {
            selectItems.add(noSelectionLabel);
         }
  -      for (Object o : iterable)
  +      for (final Object o : iterable)
  +      {
  +         selectItems.add(new ContextualSelectItem(o)
  +         {
  +
  +            @Override
  +            protected Boolean getSelectItemDisabled()
         {
  -         initVar(o);
  -         String itemLabel = emptyIfNull(getLabel());
  -         Object value = getItemValue();
  -         Object itemValue = value == null ? o : value;
            Boolean disabled = getDisabled();
  -         boolean itemDisabled = disabled == null ? false : disabled;
  -         selectItems.add( new javax.faces.model.SelectItem(itemValue, itemLabel, "", itemDisabled) );
  -         destroyVar();
  +               return disabled == null ? false : disabled;
  +            }
  +
  +            @Override
  +            protected String getSelectItemLabel()
  +            {
  +               return emptyIfNull(getLabel());
  +            }
  +
  +            @Override
  +            protected Object getSelectItemValue()
  +            {
  +               Object value = getItemValue();
  +               return value == null ? getVarValue() : value;
  +            }
  +            
  +         }.create());
         }
         return selectItems;
      }
   
      private javax.faces.model.SelectItem noSelectionLabel()
      {
  -      boolean show = false;
  +      if (isShowNoSelectionLabel())
  +      {
  +         NullableSelectItem s = new NullableSelectItem(NO_SELECTION_VALUE, getNoSelectionLabel());
  +         ConverterChain converterChain = new ConverterChain(this.getParent());
  +         Converter noSelectionConverter = new NoSelectionConverter();
  +         // Make sure that the converter is only added once
  +         if (!converterChain.containsConverterType(noSelectionConverter)) {
  +            converterChain.addConverterToChain(noSelectionConverter, ConverterChain.CHAIN_START);
  +         }
  +         return s;
  +      }
  +      else
  +      {
  +         return null;
  +      }
  +   }
  +   
  +   private boolean isShowNoSelectionLabel()
  +   {  
  +      ValueExpression vb = getValueExpression("noSelectionLabel");
  +      String noSelectionLabel = getNoSelectionLabel();
  +      Boolean hideNoSelectionLabel = getHideNoSelectionLabel();
  +      Object parentValue = getParentValue();
  +      
         /*
          * This is a slight hack. If you do an EL expresison like this (to hide the label)
          * 
  @@ -200,17 +267,13 @@
          * then, if x != y, EL will return an empty String, not null, so we work around that, with the side effect
          * that if the result of the EL expression is an empty String, then the label will be hidden.
          */
  -      ValueExpression vb = getValueExpression("noSelectionLabel");
  -      String noSelectionLabel = getNoSelectionLabel();
  -      Object parentValue = getParentValue();
  -      Boolean hideNoSelectionLabel = getHideNoSelectionLabel();
         if (noSelectionLabel != null && vb == null && !(hideNoSelectionLabel  && parentValue != null))
         {
            /* 
             * Here, the user has specfied a noSelectionLabel (may be an empty string), and, if hideNoSelectionLabel
             * is set, then, if a value is selected, then the label is hidden
             */ 
  -         show = true;
  +         return true;
         } 
         else if (noSelectionLabel != null && !"".equals(noSelectionLabel) && !(hideNoSelectionLabel && parentValue != null))
         {
  @@ -218,39 +281,12 @@
             * Here, the user has used an EL expression as the noSelectionLabel.  In this case, an empty string is
             * indicates that the label should be hidden.
             */
  -         show = true;
  -      }
  -      
  -      if (show)
  -      {
  -         NullableSelectItem s = new NullableSelectItem(NO_SELECTION_VALUE, noSelectionLabel);
  -         ConverterChain converterChain = new ConverterChain(this.getParent());
  -         Converter noSelectionConverter = new NoSelectionConverter();
  -         // Make sure that the converter is only added once
  -         if (!converterChain.containsConverterType(noSelectionConverter)) {
  -            converterChain.addConverterToChain(noSelectionConverter, ConverterChain.CHAIN_START);
  -         }
  -         return s;
  +         return true;
         }
         else
         {
  -         return null;
  -      }
  -   }
  -
  -   @SuppressWarnings("unchecked")
  -   private void initVar(Object varValue)
  -   {
  -      if (getVar() == null)
  -      {
  -         throw new FacesException("var attribute must be set");
  -      }
  -      getFacesContext().getExternalContext().getRequestMap().put(getVar(), varValue);
  +         return false;
      }
  -
  -   private void destroyVar()
  -   {
  -      getFacesContext().getExternalContext().getRequestMap().remove(getVar());
      }
   
      private Object getParentValue()
  
  
  



More information about the jboss-cvs-commits mailing list