
com.adobe.granite.ui.components.LayoutBuilder Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2012 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.granite.ui.components;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.jcr.resource.JcrResourceConstants;
/**
* A builder to create a layout. A layout is a configuration map consisting a set of properties. A layout at very least
* requires a name
property.
*/
public class LayoutBuilder {
private static final String NAME = "name";
private JSONObject result;
private String resourceType;
private ValueMap vm;
/**
* Builds a layout from the given config. The actual layout resource is retrieved based on {@link Config#LAYOUT}
* path. This method doesn't set default resource type of layout, use {@link #from(Config, String)} or
* {@link ComponentHelper#getLayout()} instead.
* @param config the config
* @return the layout builder
*/
public static LayoutBuilder from(Config config) {
return LayoutBuilder.from(config.getChild(Config.LAYOUT));
}
/**
* Builds a layout from the given config. The actual layout resource is retrieved based on {@link Config#LAYOUT}
* path. The given defaultResourceType will be used when sling:resourceType property of the resource is not set.
* @param config the config
* @param defaultResourceType the default resource type
* @return the layout builder
*/
public static LayoutBuilder from(Config config, String defaultResourceType) {
return LayoutBuilder.from(config.getChild(Config.LAYOUT), defaultResourceType);
}
/**
* Builds a layout from the given resource. This method doesn't set default resource type of layout, use
* {@link #from(Resource, String)} or {@link ComponentHelper#getLayout()} instead.
* @param resource the resource
* @return the layout builder
*/
public static LayoutBuilder from(Resource resource) {
return from(resource, null);
}
/**
* Builds a layout from the given resource. The given defaultResourceType will be used when sling:resourceType
* property of the resource is not set.
* @param resource the resource
* @param defaultResourceType the default resource type
* @return the layout builder
*/
public static LayoutBuilder from(Resource resource, String defaultResourceType) {
LayoutBuilder b = new LayoutBuilder();
b.vm = resource != null ? resource.getValueMap() : new ValueMapDecorator(new HashMap());
b.setResourceType(b.vm.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, defaultResourceType));
return b;
}
private void init() {
if (result != null) return;
result = new JSONObject();
if (vm != null) {
add(vm);
}
}
/**
* true
if this layout has name. false
otherwise.
* @return {@code true} if this layout has a name, {@code false} otherwise
*/
public boolean hasName() {
init();
return result.has(NAME);
}
/**
* Returns the name of this layout.
* @return returns the name of this layout
*/
public String getName() {
init();
return result.optString(NAME, null);
}
/**
* Sets the name of this layout.
* @param name the name to set
*/
public void setName(String name) {
try {
init();
result.put(NAME, name);
} catch (JSONException impossible) {
throw new RuntimeException(impossible);
}
}
/**
* Returns the resource type of this layout. This will be used to point to the renderer of the layout using standard
* Sling mechanism.
* @return the resource type as a string
*/
public String getResourceType() {
return resourceType;
}
/**
* Sets the resource type of this layout.
* @param resourceType the resource type to set
*/
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
/**
* Adds the given key-value pair of property to this layout.
* @param key the property key
* @param value the property value
*/
public void add(String key, Object value) {
try {
init();
result.accumulate(key, value);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
/**
* Adds a map of properties to this layout. Entry with key having a namespace (e.g. jcr:primaryType) will be
* excluded.
* @param data the map of properties to add
*/
public void add(Map data) {
try {
init();
for (Entry e : data.entrySet()) {
String key = e.getKey();
if (key.indexOf(":") >= 0) continue;
result.accumulate(key, e.getValue());
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
/**
* Returns this layout as JSON.
* @return the layout as JSON
*/
public JSONObject toJSON() {
init();
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy