get position of application level window

I have the need to store/restore the position and size of an application level window. The handling of the size is no problem but the postion is.

I think the methods Window.getPositionX() and Window.getPositionY() are only set when the window is a subwindow. So, my question is: how can I access the position of such a window? I searched a lot but couldn’t find a solution. Is it maybe possible to read values via JavaScript? Personally, I think executeJavaScript() is a one way road, but maybe I’m wrong. Or is it possible to access the DOM somehow?

Thanks in advance

Andreas

As far as I know, the information is not available from the Vaadin API. You can get the coordinates in JavaScript with window.screenX and window.screenY, as mentioned
here
(note the surprising exception for IE).

Passing them to the server-side from JavaScript is more or less difficult in Vaadin 6. Writing a custom widget to do the task is probably the most proper way, although may need a bit too much effort for such a simple task. Another way is to write the values to a cosmetically invisible TextField (use setDebugId() to find its DOM element), and then cause a request so that it’s sent to the server. Yet another way is to make an XMLHttpRequest to a server-side URIHandler/ParameterHandler, and then use vaadin.forceSync() to synchronize the UI with the server. JavaScript integration is much easier in Vaadin 7.

Hi Marko,

thanks for your ideas. I tried the hidden TextField approach: I’m able to set the value of the TextField via JavaScript (confirmed with Firebug), but when I access the TextField.getValue() from Java, I get always the initial value that was set when the TextField was created. I think the problem is that the server doesn’t know the value has been changed.
Do I need something like ICEpush in order to get it working or is there a simpler way to “cause a request so that it’s sent to the server”?

Best regards

Andreas

You need to focus and blur the TextField for it to notice the change. See
this forum post
. You can do it with just a TextField in immediate mode, but using a button may be helpful as well.

A code example:

TextField value = new TextField("Value");
value.setDebugId("jsvalue");
value.addListener(new ValueChangeListener() {
    @Override
    public void valueChange(ValueChangeEvent event) {
        getWindow().showNotification("Value: " + event.getProperty().getValue());
    }
});
value.setImmediate(true);
layout.addComponent(value);

Button execute = new Button("Execute");
execute.addListener(new ClickListener() {
    public void buttonClick(ClickEvent event) {
        getWindow().executeJavaScript(
            "document.getElementById('jsvalue').focus(); " +
            "document.getElementById('jsvalue').value = 'Foo'; " +
            "document.getElementById('jsvalue').blur(); ");
    }
});
layout.addComponent(execute);

Notice that a value change event is only sent if the value actually changes.

Thank you for your example Marko! It helped me a lot. Since the ValueChangeListener is only triggered while the TextField is visible, I had to use a little bit of CSS to hide it from the user. Maybe you have a better solution at hand?

Best regards

Andreas

Well yes, by “cosmetically invisible” above I meant having a “display: none” CSS for it. I hope that doesn’t suppress the events or something, but if it does, set the height to 0px or 1px or something.

Hi Marko,

my solution with the hidden component works nicely. Nevertheless, I would like to write a litte component to do it the right way… Since I’m not very familiar with Vaadin client side component development, I would like to ask you the following:

How can I access (on the client) the application level window where the component belongs to? I tried to climb the parent hierachy up to the root element, but I could not find a parent of type window. The top most one is a RootPanel.

As you might remember, I need to get the screenX and screenY or screenLeft and screenTop values from the window.

Any help is much appreciated

Andreas

Nobody has a clue how to access the application level window?

Regards

Andreas

Does nobody has an idea? I would really appreciate any help.

Thanks

Andreas