Open pdf file from menubar using Vaadin

蹲街弑〆低调 提交于 2019-12-23 16:01:45

问题


I have a menubar in my vaadin application and want to add an item to open a pdf-file is a new tab of the browser. I found some solutions to open files with a button, but I have to use an MenuItem...

MenuBar.Command commandHandler = new MenuBar.Command() {

    @Override
    public void menuSelected(MenuItem selectedItem) {

        if (selectedItem.equals(menu_help)) {
            openHelp();
        }
    }
};

...

menu_help = menuBar
            .addItem("", WebImageList.getImage(ImageList.gc_helpIcon),
                    commandHandler);

...


private void openHelp() {
   // open pdf-file in new window
}

Thanks for help!

SOLUTION:

private void openHelp() {
    final String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();

    Resource pdf = new FileResource(new File(basepath + "/WEB-INF/datafiles/help.pdf"));
    setResource("help", pdf);
    ResourceReference rr = ResourceReference.create(pdf, this, "help");
    Page.getCurrent().open(rr.getURL(), "blank_");
} 

Attention: This code works, but the the structure of code is not perfect ;-) Better is to store "basepath" and "pdf" as attribute...


回答1:


There is a similar problem described here: How to specify a button to open an URL? One possible solution:

public class MyMenuBar extends MenuBar {

    ResourceReference rr;

    public MyMenuBar() {
        Resource pdf = new FileResource(new File("C:/temp/temp.pdf"));
        setResource("help", pdf);
        rr = ResourceReference.create(pdf, this, "help");
    }

    private void openHelp() {
        Page.getCurrent().open(rr.getURL(), "blank_");
    }

    ...
}

The setResource method of AbstractClientConnector is protected, so this is you need to extend some Vaadin component to make it work. This is why Im creating the class MyMenuBar here. If you are using an external resource you don't need to attach it to any component with setResource and then this is not nessecary.




回答2:


I used the following code to do something similar:

private Component buildUserMenu() {
        final MenuBar settings = new MenuBar();
        settings.addStyleName("user-menu");
        final User user = getCurrentUser();       
            settingsItem = settings.addItem("", new ThemeResource(
                    "img/logo.png"), null);     
        updateUserName(null);
        settingsItem.addItem(Lang.getMessage("menu.edit"), new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                ProfilePreferencesWindow.open(user, false);
            }
        });    
        settingsItem.addSeparator();
        settingsItem.addItem(Lang.getMessage("menu.help"), new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                Window help = new Window();
                help.setWidth("90%");
                help.setHeight("90%");
                BrowserFrame e = new BrowserFrame("PDF File", new ThemeResource("pdf/ayuda.pdf"));
                e.setWidth("100%");
                e.setHeight("100%");
                help.setContent(e);
                help.center();
                help.setModal(true);
                UI.getCurrent().addWindow(help);
            }
        });
        settingsItem.addSeparator();
        settingsItem.addItem(Lang.getMessage("menu.logout"), new Command() {
            @Override
            public void menuSelected(final MenuItem selectedItem) {
                BarsEventBus.post(new UserLoggedOutEvent());
            }
        });
        return settings;
    }


来源:https://stackoverflow.com/questions/26211489/open-pdf-file-from-menubar-using-vaadin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!