I am trying to add a popup menu to a table which opens an external window. I am trying to use “BrowserWindowOpener” to open the window. Sinde there seems to be no direct method to trigger the opening of a new window, I am trying to do this via a button:
final Action actionGotoExternalLink = new Action("Goto Link");
table.addActionHandler(new Action.Handler() {
@Override
public Action[] getActions(final Object target, final Object sender)
{
return new Action[] { actionGotoExternalLink };
}
@Override
public void handleAction(final Action action, final Object sender, final Object target)
{
if (action == actionGotoExternalLink)
{
String url = "www.google.com";
// enrich url with info from the selected row
BrowserWindowOpener bwo = new BrowserWindowOpener(url);
Button b = new Button();
bwo.extend(b);
b.click();
}
}
});
I am sure that this is completely wrong. What would be the correct approach to open a new window programmatically?
The fundamental problem is that most browsers block any popups not opened directly based on user action. A popup opened in JavaScript based on an event such as a click from the user can work, but trying to open one from a request to the server doesn’t. That is the reason why BrowserWindowOpener exists - so that the popup can be opened entirely on the client side when the component has been extended with it before the end user clicks it. This is also why code such as you posted cannot work, as it would try to do this on the server side.
Currently, you cannot extend or modify the Action mechanism on the client side easily.
I would recommend replacing actions with the
ContextMenu add-on and either customizing that add-on yourself or asking its author to customize it to support menu items that directly open a page if that is not already supported. The add-on is probably much easier to customize for this than the built-in actions, and is also otherwise nicer in many situations.
Actions currently do not support using a custom component such as a Link.
The whole Action framework is long overdue for a thorough redesign, which unfortunately did not make it to Vaadin 7. It is there essentially unchanged (API-wise) since version 3.0, long before GWT, where the client side was totally different from the current one and much more limited.
Maybe at some point something simpler and much more flexible (perhaps similar to the ContextMenu add-on) will provide a better alternative for the Action framework, which could then be removed in a later major version.