Parameter passing between Windows

Hello,

Consider following classes:

package com.example.vaadintest;

import com.vaadin.Application;
import com.vaadin.ui.Window;

public class VaadintestApplication extends Application {
	@Override
	public void init() {
		Window windowA = new WindowA();
		setMainWindow(windowA);
	}

	@Override
	public Window getWindow(String windowName) {
		System.out.println("getWindow: " + windowName);
		Window window = super.getWindow(windowName);
		if (window == null) {
			if (windowName.equals("window_a")) {
				window = new WindowA();
			} else if (windowName.equals("window_b")) {
				window = new WindowB();
			}
			if (window != null) {
				addWindow(window);
			}
		}
		return window;
	}
}
package com.example.vaadintest;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Select;
import com.vaadin.ui.Window;

public class WindowA extends Window {
	public WindowA() {
		setName("window_a");
		System.out.println("Window A created");

		Select selection = new Select("Select:");
		selection.addItem("param1");
		selection.addItem("param2");
		addComponent(selection);
		
		Button button = new Button("To Window B");
		addComponent(button);
		button.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				open(new ExternalResource("window_b"));
			}
		});
	}
}
package com.example.vaadintest;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window;

public class WindowB extends Window {
	public WindowB() {
		setName("window_b");
		System.out.println("Window B created");
		Button button = new Button("To Window A");
		addComponent(button);
		button.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				open(new ExternalResource("window_a"));
			}
		});
	}
}

How I can pass the data from Window A to Window B?
For example, how can I pass the
selection
value in the Window A to the Window B?

-Timo

Anyone?
How to transfer data between windows?

Hi,

It’s a bit tricky to give you guidance, as I’m not sure precisely what constraints or intent you have.

I am guessing that you want the URL to govern which window you show, which is why you are overriding application#getName, and that you want to pass simple textual selections from one window to the other. In which case, you might want to use the
UriFragmentUtility
on WindowB, and change WindowA to be something like


open(new ExternalResource("window_b#" + selection.getValue()));

But if you aren’t tied to Windows + URL’s etc, you could simply have one Window, and change the content appropriately. That’s what I’d do.

Cheers,

Charles.

A second option is to store the information into the Application class, where WindowA can go and save it and WindowB can go and read.

A third option is to do a simple Event-Listener-Handler pattern. A bit like every listener in Vaadin does. WindowA has a addListener(SomeListener) method, WindowB implements SomeListener and the application does windowA.addListener(windowB). This way windowA can call a method defined in SomeListener for every listener that is registered and it will run code inside windowB. You can find some more info on this here:
http://download.oracle.com/javase/tutorial/uiswing/events/index.html

I have never done anything like the things you are trying to do, and because of this I have a hard time seeing any real use cases and being more specific.

Thanks Charles and Jens.

I was already thinking about the
UriFragmentUtility
, but in my opinion URL parameters are not the most beautiful solution in this case.

The ‘application data storage’ way could be ok. Actually, the windows are already cached by the application, so the data is too. WindowB just needs a reference to the windowA.

The listener way sounds interesting. I will try that one.

By the way, there was a small bug in the test code. The window opening call should have been something like this:

			public void buttonClick(ClickEvent event) {
				String url = getApplication().getURL() + "window_b";
				open(new ExternalResource(url));
			}

This ‘bug’ is a quite interesting, because the bugged code works in Jetty but not in Tomcat… well, maybe another message thread for that.

-Timo