Ooh. I've not seen the trick of passing int[] to make it pass by reference
semantics! 

Not that I've got hero worship or anything now but why visit children rather
than Component.iterator?

Your still breaking my requirement that this behaviour is encapsulated
within MyFancyRepeatingView ;) I really do appreciate all your code and I
think I'm learning a lot even if I sound horribly ungrateful. I'm warming to
every child component having a special behaviour object. Although it seems
expensive does show nice encapsulation.

How about MyFancyRepeatingView has a singleton behaviour object that it
checks is attached to all its children (should be quick as identity
operation - in childIterator). Requires only single instance for whole
application... I might be able to convince the HTML monkey that he only
needs class="first" so I don't need to iterate through all the siblings.

Anyway, many thanks Kent.


Kent Tong wrote:
> 
> 
> 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#a12964994
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to