Scaladin questions

I feel like I’m missing something obvious.

How do I create a LinkButton rather than a plain Button? I’ve tried LinkButton(“Home”), new LinkButton(“Home”), etc.

How do I go from a standard Validator to something that Scaladin wants? I’ve tried:

val email = new TextField { caption = "Email" }
email.required = true
email.validators += new EmailValidator("Invalid address")

but, again, I’m not passing the correct class.

Feels like I’m missing an import but I have no clue what it is.

Think you are looking for Link not LinkButton …

If you want a button that acts like a normal link:

com.vaadin.ui.Link link = new com.vaadin.ui.Link(“Google”, new com.vaadin.terminal.ExternalResource(“http://www.google.com”));

If you want a button that looks like a link :

com.vaadin.ui.Button button = new com.vaadin.ui.Botton(“Home”);
button.setStyleName( com.vaadin.ui.themes.BaseTheme.BUTTON_LINK );
button.addListener( new com.vaadin.ui.Button.ClickListener() {
… implement your Listener…
});

What about this code from Button.scala?

class LinkButton(override val p: com.vaadin.ui.Button with ButtonMixin = new com.vaadin.ui.Button with ButtonMixin) extends Button(p) {
p.setStyleName(BaseTheme.ButtonLink)
}

If I create a vaadin.scala.Button, it lacks the ButtonMixin. But if I make a com.vaadin.ui.Button, I’m not sure how to bring the mixin in. I’d have expected the Scala class would have it, but it doesn’t.

The class vaadin.scala.Button also uses ButtonMixin for its constructor parameter just like LinkButton does.

If you want something that looks and behaves like a button, use a Button. If you want something that looks and behaves like a link, use a Link.

ButtonLink is for the rare cases where you want a button to look like a link but its clicks be handled on the server side. However, this restricts what can be done with it (the browser does not allow certain actions to be initiated by the server), and the user will not be able to use “open in new tab” etc.

In general, you should

import vaadin.scala._

See e.g.
escalante-scaladin
for some minimal examples on how to use Scaladin - it also uses a validator etc. in
EditorApp.scala

Currently there’s no way to create a Link button by saying LinkButton(“Caption”), but you can create a LinkButton as follows:

val linkButton = new LinkButton {
	caption = "Click me!"
    clickListeners += { e =>
		Notification.show("LinkButton clicked!")
	}
}

Good question. Currently there are no built-in wrappers for the validators provided by Vaadin (although there should be), but luckily it’s quite easy to wrap them yourself. You can either do a separate class for the wrapping, or do it like the test I just wrote for Scaladin:

  test("Use Java Vaadin validators, inline wrap") {
    import scala.util._
    import scala.util.control.Exception._

    val field1 = new TextField
    field1.validators += Validator(value => {
      catching(classOf[com.vaadin.data.Validator.InvalidValueException]
) withTry (new com.vaadin.data.validator.EmailValidator("foo").validate(value.orNull)) match {
        case Success(value) => Valid
        case Failure(exception) => Invalid(List(exception.getMessage))
      }
    })

    field1.value = "foo"
    assert(field1.validate === Invalid(List("foo")))
    
    field1.value = "test@email.com"
    assert(field1.validate === Valid)
  }