wicket panels and parent class
Hi all, I'm a new Wicket user and am developing an application making use of Panels. The Panels are working, however I need to access some objects in the panel that are defined in the parent class and am not sure how to do this. e.g. MyProfile.java: String userId = Profile.getCurrentUserId(); add(new MyInfoPanel("myInfoPanel")); and in MyInfoPanel.java I need to be able to access userId for example. This is just a basic example, I need to access certain objects in MyInfoPanel.java that are defined in the parent class MyProfile.java cheers, Steve smime.p7s Description: S/MIME cryptographic signature
Re: wicket panels and parent class
getParent() ? Steve Swinsburg-2 wrote: > > Hi all, > > I'm a new Wicket user and am developing an application making use of > Panels. The Panels are working, however I need to access some objects > in the panel that are defined in the parent class and am not sure how > to do this. > > e.g. > > MyProfile.java: > > String userId = Profile.getCurrentUserId(); > > add(new MyInfoPanel("myInfoPanel")); > > > and in MyInfoPanel.java > > I need to be able to access userId for example. > > This is just a basic example, I need to access certain objects in > MyInfoPanel.java that are defined in the parent class MyProfile.java > > > > cheers, > Steve > > > > > > > > > > - Michael Sparer http://talk-on-tech.blogspot.com -- View this message in context: http://www.nabble.com/wicket-panels-and-parent-class-tp19722417p19722473.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
And then have to cast it to the class of the parent.. which kinda kills the independent component based idea of reusable panels. Why not pass along the userid when constructing? Or fetch it from the session. Michael Sparer wrote: getParent() ? Steve Swinsburg-2 wrote: Hi all, I'm a new Wicket user and am developing an application making use of Panels. The Panels are working, however I need to access some objects in the panel that are defined in the parent class and am not sure how to do this. e.g. MyProfile.java: String userId = Profile.getCurrentUserId(); add(new MyInfoPanel("myInfoPanel")); and in MyInfoPanel.java I need to be able to access userId for example. This is just a basic example, I need to access certain objects in MyInfoPanel.java that are defined in the parent class MyProfile.java cheers, Steve - Michael Sparer http://talk-on-tech.blogspot.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
I guess I am used to the jsp:include method of including panel type objects since I am originally a JSP developer and then anything in the panel instantly has access to the variables defined in the surrounding parent. I guess I could pass along a HashMap of data to the Panel when its constructed, is this possible/recommended? cheers, Steve On 29 Sep 2008, at 12:58, Thies Edeling wrote: And then have to cast it to the class of the parent.. which kinda kills the independent component based idea of reusable panels. Why not pass along the userid when constructing? Or fetch it from the session. Michael Sparer wrote: getParent() ? Steve Swinsburg-2 wrote: Hi all, I'm a new Wicket user and am developing an application making use of Panels. The Panels are working, however I need to access some objects in the panel that are defined in the parent class and am not sure how to do this. e.g. MyProfile.java: String userId = Profile.getCurrentUserId(); add(new MyInfoPanel("myInfoPanel")); and in MyInfoPanel.java I need to be able to access userId for example. This is just a basic example, I need to access certain objects in MyInfoPanel.java that are defined in the parent class MyProfile.java cheers, Steve - Michael Sparer http://talk-on-tech.blogspot.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] smime.p7s Description: S/MIME cryptographic signature
Re: wicket panels and parent class
Your Panel is just a Java class, so give it a constructor that takes the required object(s), or provide a setter if that wouldn't be too late (ie if the values are required during the panel's construction). Or, take a look at CompoundPropeprtyModel (maybe together with ComponentPropertyModel too depending on your naming conventions). Regards - Cemal http://www.jWeekend.co.uk http://jWeekend.co.uk Steve Swinsburg-2 wrote: > > Hi all, > > I'm a new Wicket user and am developing an application making use of > Panels. The Panels are working, however I need to access some objects > in the panel that are defined in the parent class and am not sure how > to do this. > > e.g. > > MyProfile.java: > > String userId = Profile.getCurrentUserId(); > > add(new MyInfoPanel("myInfoPanel")); > > > and in MyInfoPanel.java > > I need to be able to access userId for example. > > This is just a basic example, I need to access certain objects in > MyInfoPanel.java that are defined in the parent class MyProfile.java > > > > cheers, > Steve > > > > > > > > > > -- View this message in context: http://www.nabble.com/wicket-panels-and-parent-class-tp19722417p19722687.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
possible yes, recommended, no i'd say :-) if you need specific values from MyPanel then pass it via constructor, if you need values/attributes on component (e.g. isVisible) level use getParent() Steve Swinsburg-2 wrote: > > I guess I am used to the jsp:include method of including panel type > objects since I am originally a JSP developer and then anything in the > panel instantly has access to the variables defined in the surrounding > parent. > > I guess I could pass along a HashMap of data to the Panel when its > constructed, is this possible/recommended? > > > cheers, > Steve > > > > > > > On 29 Sep 2008, at 12:58, Thies Edeling wrote: > >> And then have to cast it to the class of the parent.. which kinda >> kills the independent component based idea of reusable panels. >> Why not pass along the userid when constructing? Or fetch it from >> the session. >> >> >> Michael Sparer wrote: >>> getParent() ? >>> >>> >>> >>> Steve Swinsburg-2 wrote: >>> >>>> Hi all, >>>> >>>> I'm a new Wicket user and am developing an application making use >>>> of Panels. The Panels are working, however I need to access some >>>> objects in the panel that are defined in the parent class and am >>>> not sure how to do this. >>>> >>>> e.g. >>>> >>>> MyProfile.java: >>>> >>>> String userId = Profile.getCurrentUserId(); >>>> >>>> add(new MyInfoPanel("myInfoPanel")); >>>> >>>> >>>> and in MyInfoPanel.java >>>> >>>> I need to be able to access userId for example. >>>> >>>> This is just a basic example, I need to access certain objects in >>>> MyInfoPanel.java that are defined in the parent class MyProfile.java >>>> >>>> >>>> >>>> cheers, >>>> Steve >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>> >>> >>> - >>> Michael Sparer >>> http://talk-on-tech.blogspot.com >>> >> >> >> - >> To unsubscribe, e-mail: [EMAIL PROTECTED] >> For additional commands, e-mail: [EMAIL PROTECTED] >> > > > > - Michael Sparer http://talk-on-tech.blogspot.com -- View this message in context: http://www.nabble.com/wicket-panels-and-parent-class-tp19722417p19722714.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
Thanks everyone, I changed my constructor to accept the object I wanted and passed it in from the parent class. It's working now. cheers, Steve --- Steve Swinsburg Portal Systems Developer Centre for e-Science Lancaster University Lancaster LA1 4YT email: [EMAIL PROTECTED] phone: +44 (0) 1524 594870 On 29 Sep 2008, at 13:08, jWeekend wrote: Your Panel is just a Java class, so give it a constructor that takes the required object(s), or provide a setter if that wouldn't be too late (ie if the values are required during the panel's construction). Or, take a look at CompoundPropeprtyModel (maybe together with ComponentPropertyModel too depending on your naming conventions). Regards - Cemal http://www.jWeekend.co.uk http://jWeekend.co.uk Steve Swinsburg-2 wrote: Hi all, I'm a new Wicket user and am developing an application making use of Panels. The Panels are working, however I need to access some objects in the panel that are defined in the parent class and am not sure how to do this. e.g. MyProfile.java: String userId = Profile.getCurrentUserId(); add(new MyInfoPanel("myInfoPanel")); and in MyInfoPanel.java I need to be able to access userId for example. This is just a basic example, I need to access certain objects in MyInfoPanel.java that are defined in the parent class MyProfile.java cheers, Steve -- View this message in context: http://www.nabble.com/wicket-panels-and-parent-class-tp19722417p19722687.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] smime.p7s Description: S/MIME cryptographic signature
Re: wicket panels and parent class
Just be careful. If the object reference in the parent is retrieved from a LoadableDetachableModel, then your panel and its parent can get out of sync. On Mon, Sep 29, 2008 at 8:16 AM, Steve Swinsburg <[EMAIL PROTECTED]> wrote: > Thanks everyone, > > I changed my constructor to accept the object I wanted and passed it in from > the parent class. It's working now. > > cheers, > Steve > > --- > Steve Swinsburg > Portal Systems Developer > Centre for e-Science > Lancaster University > Lancaster > LA1 4YT > > email: [EMAIL PROTECTED] > phone: +44 (0) 1524 594870 > > > > > > > > On 29 Sep 2008, at 13:08, jWeekend wrote: > >> >> Your Panel is just a Java class, so give it a constructor that takes the >> required object(s), or provide a setter if that wouldn't be too late (ie >> if >> the values are required during the panel's construction). Or, take a look >> at >> CompoundPropeprtyModel (maybe together with ComponentPropertyModel too >> depending on your naming conventions). >> >> Regards - Cemal >> http://www.jWeekend.co.uk http://jWeekend.co.uk >> >> >> >> >> Steve Swinsburg-2 wrote: >>> >>> Hi all, >>> >>> I'm a new Wicket user and am developing an application making use of >>> Panels. The Panels are working, however I need to access some objects >>> in the panel that are defined in the parent class and am not sure how >>> to do this. >>> >>> e.g. >>> >>> MyProfile.java: >>> >>> String userId = Profile.getCurrentUserId(); >>> >>> add(new MyInfoPanel("myInfoPanel")); >>> >>> >>> and in MyInfoPanel.java >>> >>> I need to be able to access userId for example. >>> >>> This is just a basic example, I need to access certain objects in >>> MyInfoPanel.java that are defined in the parent class MyProfile.java >>> >>> >>> >>> cheers, >>> Steve >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >> >> -- >> View this message in context: >> http://www.nabble.com/wicket-panels-and-parent-class-tp19722417p19722687.html >> Sent from the Wicket - User mailing list archive at Nabble.com. >> >> >> - >> To unsubscribe, e-mail: [EMAIL PROTECTED] >> For additional commands, e-mail: [EMAIL PROTECTED] >> > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
It may not be a "best practice", but if MyInfoPanel does not need to be "reusable" you can make it an inner class of MyProfile. Then userId can be accessed directly. Phil On Mon, Sep 29, 2008 at 6:45 AM, Steve Swinsburg < [EMAIL PROTECTED]> wrote: > Hi all, > I'm a new Wicket user and am developing an application making use of > Panels. The Panels are working, however I need to access some objects in the > panel that are defined in the parent class and am not sure how to do this. > > e.g. > > MyProfile.java: > > String userId = Profile.getCurrentUserId(); > > add(new MyInfoPanel("myInfoPanel")); > > > and in MyInfoPanel.java > > I need to be able to access userId for example. > > This is just a basic example, I need to access certain objects in > MyInfoPanel.java that are defined in the parent class MyProfile.java > > > > cheers, > Steve > > > > > > > > -- Phil Grimm Mobile: (858) 335-3426 Skype: philgrimm336
Re: wicket panels and parent class
On Mon, 29 Sep 2008, James Carman wrote: > Just be careful. If the object reference in the parent is retrieved > from a LoadableDetachableModel, then your panel and its parent can get > out of sync. Because of this, it is often better to abstract the access with IModel. Best wishes, Timo -- Timo Rantalaiho Reaktor Innovations Oyhttp://www.ri.fi/ > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
On Mon, 29 Sep 2008, Michael Sparer wrote: > possible yes, recommended, no i'd say :-) > > if you need specific values from MyPanel then pass it via constructor, if > you need values/attributes on component (e.g. isVisible) level use > getParent() I think that getParent() is generally a bad idea, because it adds too much coupling and makes testing difficult. Whenever you can simply pass dependencies in a constructor, or set them from the parent after construction, that's probably the best way. You can even make the object references updateable by passing and storing a model wrapping the object instead of the object itself. The visitor-based event mechanisms discussed in WICKET-1312 could be used to some extent, or you can use the visitors for pull-style stuff as well as my colleague did recently public interface SelectedUserSource { IModel selectedUser(); } public class UserProfile extends Panel implements SelectedUserSource {... and in calling component private IModel findSelectedUser() { return (IModel) getPage().visitChildren(SelectedUserSource.class, new IVisitor() { public Object component(Component component) { return ((SelectedUserSource) component).selectedUser(); } }): } Having the dependent class as an inner class of the dependee makes sense in trivial cases but is often just a more explicit way of doing getParent(). I think that we desperately need to find ways to make our UI (Wicket) code good object-oriented code. I for one find myself often putting way too much stuff and coupling in a component class. Best wishes, Timo -- Timo Rantalaiho Reaktor Innovations Oyhttp://www.ri.fi/ > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: wicket panels and parent class
Exactly. The (typed) IModel abstraction approach is the most "wickety" way of doing it. :) On Mon, Sep 29, 2008 at 9:13 PM, Timo Rantalaiho <[EMAIL PROTECTED]> wrote: > On Mon, 29 Sep 2008, James Carman wrote: >> Just be careful. If the object reference in the parent is retrieved >> from a LoadableDetachableModel, then your panel and its parent can get >> out of sync. > > Because of this, it is often better to abstract the access > with IModel. > > Best wishes, > Timo > > -- > Timo Rantalaiho > Reaktor Innovations Oyhttp://www.ri.fi/ > > > - > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]