|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.element.Element org.apache.click.element.ResourceElement org.apache.click.element.CssStyle
public class CssStyle
Provides a Css HEAD element for including inline Cascading Stylesheets using the <style> tag.
Example usage:public class MyPage extends Page { public List getHeadElements() { // We use lazy loading to ensure the CSS import is only added the // first time this method is called. if (headElements == null) { // Get the header entries from the super implementation headElements = super.getHeadElements(); CssStyle cssStyle = new CssStyle("body { font: 12px arial; }"); headElements.add(cssStyle); } return headElements; } }The cssStyle instance will render as follows:
<style type="text/css"> body { font: 12px arial; } </style>Below is an example showing how to render inline CSS from a Velocity template. First we create a Velocity template (/css/style-template.css) which contains the variable $context that must be replaced at runtime with the application context path:
.blue { background: #00ff00 url('$context/css/blue.png') no-repeat fixed center; }Next is the Page implementation:
public class MyPage extends Page { public List getHeadElements() { // We use lazy loading to ensure the 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(); Context context = getContext(); // Create a default template model to pass to the template Map model = ClickUtils.createTemplateModel(this, context); // Specify the path to CSS template String templatePath = "/css/style-template.css"; // Create the inline Css for the given template path and model CssStyle cssStyle = new CssStyle(templatePath, model); headElements.add(cssStyle); } return headElements; } }The Css above will render as follows (assuming the context path is myApp):
<style type="text/css"> .blue { background: #00ff00 url('/myApp/css/blue.png') no-repeat fixed center; } </style>
Css
in
CDATA tags. Two use cases are common for doing this:
setCharacterData(boolean)
to true. Below is shown how the Css
content would be rendered:
<style type="text/css"> /∗<![CDATA[∗/ div > p { border: 1px solid black; } /∗]]>∗/ </style>Notice the CDATA tags are commented out which ensures older browsers that don't understand the CDATA tag, will ignore it and only process the actual content. For an overview of XHTML validation and CDATA tags please see http://javascript.about.com/library/blxhtml.htm.
Field Summary |
---|
Fields inherited from class org.apache.click.element.ResourceElement |
---|
IF_IE, IF_IE7, IF_LESS_THAN_IE7, IF_LESS_THAN_IE9, IF_LESS_THAN_OR_EQUAL_TO_IE7 |
Constructor Summary | |
---|---|
CssStyle()
Construct a new Css style element. |
|
CssStyle(String content)
Construct a new Css style element with the given content. |
|
CssStyle(String template,
Map<String,Object> model)
Construct a new Css style element for the given template path and template model. |
Method Summary | |
---|---|
boolean |
equals(Object o)
|
String |
getContent()
Return the CssStyle content. |
Map<String,Object> |
getModel()
Return the model of the template
to render. |
String |
getTag()
Returns the Css HTML tag: <style>. |
String |
getTemplate()
Return the path of the template to render. |
int |
hashCode()
|
boolean |
isCharacterData()
Return true if the CssStyle's content should be wrapped in CDATA tags, false otherwise. |
void |
render(HtmlStringBuffer buffer)
Render the HTML representation of the CssStyle element to the specified buffer. |
protected void |
renderContent(HtmlStringBuffer buffer)
Render the CssStyle content
to the specified buffer. |
void |
setCharacterData(boolean characterData)
Sets whether the CssStyle's content should be wrapped in CDATA tags or not. |
void |
setContent(String content)
Set the CssStyle content. |
void |
setModel(Map<String,Object> model)
Set the model of the template to render. |
void |
setTemplate(String template)
Set the path of the template to render. |
Methods inherited from class org.apache.click.element.ResourceElement |
---|
getConditionalComment, getVersionIndicator, isRenderId, isUnique, setConditionalComment, setRenderId, setVersionIndicator |
Methods inherited from class org.apache.click.element.Element |
---|
appendAttributes, getAttribute, getAttributes, getContext, getId, hasAttribute, hasAttributes, setAttribute, setId, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public CssStyle()
public CssStyle(String content)
content
- the Css contentpublic CssStyle(String template, Map<String,Object> model)
content
.
For example:
public class MyPage extends Page { public void onInit() { Context context = getContext(); // Create a default template model Map model = ClickUtils.createTemplateModel(this, context); // Create CssStyle for the given template path and model CssStyle style = new CssStyle("/mypage-template.css", model); // Add style to the Page Head elements getHeadElements().add(style); } }
template
- the path of the template to rendermodel
- the template modelMethod Detail |
---|
public String getTag()
getTag
in class Element
public String getContent()
public void setContent(String content)
content
- the CssStyle contentpublic boolean isCharacterData()
public void setCharacterData(boolean characterData)
characterData
- true indicates that the CssStyle's content should be
wrapped in CDATA tags, false otherwisepublic String getTemplate()
setTemplate(java.lang.String)
public void setTemplate(String template)
template
property is set, the template and model
will be merged and the result will be rendered together with any CssStyle
content
.
template
- the path of the template to renderpublic Map<String,Object> getModel()
template
to render.
setModel(java.util.Map)
public void setModel(Map<String,Object> model)
template
property is set, the template and model
will be merged and the result will be rendered together with any CssStyle
content
.
model
- the model of the template to renderpublic void render(HtmlStringBuffer buffer)
render
in class ResourceElement
buffer
- the buffer to render output topublic boolean equals(Object o)
equals
in class Object
o
- the object with which to compare this instance with
Object.equals(java.lang.Object)
public int hashCode()
hashCode
in class Object
Object.hashCode()
protected void renderContent(HtmlStringBuffer buffer)
content
to the specified buffer.
Please note: if the template
property is set, this method will merge the template
and model
and the result will be
rendered, together with the CssStyle
content
,
to the specified buffer.
buffer
- the buffer to append the output to
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |