01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.util;
09:
10: import java.util.HashMap;
11: import java.util.Iterator;
12: import java.util.Map;
13: import java.util.Map.Entry;
14:
15: import com.google.gwt.user.client.Element;
16:
17: /**
18: * Sets multiple CSS rules on a style element.
19: */
20: public class StyleTemplate {
21: private Element styleElem;
22: private Map ruleMap = new HashMap();
23:
24: /**
25: * Creates a new template.
26: * @param styleElem the HTML style element
27: */
28: public StyleTemplate(Element styleElem) {
29: this .styleElem = styleElem;
30: }
31:
32: /**
33: * Sets a new rule.
34: * @param selector the CSS selector
35: * @param rule the rule
36: */
37: public void set(String selector, String rule) {
38: if (rule == null) {
39: ruleMap.remove(selector);
40: } else {
41: ruleMap.put(selector, rule);
42: }
43: }
44:
45: /**
46: * Applies the rules to the element.
47: */
48: public void apply() {
49: StringBuffer sb = new StringBuffer();
50: for (Iterator iter = ruleMap.entrySet().iterator(); iter
51: .hasNext();) {
52: Map.Entry entry = (Entry) iter.next();
53: sb.append("\n" + entry.getKey()).append(" {").append(
54: entry.getValue()).append("}");
55: }
56: CSS.setRules(styleElem, sb.toString());
57: }
58: };
|