質問

Is there any way to pass a dynamic URL for new BrowserWindowOpener(dynamicURL) ?

I want get implementation like: select file in table and click on button which will be open a browser with view of file; you can open random file so the parameter for BrowserWindowOpener must be dynamic.

I know that there is other way for open like: Page.getCurrent().open... but I need to disable URL bar for change like BrowserWindowOpener do.

役に立ちましたか?

解決

The solution I have found might be considered a hack. If any seasoned Vaadin developer happens to come across this post, please let me know if there is a better way.

In the meantime, the solution would be the following:

  • initialize the BrowserWindowOpener with an ExternalResource (you can put any url in the constructor of the ExternalResource) which has the getURL method overridden to return a value that depends on the selection within the table
  • every time the value in the table is changed, mark the BrowserWindowOpener as dirty, in order to trigger the change in the shared state

Below is an example:

public class DownloadFileTable extends CustomComponent {

private String currentSelectedFile = null;

public DownloadFileTable() {

    VerticalLayout layout = new VerticalLayout();
    setCompositionRoot(layout);

    IndexedContainer container = new IndexedContainer();
    container.addContainerProperty("name", String.class, "default");
    container.addItem("image1.jpg").getItemProperty("name").setValue("image1.jpg");
    container.addItem("text.csv").getItemProperty("name").setValue("text.csv");
    container.addItem("document.doc").getItemProperty("name").setValue("document.doc");

    Button downloadButton = new Button("Download selected file");
    final BrowserWindowOpener browserWindowOpener = new BrowserWindowOpener(new ExternalResource("http://google.com") {

        @Override
        public String getURL() {
            return currentSelectedFile;
        }
    });
    browserWindowOpener.setFeatures("location=0");
    browserWindowOpener.extend(downloadButton);

    Table table = new Table("Files", container);
    table.setSelectable(true);
    table.setImmediate(true);
    table.addValueChangeListener(new Property.ValueChangeListener() {
        @Override
        public void valueChange(Property.ValueChangeEvent event) {
            currentSelectedFile = (String) event.getProperty().getValue();
            browserWindowOpener.markAsDirty();
        }
    });
    layout.addComponent(table);
    layout.addComponent(downloadButton);
}

}

The browserWindowOpener.setFeatures("location=0"); tells the popup window to restrict the user from modifying the address bar.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top