0

How to get UiLifeCycle instance from zk.xml

asked 2015-12-02 11:10:41 +0800

viking gravatar image viking
3 1

I have a UiLifeCycle defined in zk.xml. Is there any way to get an instance created by zk at run-time?

delete flag offensive retag edit

4 Answers

Sort by ยป oldest newest most voted
0

answered 2015-12-09 04:34:54 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2015-12-09 07:42:51 +0800

I'd be careful with that, since according to the documentation there should only be one instance of a UiLifeCycle-listener. Which needs to be implemented in a thread safe way. For that it's better to keep it stateless.

As an alternative you could add a callback as a component attribute to be called when the component detaches:

e.g. in your afterComponentDetached method

if(comp.hasAttribute("cleanupCallback")) {
    Runnable callback = (Runnable)comp.getAttribute("cleanupCallback");
    callback.run();
}

you can then register the callback where you create the component
Textbox mytext = new Textbox();
mytext.setAttribute("cleanupCallback", new Runnable() {
    public void run() {
        //do some cleanup here
    }
});

for java 8

mytext.setAttribute("cleanupCallback", () -> {
    //do some cleanup here
});

or

mytext.setAttribute("cleanupCallback", this::doCleanup);
...
public void doCleanup() {
    //do some cleanup here
}
link publish delete flag offensive edit
0

answered 2015-12-09 08:00:42 +0800

viking gravatar image viking
3 1

@cor3000: I really like your answer! Great idea - already implemented :)

P.S. If there should be only one UILifecycle, then how should I listen do detach event of all components? I can't see any reason not to register new listener for this. Also in DesktopImpl there is a list of UILifecyces defined...

link publish delete flag offensive edit

Comments

maybe my answer was a bit clumsy here the official docs http://books.zkoss.org/wiki/ZKConfigurationReference/zk.xml/ThelistenerElement/Theorg.zkoss.zk.ui.util.UiLifeCycleinterface there should be only one instance of each listener configured, so there could be more, but still each is shared

cor3000 ( 2015-12-09 11:26:34 +0800 )edit
0

answered 2015-12-08 01:07:25 +0800

cor3000 gravatar image cor3000
6280 2 7

as of the source code the UiLifeCycles instances are stored in a private variable in the Desktop see: https://github.com/zkoss/zk/blob/master/zk/src/org/zkoss/zk/ui/impl/DesktopImpl.java#L187

currently this list is not accessible outside of the Desktop.

You'd have to implement/extend your own DesktopImpl and create it with a custom UiFactory

e.g. overriding the addListener method and cache the UiLifeCycle instance you are interested in.

http://books.zkoss.org/wiki/ZKDeveloper'sReference/Customization/UIFactory http://books.zkoss.org/wiki/ZKConfigurationReference/zk.xml/Thesystem-configElement/Theui-factory-class_Element

Robert

link publish delete flag offensive edit

Comments

however I have never come across a reason to make these internals available to the UI... maybe there is a better way to achieve your requirements, just in case you can share some details?

cor3000 ( 2015-12-08 01:12:59 +0800 )edit
0

answered 2015-12-08 12:03:04 +0800

viking gravatar image viking
3 1

I have some components that needs to execute some logic onDetach. It is easy to iterate through the tree of detached component in afterComponentDetached and call method of some interface. Yet I have some non-component objects that also needs to execute logic when specified component is detached. So I figured it would be good, if those objects just registers themselves in my UILifecycle saying call me also, if this components detaches. And that is a moment, where getting zk.xml class instance comes in.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-12-02 11:10:41 +0800

Seen: 38 times

Last updated: Dec 09 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More