Posts Tagged ‘html’

Copying html attributes on Lift bindings

Friday, September 18th, 2009

Since Java and Scala are complementary, I will post Scala-related stuff here, too (without creating a new blog).

Today’s tip refers to Lift (Scala’s JavaEE/Rails/Grails/Django). Suppose your webdesigner gives you this XHTML:

<input type="text" class="x" style="width: 100px;"/>

When you bind this using Lift, one of possible solutions is surround it with a tag:

<mybind:myfield><input type="text" class="x" style="width: 100px;"/></mybind:myfield>

Please notice I put it without spaces or newlines between “mybind:myfield” and “input” - this will be important later.

I prefer this way because this XHTML can be opened in other applications, like Firefox and Dreamweaver, making life easier for the webdesigner.

Then, if I bind it using old mama’s recipe:

bind("mybind", form, "myfield" -> myfield.toForm)

Lift will remove my “input” tag and replace it - destroying “class” and “style”. To solve this, I merge “toForm” tag with original attributes:

def merge(form : => Box[NodeSeq])(input : NodeSeq) : NodeSeq = {
  var in = input.first
  var attrs = form.open_!.first.attributes

  new Elem(in.prefix, in.label,
      in.attributes append attrs,
      in.scope, Group(in.child))
}
bind("mybind", form,
    FuncBindParam("myfield", merge(myfield.toForm)))

Function “merge” takes the form and returns a function that receives the original XHTML and translates it into XHTML with Lift’s attributes (”id”, “name”, “lift:gc”, etc). I guess I can improve it somehow, but works great. And, since I’m keeping only the first child of source XHTML, you need to keep “bind” and “input” together (as I said before).

I know I can use a map function to ignore whitespaces, but I’ll leave it as an later exercise (for you and for me, too).

Long lines in Java Tooltips (or multiline tooltips)

Thursday, February 14th, 2008

Do you want to show a tooltip with more than one line? Are your tooltips too large to fit in one line? The solution is easier than you think. Tooltips, in Java, accept HTML code:

jcomponent.setToolTipText("HTML tooltip");

It isn’t a Firefox, but is very powerful. And, when I said HTML, I also think about CSS. So, you can set your tooltips using this evil cheat:

<html><body>
<div style=”width: 300px; text-justification: justify;”>
Blah Blah Blah (repeat “blah” 100 times)
</div>
</html></body>

Have you seen automatic line breaks and the justification? Oh, yeah!

BTW, do you remember old days, when you couldn’t believe WordPad didn’t have justified text? :)