You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Patrick Shea <pa...@ps1.net> on 2010/05/03 16:20:11 UTC

How to retrieve wtkx:id?

Is there a way to get the wtkx:id of a given component?

Thanks
Patrick


Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
Re:

> I'm just trying to reuse the same listener for more than one component but I can't because I can't separate which is which!

If you provide some sample code or a more detailed description of what you are trying to do, I'm sure we can help you solve this problem.

G


Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
> At the very least the namedObjects map should be exposed and not be private in the serializer.

It is exposed and is intentionally quite public. You can get access to any object declared with a wtkx:id via get() or WTKX binding. You can access nested (included) objects using JSON path syntax, or by getting access to the serializer used to load the include (via getSerializer()) and calling get() on it. Nothing is hidden from Java.


Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
> The use case is to distinguish event sources without having to retrieve their instances, to write general purpose event listeners. Example:
> 
> public class MyButtonPressListener implements ButtonPressListener {
>  public void buttonPressed(Button eventSource) {
>    if (eventSource.getId().equals("copy"))
>      executeCopyAction();
>    else if (eventSource.getId().equals("paste"))
>      executePasteAction();
>  }
> }
> 
> This example is clearly not good programming style, because one could as well write two separate button event listeners, and it is not something that Pivot should encourage. However, there are some situations where this might be handy.

I agree that this is not good programming style, but for a different reason. In this example, you have simply exchanged typed member variables for string IDs - i.e. you have lost information and compilation benefits. 

You still need to define an identifier, whether it is a "copy" ID or a "copyButton" member variable, to determine the source of the event. IMO, the member variable is preferable. The overhead of retrieving and maintaining the instance is negligible and the approach is much more flexible.



Re: How to retrieve wtkx:id?

Posted by Dirk Möbius <dm...@scoop-gmbh.de>.
Greg Brown <gk...@mac.com> wrote:
> I think it would be confusing to have both wtkx:id and @ID.

Just to clarify: the @ID annotation is not for setting _another_ id;  
it is to specify into _which_ bean property the wtkx:id should be  
injected.

> I'm also not sure that the use case for such a feature has been
> clearly articulated.

The use case is to distinguish event sources without having to  
retrieve their instances, to write general purpose event listeners.  
Example:

public class MyButtonPressListener implements ButtonPressListener {
   public void buttonPressed(Button eventSource) {
     if (eventSource.getId().equals("copy"))
       executeCopyAction();
     else if (eventSource.getId().equals("paste"))
       executePasteAction();
   }
}

This example is clearly not good programming style, because one could  
as well write two separate button event listeners, and it is not  
something that Pivot should encourage. However, there are some  
situations where this might be handy.

> Perhaps the automationID property of Component would suffice?

automationID would suffice, but then you'd have to assign two ids in the wtkx:

<Button wtkx:id="copy" automationID="copy" .../>

Regards,
Dirk

