001    // Copyright 2008 Thiago H. de Paula Figueiredo
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package br.com.arsmachina.tapestrycrud.components;
016    
017    import org.apache.tapestry5.MarkupWriter;
018    import org.apache.tapestry5.annotations.BeforeRenderTemplate;
019    import org.apache.tapestry5.annotations.Parameter;
020    
021    /**
022     * Component that only shows a message if a given text is not null and not empty.
023     * It puts the message inside a <code>div</code> with class <code>t-crud-message</code>.
024     * One example can be found in
025     * <a href="http://ars-machina.svn.sourceforge.net/viewvc/ars-machina/example/trunk/src/main/webapp/project/EditProject.tml?view=markup"
026     *              >Ars Machina Project Example</a>.
027     * 
028     * @author Thiago H. de Paula Figueiredo
029     */
030    public class Message {
031    
032            /**
033             * Generated <code>&lt;div&gt;</code> CSS class. 
034             */
035            private static final String CSS_CLASS = "t-crud-message";
036            
037            /**
038             * Message to be shown.
039             */
040            @Parameter(required = true)
041            private String message;
042            
043            @BeforeRenderTemplate
044            public boolean render(MarkupWriter writer) {
045                    
046                    if (message != null && message.trim().length() > 0) {
047                            
048                            writer.element("div", "class", CSS_CLASS);
049                            
050                            writer.element("p");
051                            writer.write(message);
052                            writer.end(); // p
053                            
054                            writer.end(); // div
055                            
056                    }
057                    
058                    return false;
059                    
060            }
061            
062            /**
063             * Returns the value of the <code>message</code> property.
064             * 
065             * @return a {@link String}.
066             */
067            final public String getMessage() {
068                    return message;
069            }
070    
071            /**
072             * Changes the value of the <code>message</code> property.
073             * 
074             * @param message a {@link String}.
075             */
076            final public void setMessage(String message) {
077                    this.message = message;
078            }
079    
080    }