org.apache.click.element
Class ResourceElement

java.lang.Object
  extended by org.apache.click.element.Element
      extended by org.apache.click.element.ResourceElement
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
CssImport, CssStyle, JsImport, JsScript

public class ResourceElement
extends Element

Provides a base class for rendering HEAD resources of an HTML page, for example JavaScript (<script>) and Cascading Stylesheets (<link>/<style>).

Subclasses should override Element.getTag() to return a specific HTML tag.

Below are some example Resource elements:

Remove duplicates

Click will ensure that duplicate Resource elements are removed by checking the isUnique() property. Thus if the same Resource is imported multiple times by the Page or different Controls, only one Resource will be rendered, if isUnique() returns true.

The rules for defining a unique Resource is as follows:

For example:
 public class MyPage extends Page {

     public List getHeadElements() {
         // We use lazy loading to ensure the JavaScript and Css is only added
         // the first time this method is called.
         if (headElements == null) {
             // Get the head elements from the super implementation
             headElements = super.getHeadElements();

             JsImport jsImport = new JsImport("/js/mylib.js");
             // Click will ensure the library "/js/mylib.js" is only included
             // once in the Page
             headElements.add(jsImport);

             JsScript jsScript = new JsScript("alert('Hello!');");
             // Click won't ensure the script is unique because its ID
             // attribute is not defined
             headElements.add(jsScript);

             jsScript = new JsScript("alert('Hello!');");
             jsScript.setId("my-unique-script-id");
             // Click will ensure the script is unique because its ID attribute
             // is defined. Click will remove other scripts with the same ID
             headElements.add(jsScript);

             CssImport cssImport = new CssImport("/css/style.css");
             // Click will ensure the library "/css/style.css" is only
             // included once in the Page
             headElements.add(cssImport);

             CssScript cssScript = new CssScript("body { font-weight: bold; }");
             cssScript.setId("my-unique-style-id");
             // Click will ensure the css is unique because its ID attribute
             // is defined. Click will remove other css styles with the same ID
             headElements.add(cssScript);
         }
         return headElements;
     }
 } 

Automatic Resource versioning

ResourceElement provides the ability to automatically version elements according to Yahoo Performance Rule: Add an Expires or a Cache-Control Header. This rule recommends adding an expiry header to JavaScript, Css and image resources, which forces the browser to cache the resources. It also suggests versioning the resources so that each new release of the web application renders resources with different paths, forcing the browser to download the new resources.

For detailed information on versioning JavaScript, Css and image resources see the PerformanceFilter.

To enable versioning of JavaScript, Css and image resources the following conditions must be met:

Please note: PerformanceFilter handles the above steps for you.

Conditional comment support for Internet Explorer

Sometimes it is necessary to provide additional JavaScript and Css for Internet Explorer because it deviates quite often from the standards.

Conditional comments allows you to wrap the resource in a special comment which only IE understands, meaning other browsers won't process the resource.

You can read more about conditional comments here and here

It has to be said that IE7 and up has much better support for Css, thus conditional comments are mostly used for IE6 and below.

 public class MyPage extends Page {

     public List getHeadElements() {
         // We use lazy loading to ensure the JavaScript and Css is only added
         // the first time this method is called.
         if (headElements == null) {
             // Get the head elements from the super implementation
             headElements = super.getHeadElements();

             CssImport cssImport = new CssImport("/css/ie-style.css");
             // Use one of the predefined conditional comments to target IE6
             // and below
             cssImport.setConditionalComment(IE_LESS_THAN_IE7);
             headElements.add(cssImport);

             cssImport = new CssImport("/css/ie-style2.css");
             // Use a custom predefined conditional comments to target only IE6
             cssImport.setConditionalComment("[if IE 6]");
             headElements.add(cssImport);
         }
         return headElements;
     }
 } 
ResourceElement contains some predefined Conditional Comments namely IF_IE, IF_LESS_THAN_IE7 and IF_IE7.

See Also:
Serialized Form

