View Javadoc

1   package org.tinyjee.maven.dim.extensions;
2   
3   import org.tinyjee.maven.dim.spi.RequestParameterTransformer;
4   
5   import java.util.Map;
6   
7   import static org.tinyjee.maven.dim.IncludeMacroSignature.*;
8   
9   /**
10   * The message box extension is implemented as a {@link RequestParameterTransformer} that is triggered when the parameter
11   * "<code>message-box</code>" is set with the macro call. When "<code>message-box=text to display</code>" is present, the transformer
12   * sets "<code>source</code>" to a bundled velocity template that performs the actual rendering.
13   * <p/>
14   * <b>Note:</b> Message boxes are rendered as pure <i>HTML</i> and will only work if a XhtmlSink or derived class
15   * is used for rendering the output (=> any HTML render target should be fine, including the standard Maven site generation).
16   * Furthermore all built-in icons are rendered using embedded <i>SVG</i> graphics, which is currently supported in:
17   * IE 9+, FF 4+, Chrome, Safari and Opera.
18   *
19   * @author Juergen_Kellerer, 2011-10-06
20   */
21  public class MessageBoxParameterTransformer implements RequestParameterTransformer {
22  
23  	/**
24  	 * Enables this extension and sets the text to display inside the message box.
25  	 * <p/>
26  	 * <b>Note:</b> Setting this property has no effect in case of "<code>source</code>" or "<code>source-class</code>" were
27  	 * specified with the macro call. This behaviour ensures that the parameter "<code>message-box</code>" can still be used
28  	 * with ordinary macro calls where a source or source-class was set.
29  	 * <p/>
30  	 * <b>Markup:</b> The message box is rendered as pure HTML and text is added to the box <b>as-is</b>.
31  	 * As a consequence HTML can be used as markup inside the given message and it is also the only valid
32  	 * markup that may be used here.
33  	 * <br/>
34  	 * E.g. <code>%{include|message-box=Hello &lt;b&gt;World!&lt;/b&gt;}</code> can be used with the APT format,
35  	 * to have "<b>World!</b>" formatted in strong text style.
36  	 */
37  	public static final String PARAM_MESSAGE_BOX = "message-box";
38  
39  	/**
40  	 * <i>Optional parameter</i> specifying the type of message box to display.
41  	 * <ul>
42  	 * <li><b>info</b>: displays a message box with an information icon (= default).</li>
43  	 * <li><b>warning</b>: displays a message box showing the given warning with a corresponding icon.</li>
44  	 * <li><b>alert</b>: displays a message box showing the given alert with a corresponding icon.</li>
45  	 * </ul>
46  	 */
47  	public static final String PARAM_TYPE = "type";
48  
49  	/**
50  	 * <i>Optional parameter</i> overriding the text color of the message box.
51  	 */
52  	public static final String PARAM_TEXT_COLOR = "text-color";
53  
54  	/**
55  	 * <i>Optional parameter</i> overriding the border color of the message box.
56  	 */
57  	public static final String PARAM_COLOR = "color";
58  
59  	/**
60  	 * <i>Optional parameter</i> overriding the color of the message box background.
61  	 */
62  	public static final String PARAM_BACKGROUND_COLOR = "background-color";
63  
64  	/**
65  	 * <i>Optional parameter</i> setting a site resource path to the image that is used with the message box instead of
66  	 * the built-in SVG graphics.
67  	 * <p/>
68  	 * Supported formats include GIF, PNG, JPG and SVG. The path has to be chosen relative to the site's web root, e.g.:<br/>
69  	 * <code>%{include|icon=images/alert.png|message-box=This is an alert message}</code>
70  	 * <p/>
71  	 * <i>Note:</i> If no <code>type</code> is specified, it will be set accordingly when the image path contains one of the
72  	 * keywords "<code>alert</code>" or "<code>warn(ing)</code>" ("info" remains the default message box type).
73  	 */
74  	public static final String PARAM_ICON = "icon";
75  
76  	private static volatile int idSequence;
77  
78  	private static boolean isSourceSet(Map<String, Object> requestParams) {
79  		return requestParams.containsKey(PARAM_SOURCE) && requestParams.containsKey(PARAM_SOURCE_CLASS);
80  	}
81  
82  	public void transformParameters(Map<String, Object> requestParams) {
83  		Object messageBoxText = requestParams.get(PARAM_MESSAGE_BOX);
84  		if (messageBoxText != null && !isSourceSet(requestParams)) {
85  			requestParams.put("idSequence", ++idSequence);
86  			requestParams.put(PARAM_SOURCE, "classpath:/templates/dim/message-box.html.vm");
87  			requestParams.put(PARAM_VERBATIM, false);
88  
89  			requestParams.put("text", messageBoxText);
90  
91  			if (requestParams.containsKey(PARAM_BACKGROUND_COLOR))
92  				requestParams.put("backgroundColor", requestParams.get(PARAM_BACKGROUND_COLOR));
93  			if (requestParams.containsKey(PARAM_TEXT_COLOR))
94  				requestParams.put("textColor", requestParams.get(PARAM_TEXT_COLOR));
95  		}
96  	}
97  }