5 Replies Latest reply on Aug 3, 2007 1:13 AM by jbuechel

    Bind a string property by reference

    jbuechel

      Hi

      I'm trying to bind (or pass) a string property via "Parameterized Value Binding" to a dialog class as follows:

      <s:link value="#{entr.text}" action="#{defaultDialogManager.open('title','lable', entry.text)}" />
      

      In the dialog class i set a new value like this:
      sourceProperty = inputFieldText;

      which should be equal to:
      entry.setText(inputFieldText);


      Is there a nice way to get the reference passed to the dialog class and not a copy of the string? (should be a generic solution)

      I'd appreciate every hint.

        • 1. Re: Bind a string property by reference
          matt.drees

          I don't think it's real clear from your code what you're trying to do. You might want to post more code.

          • 2. Re: Bind a string property by reference
            jbuechel

            I see...

            The datatable in facelets page:

             <rich:dataTable value="#{entryList}" var="entr"
             rendered="#{entryList.size>0}" id="dtEntryList" rows="10"
             width="100%">
             <f:facet name="header">
             <rich:columnGroup>
             <h:column>
             <h:outputText styleClass="headerText" value="Id" />
             </h:column>
             <h:column>
             <h:outputText styleClass="headerText" value="Title" />
             </h:column>
             <h:column>
             <h:outputText styleClass="headerText" value="Text" />
             </h:column>
             </rich:columnGroup>
             </f:facet>
             <h:column>
             <h:outputText value="#{entr.id}" />
             </h:column>
             <h:column>
             <h:commandLink value="#{entr.title}" action="#{listManager.select}" />
             </h:column>
             <h:column>
             <s:link value="#{entr.text}" action="#{defaultDialogManager.open('Neue Funktion','Funktionsname', entr.text)}" />
              
             <s:link value="delete"
             action="#{listManager.delete}" />
             </h:column>
             </rich:dataTable>
            
            


            DialogManager class which opens a dialog (rich:modalpanel) by setting the display property true. Also the title, label and the object (called source) which should be modified are passed to the open method:
            @Scope(ScopeType.SESSION)
            @Name("defaultDialogManager")
            public class DefaultDialogManager implements DialogManager, Serializable {
            ...
             public String open(String title, String label, Object source){
             log.info("open called");
            
             setDisplay(true);
            
             Map labelMap = new HashMap();
             labelMap.put("title", title);
             labelMap.put("label", label);
            
             inputDialog.init(labelMap, source);
            
             return null;
             }
             public String accept(){
             log.info("accept called");
             inputDialog.accept();
             log.info("accepted: inputDialog.source: "+inputDialog.getSource());
             log.info("accepted: inputDialog.input: "+inputDialog.getInput());
             return close();
             }
            
            ..
            }
            


            Dialog facelet contains an h:inputtext and h:commandbutton with an accept action:
             <ui:define name="dialogContent">
             <h:panelGrid columns="1" styleClass="fwc-input-dialog">
             <h:outputLabel for="input">#{defaultDialogManager.inputDialog.labelMap.label}</h:outputLabel>
             <h:inputText id="input"
             value="#{defaultDialogManager.inputDialog.input}" />
             </h:panelGrid>
             </ui:define>
            
             <ui:define name="dialogActionButtons">
             <h:commandButton value="#{messages['controls.save']}"
             action="#{defaultDialogManager.accept}" />
             <h:commandButton value="#{messages['controls.cancel']}"
             action="#{defaultDialogManager.cancel}" />
             </ui:define>
            


            When #{defaultDialogManager.accept} is executed, SimpleInputDialog.accept() is called (inputDialog.accept();)

            InputDialog implementation class:
            public class SimpleInputDialog implements InputDialog {
            
             @Logger
             Log log;
            
             private Map labelMap;
             private Object source;
             private Object input;
            
             public void init(Map labelMap, Object source) {
            
             this.labelMap = labelMap;
             this.source = source;
             this.input = source;
             }
            
             public void accept() {
            
             source = input;
             }
            
             public void cancel() {
            
             }
            ...
            }


            the reference pointing to "input" should be assigned to "source":
            source = input;

            Is that clearly?
            (seems like i didn't listen to the english teacher enough.. ;-) )


            • 3. Re: Bind a string property by reference
              jbuechel

              Can someone shortly take time to tell me if my approach makes sence at all?

              Would be great!

              • 4. Re: Bind a string property by reference
                matt.drees

                So, it looks like your intention is that entr.text to be changed from whatever it is to SimpleInputDialog.input when accept() is called. You can't do that by passing 'source' as a method parameter into SimpleInputDialog.init(). 'source' is simply a copy of a reference. In particular, if 'source' points to a String, you can't change it, because Strings are immutable. So no, your approach won't work.

                I'm not too familiar with richfaces' input dialogues, so I can't really make a suggestion, but you may be able to pass 'entr' instead of 'entr.text' into SimpleInputDialog, This would give you the ability to call entr.setText() in your accept() method.

                Good luck

                • 5. Re: Bind a string property by reference
                  jbuechel

                  Thank you matt!

                  Actually, i implemented SimpleInputDialog by my self. So i can easily change it..

                  Ok, it seem's there is no way around this approach. I was looking for an easy generic one.

                  So i think i'll go with calling the entr.setText() by reflection (must be generic). That means i have to pass not only the data, but also the method name..

                  Thanks again!