/*
* Copyright (c) 2009-2012 Lockheed Martin Corporation
*
* Licensed 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.eurekastreams.web.client.ui.common.stream.renderers.verb;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eurekastreams.server.domain.EntityType;
import org.eurekastreams.server.domain.stream.ActivityDTO;
import org.eurekastreams.server.domain.stream.BaseObjectType;
import org.eurekastreams.server.domain.stream.StreamEntityDTO;
import org.eurekastreams.web.client.ui.common.stream.renderers.AvatarRenderer;
import org.eurekastreams.web.client.ui.common.stream.renderers.RenderUtilities;
import org.eurekastreams.web.client.ui.common.stream.renderers.ResourceDestinationRenderer;
import org.eurekastreams.web.client.ui.common.stream.renderers.StatefulRenderer;
import org.eurekastreams.web.client.ui.common.stream.renderers.StreamMessageItemRenderer;
import org.eurekastreams.web.client.ui.common.stream.renderers.StreamMessageItemRenderer.State;
import org.eurekastreams.web.client.ui.common.stream.renderers.object.ObjectRenderer;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* Render a share verb activity.
*
*/
public class ShareRenderer implements VerbRenderer
{
/**
* The object dictionary.
*/
private Map<BaseObjectType, ObjectRenderer> objectRendererDictionary;
/**
* The activity.
*/
private ActivityDTO activity;
/**
* The state.
*/
private StreamMessageItemRenderer.State state;
/**
* Whether or not to show the recipient.
*/
boolean showRecipient;
/**
* Setup.
*
* @param inObjectRendererDictionary
* object dictionary.
* @param inActivity
* the activity.
* @param inState
* the state of the activity.
* @param inShowRecipient
* the recipient.
*/
public void setup(final Map<BaseObjectType, ObjectRenderer> inObjectRendererDictionary,
final ActivityDTO inActivity, final StreamMessageItemRenderer.State inState, final boolean inShowRecipient)
{
objectRendererDictionary = inObjectRendererDictionary;
activity = inActivity;
state = inState;
showRecipient = inShowRecipient;
}
/**
* Should the verb allow commenting.
*
* @return the value.
*/
public boolean getAllowComment()
{
return state.equals(State.DEFAULT);
}
/**
* Should the verb allow sharing.
*
* @return the value.
*/
public boolean getAllowShare()
{
return state.equals(State.DEFAULT);
}
/**
* Should the verb allow starring.
*
* @return the value.
*/
public boolean getAllowStar()
{
return state.equals(State.DEFAULT);
}
/**
* Should the verb allow liking.
*
* @return the value.
*/
public boolean getAllowLike()
{
return state.equals(State.DEFAULT);
}
/**
* Get the avatar.
*
* @return the avatar.
*/
public Widget getAvatar()
{
if (activity.getOriginalActor() != null)
{
return new AvatarRenderer().render(activity.getOriginalActor());
}
else
{
return new AvatarRenderer().render(activity.getActor());
}
}
/**
* Get the content.
*
* @return the content.
*/
public Widget getContent()
{
Widget content = objectRendererDictionary.get(activity.getBaseObjectType()).getContentWidget(activity);
Widget attachment = objectRendererDictionary.get(activity.getBaseObjectType()).getAttachmentWidget(activity);
FlowPanel contentPanel = new FlowPanel();
if (content != null)
{
contentPanel.add(content);
}
if (attachment != null)
{
contentPanel.add(attachment);
}
return contentPanel;
}
/**
* Return a list of appropriate metadata link renderers.
*
* @return the list.
*/
public List<StatefulRenderer> getMetaDataItemRenderers()
{
return Collections.emptyList();
}
/**
* {@inheritDoc}
*/
public List<StatefulRenderer> getSourceMetaDataItemRenderers()
{
List<StatefulRenderer> renderers = new LinkedList<StatefulRenderer>();
// A lot can go wrong here.
// If it fails, fall back to the normal renderer.
try
{
RenderUtilities.addEntityNameRenderers(renderers, activity.getOriginalActor(), null);
if (activity.getActor().getEntityId() != activity.getOriginalActor().getEntityId())
{
RenderUtilities.addEntityNameRenderers(renderers, activity.getActor(), "via");
}
}
catch (Exception e)
{
RenderUtilities.addEntityNameRenderers(renderers, activity.getActor(), null);
}
if (showRecipient)
{
StreamEntityDTO stream = activity.getDestinationStream();
if (stream.getType() == EntityType.RESOURCE)
{
renderers.add(new ResourceDestinationRenderer(activity));
}
else
{
RenderUtilities.addEntityNameRenderers(renderers, stream, "to");
}
}
return renderers;
}
}