org.apache.juneau.html.HtmlRender Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.html;
import org.apache.juneau.html.annotation.*;
import org.apache.juneau.serializer.*;
/**
* Allows custom rendering of bean property values when serialized as HTML.
*
*
* Associated with bean properties using the {@link Html#render() @Html.render()} annotation.
*
*
* Using this class, you can alter the CSS style and HTML content of the bean property.
*
*
* The following example shows two render classes that customize the appearance of the pctFull
and
* status
columns shown below:
*
*
*
*
*
* // Our bean class
* public class FileSpace {
*
* private final String drive ;
* private final long total , available ;
*
* public FileSpace(String drive, long total, long available) {
* this .drive = drive;
* this .total = total;
* this .available = available;
* }
*
* @Html (link="drive/{drive}" )
* public String getDrive() {
* return drive ;
* }
*
* public long getTotal() {
* return total ;
* }
*
* public long getAvailable() {
* return available ;
* }
*
* @Html (render=FileSpacePctRender.class )
* public float getPctFull() {
* return ((100 * available ) / total );
* }
*
* @Html (render=FileSpaceStatusRender.class )
* public FileSpaceStatus getStatus() {
* float pf = getPctFull();
* if (pf < 80)
* return FileSpaceStatus.OK ;
* if (pf < 90)
* return FileSpaceStatus.WARNING ;
* return FileSpaceStatus.SEVERE ;
* }
* }
*
* // Possible values for the getStatus() method
* public static enum FileSpaceStatus {
* OK , WARNING , SEVERE ;
* }
*
* // Custom render for getPctFull() method
* public static class FileSpacePctRender extends HtmlRender<Float> {
*
* @Override
* public String getStyle(SerializerSession session, Float value) {
* if (value < 80)
* return "background-color:lightgreen;text-align:center" ;
* if (value < 90)
* return "background-color:yellow;text-align:center" ;
* return "background-color:red;text-align:center;border:;animation:color_change 0.5s infinite alternate" ;
* }
*
* @Override
* public Object getContent(SerializerSession session, Float value) {
* if (value >= 90)
* return div (
* String.format ("%.0f%%" , value),
* style ("@keyframes color_change { from { background-color: red; } to { background-color: yellow; }" )
* );
* return String.format ("%.0f%%" , value);
* }
* }
*
* // Custom render for getStatus() method
* public static class FileSpaceStatusRender extends HtmlRender<FileSpaceStatus> {
*
* @Override
* public String getStyle(SerializerSession session, FileSpaceStatus value) {
* return "text-align:center" ;
* }
*
* @Override
* public Object getContent(SerializerSession session, FileSpaceStatus value) {
* switch (value) {
* case OK : return img ().src(URI.create ("servlet:/htdocs/ok.png" ));
* case WARNING : return img ().src(URI.create ("servlet:/htdocs/warning.png" ));
* default : return img ().src(URI.create ("servlet:/htdocs/severe.png" ));
* }
* }
* }
*
*
* @param The bean property type.
*/
public abstract class HtmlRender {
/**
* Returns the CSS style of the element containing the bean property value.
*
* @param session
* The current serializer session.
* Can be used to retrieve properties and session-level information.
* @param value The bean property value.
* @return The CSS style string, or null if no style should be added.
*/
public String getStyle(SerializerSession session, T value) {
return null;
}
/**
* Returns the delegate value for the specified bean property value.
*
*
* The default implementation simply returns the same value.
* A typical use is to return an HTML element using one of the HTML5 DOM beans.
*
* @param session
* The current serializer session.
* Can be used to retrieve properties and session-level information.
* @param value The bean property value.
* @return The new bean property value.
*/
public Object getContent(SerializerSession session, T value) {
return value;
}
}