views:

101

answers:

1

Does gwt have a built in parser for converting css based strings to java constructs?

IE I'm interested in writing my own widget that takes for instance height/width:

public class MyWidget extends Composite{

  private double width;
  private Style.Unit widthUnit;
  private double height;
  private Style.Unit heightUnit;

  /**
  * @width - width of my widget in CSS dimensions (ie. "10px", "1em" ...)
  * @height - height of my widget in CSS dimensions (ie. "10px", "1em" ...)
  */
  public MyWidget(String width, String height){
  // Parse out strings and set dimensions / units
  }
}

I could easily do some regex on this but I thought I'd see if there was some sort of built in mechanism for this, I'd hate to re-invent...

Also, I of course thought about just accepting double width, Style.Unit widthUnit in the constructor, but UiObject's setWidth takes a CSS string, so I still have to do some String munging, albeit the other way around. Ideally I'd have 2 constructors to achieve both possibilities with a simple mechanism to parse the Strings / dimensions and Units properly.

EDIT:

Forgot to mention the REASON that i'd like to parse out these values:

I'm trying to write a simple scroll panel (think iphone nav) so when a user clicks a button, a new 'item' scrolls in from the right.

On the backend this would be:
1) Fetch new scroll item
2) Extend scroll wrapper to accompany new item (this is where i need actual widths)
2) Append to item scroll wrapper
3) Animate scroll wrapper (by sliding left the width of 1 item) to expose newly added item to visible section of panel wrapper

The reason I need original widths is because I'd like to extend my scroller panel by the width of the item added to it. If I'm just getting strings, I can't exactly say setWidth( currentWidth + itemWidth)

Basically I'm trying to mimic JQuery Tools Scroller

+2  A: 

GWT currently uses Flute to parse CSS, however there is a discussion to migrate to CSS Parser (http://cssparser.sourceforge.net/). You could directly use one of those parsers.

BUT, for the use case you mention - I don't think you need anything that heavy. GWT actually doesn't parse the width and height strings, it just passes it to browsers DOM and then the browser evaluates it appropriately. You should do the same.

Meaning, your API just takes a string. The caller can pass ems or pxs or percentages. Your API isn't concerned, because it will just push it to the browser to understand.

sri
+1 this is correct. `setWidth()` methods take a string, which can be anything: http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/UIObject.html#setWidth(java.lang.String)
Jason Hall
check my edit, i needed to better explain why this is necessary for me
brad