Class WebResourceRegistry
Registry for a set of web resources. Usually there is one registry per view. Used to specify things like css files, javascript files etc that should be loaded by the page. These can be added with a specific order.
Web resources are divided into separate named buckets. A single bucket usually corresponds with
a location in a layout page, for example "the resources that should be added inside the <head> of a page".
As of version 3.2.0 the functionality of web resources and the registry has been thoroughly
reworked for more flexibility.
A single web resource is represented by a WebResourceReference, the WebResource class itself is
deprecated. The rendering of a resource is determined by the ViewElementBuilder attached to the reference.
Default implementations for CSS, Javascript and META tags are available and can be created using the factory
methods WebResource.css(), WebResource.javascript() or WebResource.meta().
In practice the easiest way to add web resources is by configuring them as rules to apply to the registry: *
webResourceRegistry.apply(
WebResourceRule.add( WebResource.css( "@static:/MODULE_RESORCES/css/bootstrap.min.css" ) ).withKey( "bootstrap-min-css" ).toBucket( CSS ),
WebResourceRule.add( WebResource.javascript( "bootstrap.min.js" ) ).withKey( "bootstrap-min-js" ).toBucket( JAVASCRIPT_PAGE_END ),
WebResourceRule.add( WebResource.css().inline( "body {background-color: powderblue;}" ) ).withKey( "inline-body-blue" ).toBucket( CSS )
);
A web resource can optionally be registered with a key. This is a String that identifies the resource in its bucket.
Alternatively the ViewElementBuilder can implement WebResourceKeyProvider to provide a default key.
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(@NonNull WebResource webResource) Deprecated.voidDeprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)voidDeprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)voidaddPackage(@NonNull String... packageNames) Installs all resources attached to the packages with the names specified.booleanaddResourceToBucket(@NonNull WebResourceReference webResourceReference, @NonNull String bucket) Adds a specificWebResourceReferenceto the specified bucket, creating the bucket if it does not exist.booleanaddResourceToBucket(@NonNull WebResourceReference webResourceReference, @NonNull String bucket, boolean replaceIfExists) Add aWebResourceReferenceto the specified bucket, creating the bucket if it does not exist.voidaddWithKey(String type, String key, Object data) Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)voidaddWithKey(String type, String key, Object data, String location) Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)voidapply(@NonNull WebResourceRule... webResourceRules) Will apply the set ofWebResourceRuleitems to the registry.voidapply(@NonNull Collection<WebResourceRule> webResourceRules) Will apply the set ofWebResourceRuleitems to the registry.voidclear()Clears the entire registry, for all buckets.voidRemoves all resources in the given bucket.findResourceWithKeyInBucket(@NonNull String key, @NonNull String bucket) Find the resource reference registered under a specific key in a bucket.Return all bucket names in this registry.Deprecated.since 3.2.0Deprecated.since 3.2.0 - replaced bygetResourcesForBucket(String)getResources(String type) Deprecated.since 3.2.0 - replaced bygetResourcesForBucket(String)getResourcesForBucket(@NonNull String bucket) Return aWebResourceReferenceCollectionfrom all resources in this registry for a specific bucket.voidmerge(@NonNull WebResourceRegistry registry) Merges all resources of the other registry in this one.voidremovePackage(@NonNull String... packageNames) Deprecated.since 3.2.0 - might be removed in a future release as behaviour is hard to get consistentvoidremoveResource(Object data) Deprecated.since 3.2.0 - removing by "data" is not supported anymorevoidremoveResource(String type, Object data) Deprecated.since 3.2.0 - removing by "data" is not supported anymorevoidremoveResourceWithKey(@NonNull String key) Will remove all resources registered under the key specified.removeResourceWithKeyFromBucket(@NonNull String key, @NonNull String bucket) Will remove the resource with a specific key from the bucketvoidsetDefaultLocation(String defaultLocation) Deprecated.since 3.2.0 - it's advised to always specify a bucket name
-
Constructor Details
-
WebResourceRegistry
-
-
Method Details
-
getDefaultLocation
Deprecated.since 3.2.0- Returns:
- The default location resources will be registered with.
- See Also:
-
setDefaultLocation
Deprecated.since 3.2.0 - it's advised to always specify a bucket name- Parameters:
defaultLocation- Default location to set.- See Also:
-
add
Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)Register a specific resource.- Parameters:
webResource- WebResource instance to add.
-
add
Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)Register a new resource with the default location. Since there is no key, any other resource of the same type with the same data will be replaced.- Parameters:
type- Type of the resource, seeWebResourcefor constants.data- Data to register.
-
add
Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)Registers a resource with the location specified. Since there is no key, any other resource of the same type with the same data will be replaced.- Parameters:
type- Type of the resource, seeWebResourcefor constants.data- Data to register.location- Where the data is available.
-
addWithKey
Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)Registers a resource under the given key. For complex interactions, it is often better to provide a key. Existing resources of this type with the same key will be replaced.- Parameters:
type- Type of the resource, seeWebResourcefor constants.key- Unique key under which to register a resource.data- Data to register.
-
addWithKey
Deprecated.since 3.2.0 - replaced byWebResourceRule.add(ViewElementBuilder)Registers a resource under the given key. For complex interactions, it is often better to provide a key. Existing resources of this type with the same key will be replaced.- Parameters:
type- Type of the resource, seeWebResourcefor constants.key- Unique key under which to register a resource.data- Data to register.
-
removeResource
Deprecated.since 3.2.0 - removing by "data" is not supported anymoreWill remove all registered resources with the given content. Requires that the resource data equals() the requested data.- Parameters:
data- Content the resource should have.
-
removeResource
Deprecated.since 3.2.0 - removing by "data" is not supported anymoreWill remove all registered resources of that type with the given content.- Parameters:
type- Type of the resource, seeWebResourcefor constants.data- Content the resource should have.
-
removeResourceWithKey
Will remove all resources registered under the key specified.- Parameters:
key- Key the resource is registered under.
-
removeResourceWithKeyFromBucket
public Optional<WebResourceReference> removeResourceWithKeyFromBucket(@NonNull @NonNull String key, @NonNull @NonNull String bucket) Will remove the resource with a specific key from the bucket- Parameters:
key- Key the resource is registered under.bucket- Bucket name, seeWebResourcefor constants.- Returns:
- reference that was removed
-
addPackage
Installs all resources attached to the packages with the names specified. This requires the packages to be registered in the attachedWebResourcePackageManager.Note that a package will only be installed the first time, if it has been installed previously, it will be skipped.
- Parameters:
packageNames- Names of the packages to install.
-
removePackage
Deprecated.since 3.2.0 - might be removed in a future release as behaviour is hard to get consistentWill call theWebResourcePackage.uninstall(WebResourceRegistry)methods of the packages specified, if they are installed in the current registry. Note that the uninstalling should be used with care as it is hard to predict the exact behaviour.- Parameters:
packageNames- Names of the packages.
-
clear
public void clear()Clears the entire registry, for all buckets. -
clear
Removes all resources in the given bucket.- Parameters:
bucket- Name of the bucket.
-
getResources
Deprecated.since 3.2.0 - replaced bygetResourcesForBucket(String)Lists all resources for a given type.- Parameters:
type- Type of the resource.- Returns:
- Collection of WebResource instances.
-
getResources
Deprecated.since 3.2.0 - replaced bygetResourcesForBucket(String)Lists all resources in this registry.- Returns:
- Collection of WebResource instances.
-
getBuckets
Return all bucket names in this registry. -
getResourcesForBucket
Return aWebResourceReferenceCollectionfrom all resources in this registry for a specific bucket.- Parameters:
bucket- The bucket name.
-
merge
Merges all resources of the other registry in this one.- Parameters:
registry- Registry containing resource to be copied.
-
apply
Will apply the set ofWebResourceRuleitems to the registry. -
apply
Will apply the set ofWebResourceRuleitems to the registry. -
addResourceToBucket
public boolean addResourceToBucket(@NonNull @NonNull WebResourceReference webResourceReference, @NonNull @NonNull String bucket) Adds a specificWebResourceReferenceto the specified bucket, creating the bucket if it does not exist. If there is already a reference with that key in the bucket, it will be replaced, seeaddResourceToBucket(WebResourceReference, String, boolean)if you want to control this behaviour. -
addResourceToBucket
public boolean addResourceToBucket(@NonNull @NonNull WebResourceReference webResourceReference, @NonNull @NonNull String bucket, boolean replaceIfExists) Add aWebResourceReferenceto the specified bucket, creating the bucket if it does not exist. The value ofreplaceIfExistswill determine if a resource should be replaced if it is already present in the bucket. Replacing a resource will remove the original and put the new value in exactly the same spot. IfreplaceIfExistsisfalsebut a resource already has this key, the new resource will not be added at all andfalsewill be returned.- Parameters:
webResourceReference- resource to addbucket- to which to add the resourcereplaceIfExists- true if the resource should be replaced- Returns:
- true if resource was added or replaced, false if it already existed and has not been replaced
-
findResourceWithKeyInBucket
public Optional<WebResourceReference> findResourceWithKeyInBucket(@NonNull @NonNull String key, @NonNull @NonNull String bucket) Find the resource reference registered under a specific key in a bucket.- Parameters:
key- of the resourcebucket- name- Returns:
- reference
-
WebResourceRule.add(ViewElementBuilder)