webfirmframework for Java Experts

What is SharedTagContent class

It is a class which can be used to change inner content of one or more tags (even from multiple BrowserPage instances) from a single point.

Features of SharedTagContent

  1. Update content of multiple tags
  2. Shared behavior can be turn off/on
  3. Formatter can be set to show content in different format in different consumer tags
  4. Can be detached from all of its consumer tags with a single method
  5. Listeners can be added for content change & detach event
  6. Thread-safe & auto dereferencing
  7. Allows parallel push only when appropriate
Update content of multiple tags

SharedTagContent object can be added in tags using addInnerHtml or subscribeTo method. Eg:


SharedTagContent<String> stc = new SharedTagContent<>("Initial Content can also be empty");

Div div = new Div(null);

Span span1 = new Span(div);
span1.subscribeTo(stc);

Span span2 = new Span(div);
span2.addInnerHtml(stc);

System.out.println(div.toHtmlString());
stc.setContent("Content Changed");
System.out.println(div.toHtmlString());
				

In the first System.out.println(div.toHtmlString()); it will print as follows

<div><span>Initial Content can also be empty</span><span>Initial Content can also be empty</span></div>

In the second System.out.println(div.toHtmlString()); it will print as follows

<div><span>Content Changed</span><span>Content Changed</span></div>
Turning on/off shared behavior

By default the shared behavior property is set as true unless it is specified as a constructor argument. It can be changed using setShared(boolean) . It is useful in wffweb app. It if turned off then content changed by setContent method will not be reflected in all of its consumer tags dynamically but internal content of SharedTagContent object will be changed so that when it is added to a new tag the updated content will be applied.


SharedTagContent<String> stc = new SharedTagContent<>("Initial Content can also be empty");
stc.setShared(false);
Setting ContentFormatter

In the following code, the second argument in new Span(usdDv).addInnerHtml is an object of ContentFormatter which is passed as a lambda function. The method of ContentFormatter should always return string type of content SharedTagContent.Content<String> . The first constructor argument in SharedTagContent.Content<String> specifies the string to be embedded in the consumer tag and the second argument specifies whether the content is an HTML, i.e. true for HTML and false for text.

SharedTagContent stcAmountInUsd = new SharedTagContent<>(null);
                
new Div(null).give(dv -> {
    new Div(dv).give(usdDv -> {
        new NoTag(usdDv, "Amount in GBP: ");
        new Span(usdDv).addInnerHtml(stcAmountInUsd, content -> {
            
            BigDecimal amountInUsd = content.getContent();
            if (amountInUsd != null) {
              //1 USD to 0.78 GBP
                BigDecimal exchangeRate = new BigDecimal("0.78").setScale(10);  
                BigDecimal amountInGbp = amountInUsd.multiply(exchangeRate);
                String amountInGbpString = amountInGbp.stripTrailingZeros().toPlainString();                                
                return new SharedTagContent.Content<String>(amountInGbpString, false);
            }
            
            return new SharedTagContent.Content<String>("--", false);
        });
    });
    
    new Div(dv).give(usdDv -> {
        new NoTag(usdDv, "Amount in Yen: ");
        new Span(usdDv).addInnerHtml(stcAmountInUsd, content -> {
            
            BigDecimal amountInUsd = content.getContent();
            if (amountInUsd != null) {
              //1 USD to 108.80 Japanese Yen
                BigDecimal exchangeRate = new BigDecimal("108.80").setScale(10);  
                BigDecimal amountInYen = amountInUsd.multiply(exchangeRate);
                String amountInYenString = amountInYen.stripTrailingZeros().toPlainString();                                
                return new SharedTagContent.Content<String>(amountInYenString, false);
            }
            
            return new SharedTagContent.Content<String>("--", false);
        });
    });
});

stcAmountInUsd.setContent(new BigDecimal("1000"));
detach method in SharedTagContent

sharedTagContent.detach will remove this object from all of its consumer tags. The argument true in sharedTagContent.detach(true) method call means that the embedded content should also be removed from the consumer tags and false means not to remove the embedded content but remove the SharedTagContent object from its consumer tags. There are some other overloaded methods available for various operations. Refer its javadoc for details.

Subscribe to this youtube channel to get demonstration video about all of its features. Minimum 1000 subscribers required to publish video.