Field Summary
static String IF_IE
          A predefined conditional comment to test if browser is IE.
static String IF_IE7
          A predefined conditional comment to test if browser is IE7.
static String IF_LESS_THAN_IE7
          A predefined conditional comment to test if browser is less than IE7.
static String IF_LESS_THAN_IE9
          A predefined conditional comment to test if browser is less than IE9.
static String IF_LESS_THAN_OR_EQUAL_TO_IE7
          A predefined conditional comment to test if browser is less than or equal to IE7.
 
Constructor Summary
ResourceElement()
           
 
Method Summary
 String getConditionalComment()
          Return Internal Explorer's conditional comment to wrap the Resource with.
 String getVersionIndicator()
          Return the version indicator to be appended to the resource path.
 boolean isRenderId()
          Returns the element render ID attribute status, default value is true.
 boolean isUnique()
          Returns whether or not the Resource unique.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the Resource element to the specified buffer.
 void setConditionalComment(String conditionalComment)
          Set Internet Explorer's conditional comment to wrap the Resource with.
 void setRenderId(boolean renderId)
          Set the element render ID attribute status.
 void setVersionIndicator(String versionIndicator)
          Set the version indicator to be appended to the resource path.
 
Methods inherited from class org.apache.click.element.Element
appendAttributes, getAttribute, getAttributes, getContext, getId, getTag, hasAttribute, hasAttributes, setAttribute, setId, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

IF_IE

public static final String IF_IE
A predefined conditional comment to test if browser is IE. Value: [if IE].

See Also:
Constant Field Values

IF_LESS_THAN_IE7

public static final String IF_LESS_THAN_IE7
A predefined conditional comment to test if browser is less than IE7. Value: [if lt IE 7].

See Also:
Constant Field Values

IF_IE7

public static final String IF_IE7
A predefined conditional comment to test if browser is IE7. Value: [if IE 7].

See Also:
Constant Field Values

IF_LESS_THAN_OR_EQUAL_TO_IE7

public static final String IF_LESS_THAN_OR_EQUAL_TO_IE7
A predefined conditional comment to test if browser is less than or equal to IE7. Value: [if lte IE 7].

See Also:
Constant Field Values

IF_LESS_THAN_IE9

public static final String IF_LESS_THAN_IE9
A predefined conditional comment to test if browser is less than IE9. Value: [if lt IE 9].

See Also:
Constant Field Values
Constructor Detail

ResourceElement

public ResourceElement()
Method Detail

getVersionIndicator

public String getVersionIndicator()
Return the version indicator to be appended to the resource path.

Returns:
the version indicator to be appended to the resource path.

setVersionIndicator

public void setVersionIndicator(String versionIndicator)
Set the version indicator to be appended to the resource path.

Parameters:
versionIndicator - the version indicator to be appended to the resource path

isUnique

public boolean isUnique()
Returns whether or not the Resource unique. This method returns true if the ID attribute is defined, false otherwise.

Returns:
true if the Resource should be unique, false otherwise.

isRenderId

public boolean isRenderId()
Returns the element render ID attribute status, default value is true.

Returns:
the element render id attribute status, default value is true
See Also:
setRenderId(boolean)

setRenderId

public void setRenderId(boolean renderId)
Set the element render ID attribute status.

If renderId is false the element ID attribute will not be rendered.

Parameters:
renderId - set the element render id attribute status

getConditionalComment

public String getConditionalComment()
Return Internal Explorer's conditional comment to wrap the Resource with.

Returns:
Internal Explorer's conditional comment to wrap the Resource with.

setConditionalComment

public void setConditionalComment(String conditionalComment)
Set Internet Explorer's conditional comment to wrap the Resource with.

Parameters:
conditionalComment - Internet Explorer's conditional comment to wrap the Resource with

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the Resource element to the specified buffer.

If Element.getTag() returns null, this method will return an empty string.

Overrides:
render in class Element
Parameters:
buffer - the specified buffer to render the Resource element output to