/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.dialog;

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.dialog.BaseDialogField;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.iface.*;

class UserPhotoField extends BaseDialogField
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_photo_url = null;
  private String m_link_url;
  private String m_link_type;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  UserPhotoField(Element elt) throws DialogException
  {
    super(false,elt);
    XMLLoader loader = XMLLoader.get();
    try
    { // get link URL and link type
      m_link_url = loader.getAttribute(elt,"link");
      m_link_type = loader.getAttribute(elt,"type");

    } // end try
    catch (XMLLoadException e)
    { // translate to DialogException
      throw new DialogException(e);

    } // end catch

    // Load caption 2 from messages
    ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.venice.dialog.VeniceDialogMessages",
						Locale.getDefault());
    setCaption2(b.getString("userphoto.caption2"));

  } // end constructor

  protected UserPhotoField(UserPhotoField other)
  {
    super(other);
    m_photo_url = null;
    m_link_url = other.m_link_url;
    m_link_type = other.m_link_type;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Abstract implementations from class BaseDialogField
   *--------------------------------------------------------------------------------
   */

  protected void renderField(TextRenderControl control, Map render_params)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    if (isEnabled())
    { // write the opening <A> tag
      URLRewriter rewriter = (URLRewriter)(control.queryService(URLRewriter.class));
      wr.write("<a href=\""
	       + rewriter.rewriteURL(m_link_type,StringUtils.replaceAllVariables(m_link_url,render_params))
	       + "\">");

    } // end if

    // write the <IMG> tag by running it through the standard user photo renderer
    ObjectProvider oprov = (ObjectProvider)(control.queryService(ObjectProvider.class));
    ServiceProvider sp = (ServiceProvider)(oprov.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,
							   "venice-userphoto"));
    RenderImage rimg = (RenderImage)(sp.queryService(RenderImage.class));
    rimg.renderImageTag(control,m_photo_url);

    // write the </A> tag
    if (isEnabled())
      wr.write("</a>");

  } // end renderField

  protected void validateContents(Request r, Object data) throws ValidationException
  { // do nothing
  } // end validateContents

  public Object clone()
  {
    return new UserPhotoField(this);

  } // end clone

  public Object getValue()
  {
    return m_photo_url;

  } // end getValue

  public boolean containsValue()
  {
    return (m_photo_url!=null);

  } // end containsValue

  public void setValue(Object obj)
  {
    m_photo_url = ((obj==null) ? null : obj.toString());

  } // end setValue

  public void setValueFrom(Request r)
  { // do nothing - this doesn't get handled in the usual way
  } // end setValueFrom

  public void reset()
  { // do nothing
  } // end reset

  /*--------------------------------------------------------------------------------
   * Overrides from class BaseDialogField
   *--------------------------------------------------------------------------------
   */

  protected boolean isNull(Object value)
  {
    return false;

  } // end isNull

  public int getFlags()
  {
    return 0;

  } // end getFlags

} // end class UserPhotoField
