4

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...

Kara
  • 6,115
  • 16
  • 50
  • 57
Sheldon
  • 276
  • 3
  • 17
  • Your class is a subclass of `Component`, or to be precise `AbstractClientConnector`. As Kris mentioned, a subclass of `Component` still needs to provide `setResource("help", pdf);` function. So if you define the `MenuBar` inside a class that is not a `Component` you need to explicitly attach it to a component. – Abbas Dec 01 '14 at 23:18

2 Answers2

4

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.

Community
  • 1
  • 1
kris54321
  • 776
  • 4
  • 7
  • Thanks for help! Now it works! I have modified your solution that you don't need to create a new MenuBar class. The help-file is stored in WEB-INF folder. See code above – Sheldon Oct 06 '14 at 10:02
0

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;
    }
Fabri Pautasso
  • 485
  • 6
  • 17