简体   繁体   English

使用Vaadin从菜单栏打开pdf文件

[英]Open pdf file from menubar using Vaadin

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. 我的vaadin应用程序中有一个菜单栏,想要添加一个项目来打开pdf文件是浏览器的新选项卡。 I found some solutions to open files with a button, but I have to use an MenuItem... 我找到了一些用按钮打开文件的解决方案,但我必须使用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... 注意:这段代码有效,但代码结构并不完美;-)最好将“basepath”和“pdf”存储为属性...

There is a similar problem described here: How to specify a button to open an URL? 此处描述了类似的问题: 如何指定打开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. AbstractClientConnector的setResource方法受到保护,因此您需要扩展一些Vaadin组件才能使其工作。 This is why Im creating the class MyMenuBar here. 这就是我在这里创建MyMenuBar类的原因。 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. 如果您使用外部资源,则无需将其附加到具有setResource的任何组件,然后这不是nessecary。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM