The issue I have was hard to reproduce but I hope someone here can give me a hint on how to solve this.
I've written a ComponentProvider<JLabel>
that takes a String value an creates an clickable cell decorated with a icon if the String contains a URL. If no URL is found in the String value, then no icon is set for the label and the cell shouldn't be clickable.
Here's the code:
public class ExternalLinkProvider extends ComponentProvider<JLabel> implements RolloverRenderer {
private String url = null;
@Override
protected void format(CellContext context) {}
@Override
protected void configureState(CellContext context) {
if (context.getValue() instanceof String) {
String stringValue = (String) context.getValue();
WwwLink link = new WwwLink(stringValue);
// If this contains a valid Url, set a link icon.
if (link.isValid()) {
rendererComponent.setIcon(ExternalLink.WWW_LINK_ICON);
url = link.getUrl();
} else {
rendererComponent.setIcon(null);
url = null;
}
rendererComponent.setText(link.getString());
}
}
@Override
protected JLabel createRendererComponent() {
return new JRendererLabel();
}
@Override
public boolean isEnabled() {
return url != null;
}
@Override
public void doClick() {
if (url != null) {
// Follow the url
}
}
}
It looks like this:
The first cell should be clickable to follow the link that was in the string value and has been removed from the display text. The second cell didn't contain a link in the text and therefore shouldn't be clickable.
However, in this case both cells aren't clickable and if I store the string value and print it out to the console from the isEnabled()
method I see that always the text from the second cell is printed regardless if i hover over the first or the second cell.
This is the only situation I noticed this behavior. I know that the one Provider instance is reused for every cell containing this type, but for some reason the RolloverRenderer doesn't seem to reconfigure this Provider properly in this case.
Am I doing something wrong here?