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;
020    
021    import com.thoughtworks.xstream.XStream;
022    import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
023    import static echopoint.internal.AbstractContainer.ACTION_LISTENERS_CHANGED_PROPERTY;
024    import static echopoint.internal.AbstractContainer.INPUT_ACTION;
025    import echopoint.internal.AbstractContainerPeer;
026    import echopoint.internal.DefaultEventPeer;
027    import echopoint.model.Tag;
028    import nextapp.echo.app.Component;
029    import nextapp.echo.app.util.Context;
030    import nextapp.echo.webcontainer.ServerMessage;
031    import nextapp.echo.webcontainer.Service;
032    import nextapp.echo.webcontainer.WebContainerServlet;
033    import nextapp.echo.webcontainer.service.JavaScriptService;
034    
035    /**
036     * Component rendering peer for {@link echopoint.TagCloud}.
037     *
038     * @author Rakesh 2008-07-20
039     * @version $Id: TagCloudPeer.java 120 2009-02-20 15:43:33Z sptrakesh $
040     */
041    public class TagCloudPeer extends AbstractContainerPeer
042    {
043      /** The component name for which this class is a peer. */
044      private static final String COMPONENT_NAME = TagCloud.class.getName();
045    
046      /** The serialiser used to serialise {@link echopoint.model.Tag} instances. */
047      protected static final XStream xstream;
048    
049      /** The JS service files to load. */
050      private static final String[] SERVICE_FILES =
051          {
052              "resource/js/Application.TagCloud.js",
053              "resource/js/Sync.TagCloud.js"
054          };
055    
056      /** The service for the client side peer for this component. */
057      private static final Service COMPONENT_SERVICE =
058          JavaScriptService.forResources( COMPONENT_NAME, SERVICE_FILES );
059    
060      /** Register the services and resources. */
061      static
062      {
063        WebContainerServlet.getServiceRegistry().add( COMPONENT_SERVICE );
064    
065        xstream = new XStream( new JsonHierarchicalStreamDriver() );
066        xstream.processAnnotations( Tag.class );
067        xstream.setMode( XStream.NO_REFERENCES );
068      }
069    
070      /** Register an event peer for client events. */
071      public TagCloudPeer()
072      {
073        addEvent( new DefaultEventPeer(
074            INPUT_ACTION, ACTION_LISTENERS_CHANGED_PROPERTY, String.class ) );
075      }
076    
077      /**
078       * {@inheritDoc}
079       * @see nextapp.echo.webcontainer.AbstractComponentSynchronizePeer#init
080       */
081      @Override
082      public void init( final Context context, final Component component )
083      {
084        super.init( context, component );
085        final ServerMessage serverMessage =
086            (ServerMessage) context.get( ServerMessage.class );
087        serverMessage.addLibrary( COMPONENT_NAME );
088      }
089    
090      /**
091       * Over-ridden to handle requests for the {@link
092       * echopoint.TagCloud#PROPERTY_TAGS} property.  The collection of tag
093       * instances are serialised as a JSON stucture.
094       *
095       * @see nextapp.echo.webcontainer.ComponentSynchronizePeer#getOutputProperty(Context,
096       *      Component, String, int)
097       */
098      @Override
099      public Object getOutputProperty( final Context context,
100          final Component component, final String propertyName,
101          final int propertyIndex )
102      {
103        if ( TagCloud.PROPERTY_TAGS.equals( propertyName ) )
104        {
105          return xstream.toXML( ( (TagCloud) component ).getData() );
106        }
107    
108        return super.getOutputProperty(
109            context, component, propertyName, propertyIndex );
110      }
111    
112      /**
113       * {@inheritDoc}
114       * @see nextapp.echo.webcontainer.AbstractComponentSynchronizePeer#getComponentClass
115       */
116      public Class getComponentClass()
117      {
118        return TagCloud.class;
119      }
120    
121      /**
122       * {@inheritDoc}
123       * @see nextapp.echo.webcontainer.AbstractComponentSynchronizePeer#getClientComponentType
124       */
125      public String getClientComponentType( final boolean shortType )
126      {
127        return COMPONENT_NAME;
128      }
129    }