Add CssResource.ensureInjected().

Patch by: bobv
Review by: rjrjr

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@6252 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/resources/Resources.gwt.xml b/user/src/com/google/gwt/resources/Resources.gwt.xml
index 1db713a..3c66d64 100644
--- a/user/src/com/google/gwt/resources/Resources.gwt.xml
+++ b/user/src/com/google/gwt/resources/Resources.gwt.xml
@@ -16,6 +16,8 @@
 <module>
   <!-- Pull in the necessary base support, including user.agent detection -->
   <inherits name="com.google.gwt.core.Core" />
+  <!-- Pull in StyleInjector for CssResource -->
+  <inherits name="com.google.gwt.dom.DOM" />
   <!-- Used by ExternalTextResource -->
   <inherits name="com.google.gwt.http.HTTP" />
 
diff --git a/user/src/com/google/gwt/resources/client/CssResource.java b/user/src/com/google/gwt/resources/client/CssResource.java
index 908c777..5020448 100644
--- a/user/src/com/google/gwt/resources/client/CssResource.java
+++ b/user/src/com/google/gwt/resources/client/CssResource.java
@@ -285,6 +285,16 @@
   }
 
   /**
+   * Calls
+   * {@link com.google.gwt.dom.client.StyleInjector#injectStylesheet(String)} to
+   * inject the contents of the CssResource into the DOM. Repeated calls to this
+   * method on an instance of a CssResources will have no effect.
+   * 
+   * @return <code>true</code> if this method mutated the DOM.
+   */
+  boolean ensureInjected();
+
+  /**
    * Provides the contents of the CssResource.
    */
   String getText();
diff --git a/user/src/com/google/gwt/resources/rg/CssResourceGenerator.java b/user/src/com/google/gwt/resources/rg/CssResourceGenerator.java
index db31336..85846d4 100644
--- a/user/src/com/google/gwt/resources/rg/CssResourceGenerator.java
+++ b/user/src/com/google/gwt/resources/rg/CssResourceGenerator.java
@@ -30,6 +30,7 @@
 import com.google.gwt.dev.util.DefaultTextOutput;
 import com.google.gwt.dev.util.Util;
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.StyleInjector;
 import com.google.gwt.i18n.client.LocaleInfo;
 import com.google.gwt.resources.client.CssResource;
 import com.google.gwt.resources.client.CssResource.ClassName;
@@ -405,6 +406,10 @@
       }
     }
 
+    // Methods defined by CssResource interface
+    writeEnsureInjected(sw);
+    writeGetName(method, sw);
+
     sw.println("public String getText() {");
     sw.indent();
     boolean strict = isStrict(logger, context, method);
@@ -416,12 +421,6 @@
     sw.outdent();
     sw.println("}");
 
-    sw.println("public String getName() {");
-    sw.indent();
-    sw.println("return \"" + method.getName() + "\";");
-    sw.outdent();
-    sw.println("}");
-
     /*
      * getOverridableMethods is used to handle CssResources extending
      * non-CssResource types. See the discussion in computeReplacementsForType.
@@ -912,6 +911,28 @@
     sw.println("}");
   }
 
+  private void writeEnsureInjected(SourceWriter sw) {
+    sw.println("private boolean injected;");
+    sw.println("public boolean ensureInjected() {");
+    sw.indent();
+    sw.println("if (!injected) {");
+    sw.indentln("injected = true;");
+    sw.indentln(StyleInjector.class.getName() + ".injectStylesheet(getText());");
+    sw.indentln("return true;");
+    sw.println("}");
+    sw.println("return false;");
+    sw.outdent();
+    sw.println("}");
+  }
+
+  private void writeGetName(JMethod method, SourceWriter sw) {
+    sw.println("public String getName() {");
+    sw.indent();
+    sw.println("return \"" + method.getName() + "\";");
+    sw.outdent();
+    sw.println("}");
+  }
+
   /**
    * Write all of the user-defined methods in the CssResource subtype.
    */
@@ -927,7 +948,8 @@
 
     for (JMethod toImplement : methods) {
       String name = toImplement.getName();
-      if ("getName".equals(name) || "getText".equals(name)) {
+      if ("getName".equals(name) || "getText".equals(name)
+          || "ensureInjected".equals(name)) {
         continue;
       }
 
diff --git a/user/test/com/google/gwt/resources/client/CSSResourceTest.java b/user/test/com/google/gwt/resources/client/CSSResourceTest.java
index b0c4664..3bb69ec 100644
--- a/user/test/com/google/gwt/resources/client/CSSResourceTest.java
+++ b/user/test/com/google/gwt/resources/client/CSSResourceTest.java
@@ -135,6 +135,10 @@
     @Strict
     HasDescendants descendants();
 
+    // Make sure an empty, no-op CssResource works
+    @Strict
+    CssResource empty();
+
     @Source("16x16.png")
     ImageResource spriteMethod();
   }
@@ -292,6 +296,17 @@
     assertFalse("10".equals(defines.overrideIntClass()));
   }
 
+  public void testEnsureInjected() {
+    Resources r = GWT.create(Resources.class);
+    assertTrue(r.empty().ensureInjected());
+
+    r = GWT.create(Resources.class);
+    assertFalse(r.empty().ensureInjected());
+
+    r = GWT.create(ChildResources.class);
+    assertTrue(r.empty().ensureInjected());
+  }
+
   public void testMultipleBundles() {
     Resources r1 = GWT.create(Resources.class);
     SiblingResources r2 = GWT.create(SiblingResources.class);
diff --git a/user/test/com/google/gwt/resources/client/empty.css b/user/test/com/google/gwt/resources/client/empty.css
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/user/test/com/google/gwt/resources/client/empty.css