wicketwicket-6wicket-1.5wicket-1.6wicket-7

Apache wicket 7: how to add a behavior to a label inside enclosure?


I have a legacy block of code as shown:

<wicket:enclosure >
   <div class="form-group">
       <label class="col-sm-4 control-label">Vorlage</label>
       <div class="col-sm-8 form-control-static" wicket:id="referencedAeCode">XYZ</div>
   </div>
</wicket:enclosure>

and the corresponding java code is:

add(newReferenceCodeLabel("referencedAeCode")); 

where the impl. of newReferenceCodeLabel() is:

private Label newReferenceCodeLabel(final String id) {
    return new Label(id, new AbstractReadOnlyModel<String>() {

        private static final long serialVersionUID = 5340631294817017953L;

        @Override
        public String getObject() {
        final Element element = getElement();
        String code = element.getCode ();
        if (element instanceof Thing
              && element.getACode().equals(code)) {
                 code = null;
            }
            return code;
        }
     });
    }

Now, I want to add a wicket behavior (using the add() method) to the Label itself inside the enclosure, what currently happening is the behavior being added to the whole enclosure.

<label class="col-sm-4 control-label">Vorlage</label>


Solution

  • You didn't show us the code that adds the Behavior so it is not clear why it behaves this way.

    But I'd recommend you to replace the markup-based enclosure ( <wicket:enclosure>) with the explicit Java-based counterpart - EnclosureContainer:

    New markup:

    <div wicket:id="referencedAeCodeEnclosure" class="form-group">
       <label class="col-sm-4 control-label">Vorlage</label>
       <div class="col-sm-8 form-control-static" wicket:id="referencedAeCode">XYZ</div>
    </div>
    

    New Java code:

    private Label newReferenceCodeLabel(final String id) {
      Label label = new Label(id, new AbstractReadOnlyModel<String>() {
    
        private static final long serialVersionUID = 5340631294817017953L;
    
        @Override
        public String getObject() {
          final Element element = getElement();
          String code = element.getCode ();
          if (element instanceof Thing
              && element.getACode().equals(code)) {
                 code = null;
          }
          return code;
        }
     });
     label.add(someBehavior);
     return label;
    }
    ...
    Label label = newReferenceCodeLabel("referencedAeCode");
    EnclosureContainer container = new EnclosureContainer("referencedAeCodeEnclosure", label);
    container.add(label); 
    add(container);
    }