> On May 7, 2010, at 3:23 AM, Dirk Möbius wrote:
>> On second thought, I admit Patrick has a point here. I'm not happy  
>> with my own code example because there's too much boilerplate code.  
>> The injected button members are not really needed.
>>
>> Albeit, simply adding an "id" property to Component (analogous to  
>> Swing's "name" property) is not a good solution either, because the  
>> WTKXSerializer (soon to be named BeanSerializer) is more general  
>> and unaware of Component -- it would be awkward to insert something  
>> like:
>>
>>  if (bean instanceof Component)
>>    ((Component)bean).setId(...);
>>
>> Instead I propose a new class annotation:
>>
>> @Id
>> public class Component {
>>  private String id;
>>  // getter and setter...
>> }
>>
>> You can also set the name of the id property:
>>
>> @Id("name")
>> public class MyBean {
>>  private String name;
>>  // getter and setter...
>> }
>>
>> If WTKXSerializer sees the @Id annotation on a bean, it calls its  
>> setter, assuming its type is String. I think that would be general  
>> enough.
>>
>> @Id could also be named @WtkxId or @BxId.
>>
>> What do you think?
>> Pivot 2.0?!?
>>
>> Regards,
>> Dirk
>>
>>
>> Patrick Shea <pa...@ps1.net> wrote:
>>
>>> Unfortunately I don't come from an html background. I'm just  
>>> trying to reuse the same listener for more than one component but  
>>> I can't because I can't separate which is which!
>>>
>>> This is where the id comes into play...
>>>
>>> I don't think that any xml attribute should be hidden from the  
>>> java model, they should always be exposed.
>>>
>>> At the very least the namedObjects map should be exposed and not  
>>> be private in the serializer.
>>>
>>> Patrick
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: "Greg Brown" <gk...@mac.com>
>>> Sent: Monday, May 3, 2010 6:24pm
>>> To: user@pivot.apache.org
>>> Subject: Re: How to retrieve wtkx:id?
>>>
>>> I understand why you might expect it to work that way, especially  
>>> if you are coming from HTML, but it doesn't. Consider that Java  
>>> member variables are also accessible via reflection, but you still  
>>> can't get a variable name from a class instance. WTKX isn't a DOM  
>>> - it's just another way of instantiating classes.
>>>
>>> Hope that helps clarify things.
>>>
>>> Greg
>>>
>>> On May 3, 2010, at 6:16 PM, Patrick Shea wrote:
>>>
>>>> Well, they are declared in xml as id's and used in code in  
>>>> get(...) so they are quite public.
>>>>
>>>> I was able to go around this by reflection but it's ugly and they  
>>>> should really be exposed at the component level I think.
>>>>
>>>> Patrick
>>>>
>>>> -----Original Message-----
>>>> From: "Greg Brown" <gk...@mac.com>
>>>> Sent: Monday, May 3, 2010 6:07pm
>>>> To: user@pivot.apache.org
>>>> Subject: Re: How to retrieve wtkx:id?
>>>>
>>>> No. WTKX IDs don't work like HTML IDs - they are more like  
>>>> variable names. Just as there is no way to retrieve a variable  
>>>> name from an object instantiated in Java, there is no way to  
>>>> retrieve an ID from an object declared in WTKX.
>>>>
>>>> On May 3, 2010, at 10:20 AM, Patrick Shea wrote:
>>>>
>>>>> Is there a way to get the wtkx:id of a given component?
>>>>>
>>>>> Thanks
>>>>> Patrick
>>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>> --
>> Dirk Möbius
>>
>> SCOOP GmbH
>> Am Kielshof 29
>> D-51105 Köln
>> Fon   +49 221 801916-0
>> Fax   +49 221 801916-17
>> Mobil +49 170 7363035
>> www.scoop-gmbh.de
>> Sitz der Gesellschaft: Köln
>> Handelsregister: Köln
>> Handelsregisternummer: HRB 36623
>> Geschäftsführer:
>> Dr. Oleg Balovnev
>> Frank Heinen
>> Dr. Wolfgang Reddig
>> Roland Scheel
>>
>>
>
>



-- 
Dirk Möbius

SCOOP GmbH
Am Kielshof 29
D-51105 Köln
Fon   +49 221 801916-0
Fax   +49 221 801916-17
Mobil +49 170 7363035
www.scoop-gmbh.de
Sitz der Gesellschaft: Köln
Handelsregister: Köln
Handelsregisternummer: HRB 36623
Geschäftsführer:
Dr. Oleg Balovnev
Frank Heinen
Dr. Wolfgang Reddig
Roland Scheel



Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
I think it would be confusing to have both wtkx:id and @ID. I'm also not sure that the use case for such a feature has been clearly articulated. Perhaps the automationID property of Component would suffice?


On May 7, 2010, at 3:23 AM, Dirk Möbius wrote:

> On second thought, I admit Patrick has a point here. I'm not happy with my own code example because there's too much boilerplate code. The injected button members are not really needed.
> 
> Albeit, simply adding an "id" property to Component (analogous to Swing's "name" property) is not a good solution either, because the WTKXSerializer (soon to be named BeanSerializer) is more general and unaware of Component -- it would be awkward to insert something like:
> 
>  if (bean instanceof Component)
>    ((Component)bean).setId(...);
> 
> Instead I propose a new class annotation:
> 
> @Id
> public class Component {
>  private String id;
>  // getter and setter...
> }
> 
> You can also set the name of the id property:
> 
> @Id("name")
> public class MyBean {
>  private String name;
>  // getter and setter...
> }
> 
> If WTKXSerializer sees the @Id annotation on a bean, it calls its setter, assuming its type is String. I think that would be general enough.
> 
> @Id could also be named @WtkxId or @BxId.
> 
> What do you think?
> Pivot 2.0?!?
> 
> Regards,
> Dirk
> 
> 
> Patrick Shea <pa...@ps1.net> wrote:
> 
>> Unfortunately I don't come from an html background. I'm just trying to reuse the same listener for more than one component but I can't because I can't separate which is which!
>> 
>> This is where the id comes into play...
>> 
>> I don't think that any xml attribute should be hidden from the java model, they should always be exposed.
>> 
>> At the very least the namedObjects map should be exposed and not be private in the serializer.
>> 
>> Patrick
>> 
>> 
>> 
>> -----Original Message-----
>> From: "Greg Brown" <gk...@mac.com>
>> Sent: Monday, May 3, 2010 6:24pm
>> To: user@pivot.apache.org
>> Subject: Re: How to retrieve wtkx:id?
>> 
>> I understand why you might expect it to work that way, especially if you are coming from HTML, but it doesn't. Consider that Java member variables are also accessible via reflection, but you still can't get a variable name from a class instance. WTKX isn't a DOM - it's just another way of instantiating classes.
>> 
>> Hope that helps clarify things.
>> 
>> Greg
>> 
>> On May 3, 2010, at 6:16 PM, Patrick Shea wrote:
>> 
>>> Well, they are declared in xml as id's and used in code in get(...) so they are quite public.
>>> 
>>> I was able to go around this by reflection but it's ugly and they should really be exposed at the component level I think.
>>> 
>>> Patrick
>>> 
>>> -----Original Message-----
>>> From: "Greg Brown" <gk...@mac.com>
>>> Sent: Monday, May 3, 2010 6:07pm
>>> To: user@pivot.apache.org
>>> Subject: Re: How to retrieve wtkx:id?
>>> 
>>> No. WTKX IDs don't work like HTML IDs - they are more like variable names. Just as there is no way to retrieve a variable name from an object instantiated in Java, there is no way to retrieve an ID from an object declared in WTKX.
>>> 
>>> On May 3, 2010, at 10:20 AM, Patrick Shea wrote:
>>> 
>>>> Is there a way to get the wtkx:id of a given component?
>>>> 
>>>> Thanks
>>>> Patrick
>>>> 
>>> 
>>> 
>>> 
>> 
>> 
>> 
>> 
> 
> 
> 
> -- 
> Dirk Möbius
> 
> SCOOP GmbH
> Am Kielshof 29
> D-51105 Köln
> Fon   +49 221 801916-0
> Fax   +49 221 801916-17
> Mobil +49 170 7363035
> www.scoop-gmbh.de
> Sitz der Gesellschaft: Köln
> Handelsregister: Köln
> Handelsregisternummer: HRB 36623
> Geschäftsführer:
> Dr. Oleg Balovnev
> Frank Heinen
> Dr. Wolfgang Reddig
> Roland Scheel
> 
> 


Re: How to retrieve wtkx:id?

Posted by Dirk Möbius <dm...@scoop-gmbh.de>.
On second thought, I admit Patrick has a point here. I'm not happy  
with my own code example because there's too much boilerplate code.  
The injected button members are not really needed.

Albeit, simply adding an "id" property to Component (analogous to  
Swing's "name" property) is not a good solution either, because the  
WTKXSerializer (soon to be named BeanSerializer) is more general and  
unaware of Component -- it would be awkward to insert something like:

   if (bean instanceof Component)
     ((Component)bean).setId(...);

Instead I propose a new class annotation:

@Id
public class Component {
   private String id;
   // getter and setter...
}

You can also set the name of the id property:

@Id("name")
public class MyBean {
   private String name;
   // getter and setter...
}

If WTKXSerializer sees the @Id annotation on a bean, it calls its  
setter, assuming its type is String. I think that would be general  
enough.

@Id could also be named @WtkxId or @BxId.

What do you think?
Pivot 2.0?!?

Regards,
Dirk


Patrick Shea <pa...@ps1.net> wrote:

> Unfortunately I don't come from an html background. I'm just trying  
> to reuse the same listener for more than one component but I can't  
> because I can't separate which is which!
>
> This is where the id comes into play...
>
> I don't think that any xml attribute should be hidden from the java  
> model, they should always be exposed.
>
> At the very least the namedObjects map should be exposed and not be  
> private in the serializer.
>
> Patrick
>
>
>
> -----Original Message-----
> From: "Greg Brown" <gk...@mac.com>
> Sent: Monday, May 3, 2010 6:24pm
> To: user@pivot.apache.org
> Subject: Re: How to retrieve wtkx:id?
>
> I understand why you might expect it to work that way, especially if  
> you are coming from HTML, but it doesn't. Consider that Java member  
> variables are also accessible via reflection, but you still can't  
> get a variable name from a class instance. WTKX isn't a DOM - it's  
> just another way of instantiating classes.
>
> Hope that helps clarify things.
>
> Greg
>
> On May 3, 2010, at 6:16 PM, Patrick Shea wrote:
>
>> Well, they are declared in xml as id's and used in code in get(...)  
>> so they are quite public.
>>
>> I was able to go around this by reflection but it's ugly and they  
>> should really be exposed at the component level I think.
>>
>> Patrick
>>
>> -----Original Message-----
>> From: "Greg Brown" <gk...@mac.com>
>> Sent: Monday, May 3, 2010 6:07pm
>> To: user@pivot.apache.org
>> Subject: Re: How to retrieve wtkx:id?
>>
>> No. WTKX IDs don't work like HTML IDs - they are more like variable  
>> names. Just as there is no way to retrieve a variable name from an  
>> object instantiated in Java, there is no way to retrieve an ID from  
>> an object declared in WTKX.
>>
>> On May 3, 2010, at 10:20 AM, Patrick Shea wrote:
>>
>>> Is there a way to get the wtkx:id of a given component?
>>>
>>> Thanks
>>> Patrick
>>>
>>
>>
>>
>
>
>
>



-- 
Dirk Möbius

SCOOP GmbH
Am Kielshof 29
D-51105 Köln
Fon   +49 221 801916-0
Fax   +49 221 801916-17
Mobil +49 170 7363035
www.scoop-gmbh.de
Sitz der Gesellschaft: Köln
Handelsregister: Köln
Handelsregisternummer: HRB 36623
Geschäftsführer:
Dr. Oleg Balovnev
Frank Heinen
Dr. Wolfgang Reddig
Roland Scheel



Re: How to retrieve wtkx:id?

Posted by Dirk Möbius <dm...@scoop-gmbh.de>.
Patrick Shea <pa...@ps1.net> wrote:

> Unfortunately I don't come from an html background. I'm just trying  
> to reuse the same listener for more than one component but I can't  
> because I can't separate which is which!

If you define your components in WTKX such as this:

<PushButton wtkx:id="button1" .../>
<PushButton wtkx:id="button2" .../>

then let Pivot inject the buttons into your controller class:

class Controller {
   @WTKX(id="button1") PushButton button1;
   @WTKX(id="button2") PushButton button2;

   public void createGui() {
     WTKXSerializer serializer = new WTKXSerializer();
     Component topLevelComponent = serializer.readObject(this, "gui.wtkx");
     serializer.bind(this);
     // reuse the same listener for both buttons
     ButtonPressListener listener = new MyButtonPressListener();
     button1.getButtonPressListeners().add(listener);
     button2.getButtonPressListeners().add(listener);
     // ... show topLevelComponent etc.
   }

   // your listener can decide which is which
   private class MyButtonPressListener implements ButtonPressListener {
     public void buttonPressed(Button button) {
       if(button == button1) {
         ...
       } else if(button == button2) {
         ...
       }
     }
   }
}

Regards,
Dirk.




Re: How to retrieve wtkx:id?

Posted by Patrick Shea <pa...@ps1.net>.
Unfortunately I don't come from an html background. I'm just trying to reuse the same listener for more than one component but I can't because I can't separate which is which!

This is where the id comes into play...

I don't think that any xml attribute should be hidden from the java model, they should always be exposed.

At the very least the namedObjects map should be exposed and not be private in the serializer.

Patrick



-----Original Message-----
From: "Greg Brown" <gk...@mac.com>
Sent: Monday, May 3, 2010 6:24pm
To: user@pivot.apache.org
Subject: Re: How to retrieve wtkx:id?

I understand why you might expect it to work that way, especially if you are coming from HTML, but it doesn't. Consider that Java member variables are also accessible via reflection, but you still can't get a variable name from a class instance. WTKX isn't a DOM - it's just another way of instantiating classes.

Hope that helps clarify things.

Greg

On May 3, 2010, at 6:16 PM, Patrick Shea wrote:

> Well, they are declared in xml as id's and used in code in get(...) so they are quite public.
> 
> I was able to go around this by reflection but it's ugly and they should really be exposed at the component level I think.
> 
> Patrick
> 
> -----Original Message-----
> From: "Greg Brown" <gk...@mac.com>
> Sent: Monday, May 3, 2010 6:07pm
> To: user@pivot.apache.org
> Subject: Re: How to retrieve wtkx:id?
> 
> No. WTKX IDs don't work like HTML IDs - they are more like variable names. Just as there is no way to retrieve a variable name from an object instantiated in Java, there is no way to retrieve an ID from an object declared in WTKX.
> 
> On May 3, 2010, at 10:20 AM, Patrick Shea wrote:
> 
>> Is there a way to get the wtkx:id of a given component?
>> 
>> Thanks
>> Patrick
>> 
> 
> 
> 




Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
I understand why you might expect it to work that way, especially if you are coming from HTML, but it doesn't. Consider that Java member variables are also accessible via reflection, but you still can't get a variable name from a class instance. WTKX isn't a DOM - it's just another way of instantiating classes.

Hope that helps clarify things.

Greg

On May 3, 2010, at 6:16 PM, Patrick Shea wrote:

> Well, they are declared in xml as id's and used in code in get(...) so they are quite public.
> 
> I was able to go around this by reflection but it's ugly and they should really be exposed at the component level I think.
> 
> Patrick
> 
> -----Original Message-----
> From: "Greg Brown" <gk...@mac.com>
> Sent: Monday, May 3, 2010 6:07pm
> To: user@pivot.apache.org
> Subject: Re: How to retrieve wtkx:id?
> 
> No. WTKX IDs don't work like HTML IDs - they are more like variable names. Just as there is no way to retrieve a variable name from an object instantiated in Java, there is no way to retrieve an ID from an object declared in WTKX.
> 
> On May 3, 2010, at 10:20 AM, Patrick Shea wrote:
> 
>> Is there a way to get the wtkx:id of a given component?
>> 
>> Thanks
>> Patrick
>> 
> 
> 
> 


Re: How to retrieve wtkx:id?

Posted by Patrick Shea <pa...@ps1.net>.
Well, they are declared in xml as id's and used in code in get(...) so they are quite public.

I was able to go around this by reflection but it's ugly and they should really be exposed at the component level I think.

Patrick

-----Original Message-----
From: "Greg Brown" <gk...@mac.com>
Sent: Monday, May 3, 2010 6:07pm
To: user@pivot.apache.org
Subject: Re: How to retrieve wtkx:id?

No. WTKX IDs don't work like HTML IDs - they are more like variable names. Just as there is no way to retrieve a variable name from an object instantiated in Java, there is no way to retrieve an ID from an object declared in WTKX.

On May 3, 2010, at 10:20 AM, Patrick Shea wrote:

> Is there a way to get the wtkx:id of a given component?
> 
> Thanks
> Patrick
> 




Re: How to retrieve wtkx:id?

Posted by Greg Brown <gk...@mac.com>.
No. WTKX IDs don't work like HTML IDs - they are more like variable names. Just as there is no way to retrieve a variable name from an object instantiated in Java, there is no way to retrieve an ID from an object declared in WTKX.

On May 3, 2010, at 10:20 AM, Patrick Shea wrote:

> Is there a way to get the wtkx:id of a given component?
> 
> Thanks
> Patrick
>