1 2 Previous Next 28 Replies Latest reply on Oct 27, 2010 3:48 AM by nigiyan Go to original post
      • 15. Re: Disabling a group of input fields.
        cash1981

        I didnt say you couldnt do it with your own component, but there is no seam, facelets or jsf component that supports what you want.

        • 16. Re: Disabling a group of input fields.
          kragoth

          P. S.: Wondering how the rendered attribute at a s:fragement could be impact the rendering behaviour of all inner components? :-)


          Technically speaking the s:fragment does not have an impact on the child component's rendered attribute. The reason why the child components do not render is because renderChildren on the s:fragment is not called if rendered is false. Thus all child components of the s:fragment do not get rendered. Even if their rendered attribute is set to true.


          What you are trying to do is modify the state of child components. Before they get rendered. Like I said, I'm not 100% sure that my idea will work. If it doesn't I have another idea on how it could be made to work...maybe.


          Let me know how it goes. :)

          • 17. Re: Disabling a group of input fields.

            So I build a component, that works so far for my requirements. I am sure it contains also some bugs.


            It based on Tims suggesting (which was 99% correct :-)


            public abstract class UIDisablePanel extends UIComponentBase {
            
                public static final String COMPONENT_TYPE = "com.myCompany.taglib.DisablePanel";
                public static final String COMPONENT_FAMILY = "com.myCompany.taglib.DisablePanel";
            
                private boolean disabled = false;
            
                @Override
                public void encodeBegin(FacesContext context) throws IOException {
                    for (UIComponent component : this.getChildren()) {
                        Object b = component.getAttributes().get("disabled");
                        if(b != null) {
                           component.getAttributes().put("disabled", Boolean.valueOf(disabled));
                        }
                    }
                    super.encodeBegin(context);
            
                }
            
                public String isDisabled() {
                    return Boolean.toString(disabled);
                }
            
                public void setDisabled(String disabled) {
                    this.disabled = Boolean.parseBoolean(disabled);
                }
            }
            



            Hoping someone can use this, too.


            Regards
            Marco


            • 18. Re: Disabling a group of input fields.
              kragoth

              Hey Marco,


              I'm glad this worked out for you! :D
              I may find use for this component in my app at some stage, so nice to know that it works. :)


              Cheers,
              Tim

              • 19. Re: Disabling a group of input fields.
                cash1981

                I have always used jquery to disable stuff like that. Never thought about making a component for it, but its sounds better.


                Can you explain how to use this component? I have never created custom component before. what do I need to do to make this work on my app?

                • 20. Re: Disabling a group of input fields.
                  kragoth

                  I would really recommend reading THIS tutorial. It explains how to create custom components and how to use them.


                  If you can't get it working after reading that then I'll try step you through it. We don't use the standard components much in our application. We've extended (made custom implementations of)  most of the standard jsf components, and we've extended some of the richfaces components too (that is a bit trickier so, start with a standard jsf component :)


                  After a bit of reading about custom components I found it was fairly easy to get whatever behaviour I desired. So, have a read of that link and see how you go. :)


                  The big reason why jQuery wouldn't be suitable here is that if the user has javascript disabled they could by pass your restrictions. (Obviously if this is an internal app then that isn't such a big deal, but, it is still something to consider.

                  • 21. Re: Disabling a group of input fields.
                    swd847

                    In a similar vein I use a facelets tag decorator to disable and change the appearance of links if they point somewhere the user does not have permission to view:



                    public class SecurityTagDecorator implements TagDecorator
                    {
                    
                        public Tag decorate(Tag arg0)
                        {
                         if (!arg0.getNamespace().equals("http://jboss.com/products/seam/taglib"))
                         {
                             return null;
                         }
                         if (arg0.getLocalName().equals("link") || arg0.getLocalName().equals("button"))
                         {
                             TagAttribute view = arg0.getAttributes().get("view");
                             if (view == null)
                             {
                              return null;
                             }
                             String viewId = view.getValue();
                             //don't try and fix el based permission
                             if (viewId.contains("#{"))
                             {
                              return null;
                             }
                             String disabled = "#{not pageSecurityManager.isViewable('" + viewId + "')}";
                             String styleClass = "#{pageSecurityManager.isViewable('" + viewId
                                  + "') ? '' : ' permissionDenied'}";
                    
                             TagAttribute[] at = arg0.getAttributes().getAll();
                             boolean styleFound = false;
                             boolean disFound = false;
                             for (int j = 0; j < at.length; ++j)
                             {
                              TagAttribute i = at[j];
                              if (i.getLocalName().equals("styleClass"))
                              {
                                  styleFound = true;
                                  String val = i.getValue();
                                  TagAttribute ti = new TagAttribute(i.getLocation(), i.getNamespace(), i
                                       .getLocalName(), i.getQName(), val + styleClass);
                                  at[j] = ti;
                              }
                              if (i.getLocalName().equals("disabled"))
                              {
                                  disFound = true;
                                  TagAttribute ti = new TagAttribute(i.getLocation(), i.getNamespace(), i
                                       .getLocalName(), i.getQName(), disabled);
                                  at[j] = ti;
                              }
                             }
                             if (!styleFound)
                             {
                              TagAttribute ti = new TagAttribute(arg0.getLocation(), "", "styleClass",
                                   "styleClass", styleClass);
                              TagAttribute[] old = at;
                              at = new TagAttribute[old.length + 1];
                              for (int k = 0; k < old.length; ++k)
                              {
                                  at[k] = old[k];
                              }
                              at[old.length] = ti;
                             }
                             if (!disFound)
                             {
                              TagAttribute ti = new TagAttribute(arg0.getLocation(), "", "disabled", "disabled",
                                   disabled);
                              TagAttribute[] old = at;
                              at = new TagAttribute[old.length + 1];
                              for (int k = 0; k < old.length; ++k)
                              {
                                  at[k] = old[k];
                              }
                              at[old.length] = ti;
                             }
                             TagAttributes ats = new TagAttributes(at);
                             return new Tag(arg0, ats);
                    
                         }
                         return null;
                    
                        }
                    }
                    
                    
                    
                    

                    • 22. Re: Disabling a group of input fields.
                      kragoth

                      I havn't used tag decorates... I can see that they could be quite handy. :)


                      Don't suppose you have a link to a good tutorial on this? (I'll go google anyways, but sometimes finding a good tutorial can be a little painful :P)


                      Guess, I'm looking for



                      1. How to hook in a decorator

                      2. Where in the lifecycle they get executed

                      3. What limitations are there in what you can do

                      • 23. Re: Disabling a group of input fields.
                        swd847

                        They are hooked in in web.xml:


                        <context-param>
                          <param-name>facelets.DECORATORS</param-name>
                          <param-value>
                          com.mydomain.jsf.SecurityTagDecorator;com.mydomain.jsf.DataTableTagDecorator;com.mydomain.jsf.RequiredTagDecorator;
                        </param-value>
                        



                        They are evaluated for each tag when the facelet is compiling


                        In terms of limitations they cannot add a sub tag, they can only change existing tags. Here are two other examples, the first gives all required fields a specific styleClass, the second adds a rowClasses attribute to richfaces datatables to automatically have alternating colored rows (unless there is an existing rowClasses attribute):


                        public Tag decorate(Tag arg0)
                            {
                             TagAttribute required = arg0.getAttributes().get("required");
                             if (required == null)
                             {
                                 return null;
                             }
                             if (!required.getValue().equals("true"))
                             {
                                 return null;
                             }
                        
                             String styleAt = "styleClass";
                             if (arg0.getLocalName().equals("calendar"))
                             {
                                 styleAt = "inputClass";
                             }
                        
                             String styleClass = " requiredstyle ";
                        
                             TagAttribute[] at = arg0.getAttributes().getAll();
                             boolean styleFound = false;
                             for (int j = 0; j < at.length; ++j)
                             {
                                 TagAttribute i = at[j];
                                 if (i.getLocalName().equals(styleAt))
                                 {
                                  styleFound = true;
                                  String val = i.getValue();
                                  TagAttribute ti = new TagAttribute(i.getLocation(), i.getNamespace(), i
                                       .getLocalName(), i.getQName(), val + styleClass);
                                  at[j] = ti;
                                 }
                        
                             }
                             if (!styleFound)
                             {
                                 TagAttribute ti = new TagAttribute(arg0.getLocation(), "", styleAt, styleAt, styleClass);
                                 TagAttribute[] old = at;
                                 at = new TagAttribute[old.length + 1];
                                 for (int k = 0; k < old.length; ++k)
                                 {
                                  at[k] = old[k];
                                 }
                                 at[old.length] = ti;
                             }
                        
                             TagAttributes ats = new TagAttributes(at);
                             return new Tag(arg0, ats);
                        
                            }
                        
                        




                        public Tag decorate(Tag arg0)
                            {
                             if (!arg0.getNamespace().equals("http://richfaces.org/rich"))
                             {
                                 return null;
                             }
                             if (arg0.getAttributes().get("rowClasses") != null)
                             {
                                 return null;
                             }
                             if (arg0.getLocalName().equals("dataTable") || arg0.getLocalName().equals("subTable"))
                             {
                        
                                 TagAttribute[] at = arg0.getAttributes().getAll();
                        
                                 TagAttribute ti = new TagAttribute(arg0.getLocation(), "", "rowClasses", "rowClasses",
                                      "trRow1, trRow2");
                                 TagAttribute[] old = at;
                                 at = new TagAttribute[old.length + 1];
                                 for (int k = 0; k < old.length; ++k)
                                 {
                                  at[k] = old[k];
                                 }
                                 at[old.length] = ti;
                        
                                 TagAttributes ats = new TagAttributes(at);
                                 return new Tag(arg0, ats);
                        
                             }
                             return null;
                        
                            }
                        
                        

                        • 24. Re: Disabling a group of input fields.
                          cash1981

                          I got an exception using this component.


                          Even though I added constructor with a call to super() and it still gives same error.


                          Caused by: javax.faces.FacesException: Cant instantiate class: no.whatever.fag.sak.jsf.UIDisablePanel.
                               at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1016)
                               at com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:539)
                               ... 86 more
                          Caused by: java.lang.InstantiationException
                               at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
                          



                          • 25. Re: Disabling a group of input fields.
                            cash1981

                            Hmmm no wonder. The class is abstract...

                            • 26. Re: Disabling a group of input fields.

                              Sorry I forgot to mention that I use the RichFaces CDK for building components. See here for an introduction.


                              It creates the base classes and files, and generates the corresponding correct components files with all the necessary configuration files.


                              HTH,


                              Marco

                              • 27. Re: Disabling a group of input fields.
                                ladanz

                                So how did this story end?


                                Did the suggested component made it to the official richfaces lib or is it planned to integrate it, or a component with a similar behaviour???


                                Greetings...

                                • 28. Re: Disabling a group of input fields.
                                  nigiyan

                                  here is my working mehtod:


                                  in my template.xhtml


                                  <f:view contentType="text/html">
                                  <head>
                                      ...
                                      <a:loadScript src="/js/disableElements.js"/>
                                  </head>



                                  disableElements.js




                                  function addLoadEvent(func) {
                                  
                                      var oldonload = window.onload;
                                      if (typeof window.onload != 'function') {
                                          window.onload = func;
                                      }
                                      else {
                                          window.onload = function() {
                                              if (oldonload) {
                                                  oldonload();
                                              }
                                              func();
                                          }
                                      }
                                  }
                                  
                                  addLoadEvent(disableElements);
                                  
                                  function disableElements() {
                                    if (document.getElementById('field which i render when need to disable form') != null) {
                                       disableChildrenElements(document.getElementById('i pass here form name to disable all its children'));
                                    }
                                  }
                                  
                                  function disableChildrenElements(parent) {
                                      var objElems = parent.elements;
                                      var i;
                                  
                                      for (i = 0; i < objElems.length; i++) {
                                          if (objElems[i].className == 'i dont want to disable this one')
                                              continue;
                                          objElems[i].readOnly = true;
                                          objElems[i].disabled = true;
                                      }
                                  }
                                  
                                  
                                  



                                  1 2 Previous Next