I created a custom component that should represent a breadcrumb, which is based on a regular label.
The code works as expected, but the label value is not updated on the screen.
The value of the label is set correctly, because when debugging I can see the previously set text.
How can I make the label refresh on screen ?
public class BreadCrumbs extends Label implements Page.UriFragmentChangedListener {
private static final String BREAD_CRUMB_START_ELEMENT = "Start";
public BreadCrumbs() {
super(BREAD_CRUMB_START_ELEMENT);
}
/**
* The character used to separate parts from the path. Doesn't need to be /.
*/
private static final String FRAGMENT_SEPARATOR = "/";
/**
* Splits the URI Fragment into its composite parts, using {@link #FRAGMENT_SEPARATOR} as its delimiter.
* @return
*/
private String[] splitFragmentIntoPaths(String cleanFragment) {
return cleanFragment.split(FRAGMENT_SEPARATOR);
}
/**
* Get the current fragment without superfluous {@link #FRAGMENT_SEPARATOR}
* @return A cleaned URI fragment.
*/
private String getCleanFragment(Page.UriFragmentChangedEvent event) {
String fragment = event.getUriFragment();
if (fragment != null && fragment.length() > 0) {
fragment = fragment.replaceFirst("^" + FRAGMENT_SEPARATOR + "+", "");
fragment = fragment.replaceFirst(FRAGMENT_SEPARATOR + "+$", "");
return fragment;
} else {
return "";
}
}
/**
* {@inheritDoc}
*/
@Override
public void uriFragmentChanged(Page.UriFragmentChangedEvent event) {
String cleanFragment = getCleanFragment(event);
String []cleanFragments = splitFragmentIntoPaths(cleanFragment);
StringBuilder breadCrumbTextBuilder = new StringBuilder();
breadCrumbTextBuilder.append(BREAD_CRUMB_START_ELEMENT);
for (String fragment : cleanFragments) {
breadCrumbTextBuilder.append(" > ");
breadCrumbTextBuilder.append(fragment);
}
setValue(breadCrumbTextBuilder.toString());
markAsDirty();
}
}
I configured this component within my UI like this :
// Link the breadcrumbs to this application
getPage().addUriFragmentChangedListener(breadCrumbs);