001    /*
002     * This file is part of the Echo Point Project.  This project is a
003     * collection of Components that have extended the Echo Web Application
004     * Framework Version 3.
005     *
006     * Version: MPL 1.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     */
018    
019    package echopoint.internal;
020    
021    import nextapp.echo.app.Component;
022    
023    /**
024     * An abstract super-class for components that display raw HTML text.
025     *
026     * @author Rakesh 2008-03-22
027     * @version $Id: AbstractHtmlComponent.java 66 2008-09-09 08:13:37Z sptrakesh $
028     */
029    public abstract class AbstractHtmlComponent extends AbstractContainer
030    {
031      private static final long serialVersionUID = 1l;
032    
033      /** The property for specifying the content displayed in the component. */
034      public static final String PROPERTY_TEXT = "text";
035    
036      /** The property for specifying the target for anchor tags in the content. */
037      public static final String PROPERTY_TARGET = "target";
038    
039      /** Default constructor.  Create a new instance with empty text. */
040      public AbstractHtmlComponent() { this( "" ); }
041    
042      /**
043       * Create a new instance enclosing the specified HTML text.
044       *
045       * @param text The HTML text that is to be displayed in this component.
046       */
047      public AbstractHtmlComponent( final String text )
048      {
049        setText( text );
050      }
051    
052      /**
053       * Over-ridden to return <code>false</code> always as this component does
054       * not support child components.
055       *
056       * {@inheritDoc}
057       */
058      @Override
059      public boolean isValidChild( final Component component )
060      {
061        return false;
062      }
063    
064      /**
065       * A convenience method to append the given string to the end of the
066       * existing content of this component.  This method is safe to use even
067       * if no <code>text</code> has been associated with this component.
068       *
069       * @see #getText
070       * @see #setText
071       * @param text The additional text to add.  Note that no line breaks etc.
072       *   are added to the existing content.
073       */
074      public void append( final String text )
075      {
076        String content = getText();
077        if ( content == null ) content = "";
078        setText( content + text );
079      }
080    
081      /**
082       * Return the HTML text displayed within this component.
083       *
084       * @return The HTML text.
085       */
086      public String getText()
087      {
088        return (String) get( PROPERTY_TEXT );
089      }
090    
091      /**
092       * Set the value of the HTML displayed within this component.
093       *
094       * @param text The value to set.
095       */
096      public void setText( final String text )
097      {
098        set( PROPERTY_TEXT, text );
099      }
100    
101      /**
102       * Return the target attribute for anchor tags embedded in the content
103       * displayed in the component.
104       *
105       * @return The target value.
106       */
107      public String getTarget()
108      {
109        return (String) get( PROPERTY_TARGET );
110      }
111    
112      /**
113       * Set the value for the target attribute to be applied to all anchor tags
114       * embedded in the content displayed in the component.  Note that the
115       * implementation only adds a target attribute if no attribute value exists
116       * in the anchor tags.
117       *
118       * @param target The value to set.
119       */
120      public void setTarget( final String target )
121      {
122        set( PROPERTY_TARGET, target );
123      }
124    }