12

I want to write a web application that triggers the default email client of the user to send an email.

Thus, I created a Link, that leads to an URL conforming to the mailto URI scheme (http://en.wikipedia.org/wiki/Mailto):

Link emailLink = new Link("Send Email", 
    new ExternalResource("mailto:someone@example.com"));

However, instead of using a Link, I want to provide a Button that allows to trigger the respective functionality. But, for buttons I cannot set an ExternalResource to be opened.

Does anybody know to solve this problem for Buttons, or how to create a Link that looks and behaves exactly like a button? I also tried some CCS modification but did not manage the task by myself. I also found some solutions for former Vaadin versions (https://vaadin.com/forum/#!/thread/69989), but, unfortunately they do not work for Vaadin 7.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Claas Wilke
  • 1,951
  • 1
  • 18
  • 29
  • 3
    Use CSS to make your link look like a button. – Kevin Workman Mar 12 '14 at 14:50
  • 2
    Well, this sound easy, however my application is using a big CCS theme with many classes and properties for Buttons (including hover stuff etc.). Is there an easy way to apply all theses CCS classes to the respective Link? – Claas Wilke Mar 12 '14 at 14:55
  • Make sure the link has the correct class and/or id? What have you tried? You could also google "html make button open link". – Kevin Workman Mar 12 '14 at 15:05
  • You might find this similar Question helpful: [Vaadin: open new window with ABSOLUTE url path from a BUTTON](http://stackoverflow.com/q/23280379/642706) – Basil Bourque Mar 14 '16 at 23:30

3 Answers3

14

I remember solving a similar problem using a ResourceReference.

Button emailButton = new Button("Email");
content.addComponent(emailButton);
Resource res = new ExternalResource("mailto:someone@example.com");
final ResourceReference rr = ResourceReference.create(res, content, "email");

emailButton.addClickListener(new Button.ClickListener() {

    @Override
    public void buttonClick(ClickEvent event) {
        Page.getCurrent().open(rr.getURL(), null);
    }
});
kris54321
  • 776
  • 4
  • 7
2

For solving similar issue, I applied previously:

    String email="info@ORGNAME.org";
    Link l=new Link();
    l.setResource(new ExternalResource("mailto:" + email));
    l.setCaption("Send email to " + email);
    addComponent(l);
Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43
0

After some further tries a managed to adapt the proposed LinkButton solution from https://vaadin.com/forum/#!/thread/69989 for Vaadin 7:

public class LinkButton extends Button {
    public LinkButton(final String url, String caption) {
        super(caption);
        setImmediate(true);
        addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = -2607584137357484607L;

            @Override
            public void buttonClick(ClickEvent event) {
                LinkButton.this.getUI().getPage().open(url, "_blank");
            }
        });
    }
}

However, this solution is still not perfect as it causes the opening of a popup window being blocked by some web browsers.

Claas Wilke
  • 1,951
  • 1
  • 18
  • 29
  • 1
    Consider using kris54321' approach, using Resources and Reference is safer than just operating on Strings. Plus: you wont solve the "some browsers block pop ups" problem, thats client config. Consider a world where the ad can decide to ignore popup blockers ... In a company where every user uses the same browser you might succeed via central policies. – Jan Galinski Mar 13 '14 at 10:22