Copyright© 2008-2018 SiteVision AB, all rights reserved.
@Requireable(value="FileIconRenderer") public interface FileIconRenderer
An instance of the SiteVision class implementing this interface can be obtained via Utils.getFileIconRenderer()
.
See Utils
for how to obtain an instance of the Utils
interface.
FileIconRenderer is very suitable when rendering more than one file icon (for instance when rendering a search result where different icons should be displayed for different types of hits) or if you don't want to bother about how to get file icons from the icon repository of the site.
FileIconRenderer has few attributes:
null
. If you try to load a new file icon and it fails,
the previously loaded icon will be cleared (i.e. the renderer will not contain any icon to render).
null
. If you try to
load a new default file icon and it fails, the previously loaded default icon will be cleared (i.e. the renderer will not contain any
default icon to render).
true
.true
.
sv-noborder
(i.e. border-style:none
).
----------------------------------------------------------------------------------------------------
Using the FileIconRenderer is pretty straightforward, if you remember that it is stateful and that previously loaded icons will be cleared whenever you try to load new ones. Conceptually you would typically use it like this:
When you have rendered once, you can re-use the FileIconRenderer until you are done. Something like:
Loading of an icon can be done using:
Some information about content type mappings:
A precise file extension (pdf, doc, png etc.) is always needed to extract a file icon from a web site. To enable file icon extraction
via content types, a "content-type-to-extension" mapping will be used. Please be aware that not all content types will be mapped due to
the sheer amount of existing types. Some content types can't be mapped since they doesn't have a single well-defined extension
(a typical example is "application/octet-stream" that is used for a variety of file types, i.e. file extensions).
Whenever you try to load the renderer with an icon, the (possibly) previously loaded icon will be cleared. If loading fails, no result will be rendered by default (i.e. an empty string will be the output when calling the render method). If a default icon has been loaded successfully, it will be the output if the renderer contains no icon. (Note that the result of every icon loading is cached for best performance, hence no need for you to do the same in your code.)
Please note that all load
methods return a boolean! To prevent the boolean to be part of the page output you might in
some cases need to do a workaround. The set statement for $ignore
in below example demonstrates a simple workaround.
Example of how this strategy could be implemented in Velocity:
(You want to do a simple listing of all nodes in a collection/iterator "$items" and use FileIconRenderer to render the file icons of all items
that has a configured file icon
## Get PropertyUtil to ease Node property fetching
#set ($propertyUtil = $sitevisionUtils.propertyUtil)
## Get FileIconRenderer
#set ($iconRenderer = $sitevisionUtils.fileIconRenderer)
## Iterate through items and render the names and file icons (if they exist)
<ul style="list-style:none">
#foreach ($item in $items)
## Try to load the renderer by the item's uri (e.g. "/aPath/aFile.pdf")
#if (!$iconRenderer.loadIconByURI(${propertyUtil.getString($item, 'URI')}))
## If loading failed, try to load the renderer by the item's content type instead (e.g. "application/pdf")
#set ($ignore = $iconRenderer.loadIconByContentType(${propertyUtil.getString($item, 'mimeType')}))
#end
<li>
## Render the name of the node
$propertyUtil.getString($item, 'displayName', '')
## Render the file icon of the node (if no icon was loaded, an empty string will be rendered)
$iconRenderer.render()
</li>
#end
</ul>
load
method, you must suppress the boolean result so
it doesn't get a part of the rendered output. The above example contains an assignment workaround to suppress the load
result: #set ($ignore = $iconRenderer.loadIconByContentType(${propertyUtil.getString($item, 'mimeType')}))
This type of workaround can also be achieved via the swallow
method of ScriptUtil
as: $scriptUtil.swallow($iconRenderer.loadIconByContentType(${propertyUtil.getString($item, 'mimeType')}))
Since FileIconRenderer is stateful and likely will be used in Velocity frequently, there are some "shortcuts" that might be useful. Due to the lack of proper boolean support in Velocity, the boolean attributes has corresponding force/clear methods to set the boolean to true/false. For example:
forceUseSmallIcons()
results in an execution of setUseSmallIcons(true)
clearUseSmallIcons()
results in an execution of setUseSmallIcons(false)
Modifier and Type | Method and Description |
---|---|
void |
clearDefaultIcon()
Removes loaded default icon (does nothing if no default icon is loaded)
|
void |
clearFontClass()
Removes the CSS class name(-s).
|
void |
clearIcon()
Removes loaded icon (does nothing if no icon is loaded)
|
void |
clearUseDescription()
Utility method for executing
setUseDescription(false) , i.e. |
void |
clearUseSmallIcons()
Utility method for executing
setUseSmallIcons(false) , i.e. |
void |
forceUseDescription()
Utility method for executing
setUseDescription(true) , i.e. |
void |
forceUseSmallIcons()
Utility method for executing
setUseSmallIcons(true) , i.e. |
boolean |
isDefaultIconLoaded()
Whether or not a default icon is loaded.
|
boolean |
isIconLoaded()
Whether or not an icon is loaded.
|
boolean |
loadDefaultIconByContentType(String aContentType)
Uses a content type to load the fallback icon that should be used when rendering if no icon is loaded.
|
boolean |
loadDefaultIconByFileExtension(String aFileExtension)
Uses a file extension to load the fallback icon that should be used when rendering if no icon is loaded.
|
boolean |
loadDefaultIconByURI(String aURI)
Uses a URI to load the fallback icon that should be used when rendering if no icon is loaded.
|
boolean |
loadIconByContentType(String aContentType)
Uses a content type to load the icon that should be rendered.
|
boolean |
loadIconByFileExtension(String aFileExtension)
Uses a URI to load the icon that should be rendered.
|
boolean |
loadIconByURI(String aURI)
Uses a URI to load the icon that should be rendered.
|
String |
render()
Builds a html img element based on current state.
|
void |
setFontClass(String aFontClass)
Sets the CSS class name(-s) to use when rendering.
|
void |
setLocale(java.util.Locale aLocale)
The locale to use when extracting the decription of file icons.
|
void |
setUseDescription(boolean aUseDescription)
If file icon descriptions should be used or not.
|
void |
setUseSmallIcons(boolean aUseSmallIcons)
Which type if file icons (small or large) that should be rendered.
|
void setLocale(java.util.Locale aLocale)
Default is the locale of the currently executing portlet.
Note! Icons (icon and default icon) that has been loaded with a different locale will be removed whenever an invocation of this method changes the locale.
aLocale
- the locale to use when extracting the icon descriptionvoid setUseSmallIcons(boolean aUseSmallIcons)
Default is true
.
Note! Icons (icon and default icon) that has been loaded with a different useSmallIcons state will be removed whenever an invocation of this method changes the useSmallIcons state.
aUseSmallIcons
- whether or not to use small icons (true
== "use small icons", false
== "use large icons")void clearUseSmallIcons()
setUseSmallIcons(false)
, i.e. "use large icons when rendering".setUseSmallIcons(boolean)
void forceUseSmallIcons()
setUseSmallIcons(true)
, i.e. "use small icons when rendering".setUseSmallIcons(boolean)
void setUseDescription(boolean aUseDescription)
Default is true
.
Note that file icon descriptions may be locale-dependent, see setLocale(java.util.Locale)
.
aUseDescription
- whether or not a file icon description should be used when renderingvoid clearUseDescription()
setUseDescription(false)
, i.e. "don't use description when rendering".setUseDescription(boolean)
void forceUseDescription()
setUseDescription(true)
, i.e. "use description when rendering".setUseDescription(boolean)
boolean loadDefaultIconByContentType(String aContentType)
aContentType
- the content type (mime type) that decides what file icon to loadboolean loadDefaultIconByURI(String aURI)
This method tries to extract a file extension from the URI and use that extension to load the default file icon. If the URI doesn't end with a file extension, default icon loading will fail.
aURI
- the uri that decides what file icon to loadtrue
if a default file icon was loaded, false
if notboolean loadDefaultIconByFileExtension(String aFileExtension)
aFileExtension
- the file extension that decides what file icon to loadtrue
if a default file icon was loaded, false
if notboolean isDefaultIconLoaded()
true
if a default icon is loaded, false
if not.void clearDefaultIcon()
boolean loadIconByContentType(String aContentType)
aContentType
- the content type (mime type) that decides what file icon to loadtrue
if a file icon was loaded, false
if notboolean loadIconByURI(String aURI)
This method tries to extract a file extension from the URI and use that extension to load the icon. If the URI doesn't end with a file extension, icon loading will fail.
aURI
- the URI that decides what file icon to loadtrue
if a file icon was loaded, false
if notboolean loadIconByFileExtension(String aFileExtension)
aFileExtension
- the file extension that decides what file icon to loadtrue
if a file icon was loaded, false
if notboolean isIconLoaded()
true
if an icon is loaded, false
if not.void clearIcon()
void setFontClass(String aFontClass)
aFontClass
- the CSS class name(-s) expression. An empty string is equivalent to null
- no class attribute will be renderedvoid clearFontClass()
String render()
SiteVision - Portal and Content Management Made Easy
SiteVision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2018 SiteVision AB, all rights reserved.