Sam Hough wrote:
>
> I'm full of cold so probably being very thick but that doesn't work for
> RepeatingView does it as it implies notification of objects being attached
> to a parent :( It looks very clever but I'm not having one of those "god
> that is so simple why didn't I think of that" moments... Have you been
> following the "remove final for add, remove, removeAll" etc thread? Seems
> like I could do a simple (if ugly) and efficient version that those
> changes. To add to the horror I've got my own add(int i, Component c)
> method , that igor didn't like, that I want to honour.
>
The code below seems to work for a RepeatingView. Of course it is a hack as
it depends on the
order in which the children are visited. However, it doesn't depend on
notification of objects
being attached to the parent. The bind() method is called when the behavior
is added to the
component.
public class Home extends WebPage {
private static class BoundHighlighter extends AttributeModifier {
private Component owner;
public BoundHighlighter() {
super("class", true, null);
}
protected String newValue(String currentValue, String
replacementValue) {
int idx = getItemIndex();
return idx == 0 ? "first" : (idx == getItemCount() - 1
? "last"
: null);
}
public void bind(Component component) {
super.bind(component);
this.owner = component;
}
private int getItemIndex() {
final int[] idx = { -1 };
Object result = owner.getParent().visitChildren(new
IVisitor() {
public Object component(Component component) {
idx[0]++;
return component == owner ? idx[0]
:
CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
}
});
return (Integer) result;
}
private int getItemCount() {
return owner.getParent().size();
}
}
public Home() {
RepeatingView view = new RepeatingView("view");
view.add(new Label("x", "a").add(new BoundHighlighter()));
view.add(new Label("y", "b").add(new BoundHighlighter()));
view.add(new Label("z", "c").add(new BoundHighlighter()));
view.add(new Label("t", "d").add(new BoundHighlighter()));
add(view);
}
}
--
View this message in context:
http://www.nabble.com/Reach-into-a-component-to-change-XML-attribute-tf4527906.html#a12961809
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]