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

4

1 回答 1

3

GWT 目前使用 Flute 解析 CSS,但是有一个讨论迁移到 CSS Parser ( http://cssparser.sourceforge.net/ )。您可以直接使用其中一个解析器。

但是,对于您提到的用例-我认为您不需要那么重的东西。GWT 实际上不解析宽度和高度字符串,它只是将其传递给浏览器 DOM,然后浏览器对其进行适当的评估。你也应该这样做。

这意味着,您的 API 只需要一个字符串。调用者可以传递 ems 或 pxs 或百分比。您的 API 并不关心,因为它只会将其推送到浏览器以进行理解。

于 2010-04-09T14:36:33.527 回答