Get window the component is attached to

In Vaadin 6.xx you could just use, Component.getWindow(),
in Vaadin 7 this function is not anymore available.

Do you know a way to get this done?
Do I have to loop on getParent() until I find an instance of window?

If you want the UI - the browser level window - you can call Component#getUI()
If you want the Window (i.e. inside a floating window inside the UI), then yes, you have to loop.

There is a handy findAncestor method on AbstractComponent that does the looping for you, - so you can can call Window w = component.findAncestor(Window.class);

Cheers,

Charles.

Thanks, I’ll try that out

What was the case of restricting this method parameter to HasComponents? With this restriction this method is useless for me: I have an interface that is implemented by both custom Window and custom UI.

I assume you’re referring to the type parameter ; I don’t know, I didn’t write it !

I guess the logic was to fund any parent component in the tree, not an arbitrary interface - I can see that finding an interface would be useful. Being a pragmatist, I’d just create my own version of the method in a utility class somewhere changing the type parameter, and use it instead of AbstractComponent.findAncestor - a bonus being it could work with Component, and prevent the need for casting.

Cheers,

Charles.

Probably a little late to help you, but maybe this will help someone else.

If your interface is only implemented by components that implement HasComponents (if it doesn’t I would think that your interface might span multiple tiers of the MVC pattern, and should probably be rethought), then just make your custom interface extend HasComponents:

public interface MyCustomInterface extends HasComponents{
//your methods here.
}

public class MyCustomWindow extends Window implements MyCustomInterface {
//your implementations here. Methods from HasComponents are already implemented in
//Window.class
}

With this implementation, calling component.findAncestor(MyCustomInterface.class) should work.

I have Layouts for view/edits which I have to load in the main window or in a sub window based on how it gets accessed.
In this case, what would be the best way to get the window… With vaadin 6 it worked fine as the main window and the subwindows were all windows… Now I am moving to vaadin 7 and cant find the best way to do it.

Thanks in advance
Hari