/*
 * 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) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.view;

import java.util.*;
import javax.servlet.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.servlet.RequestImpl;

public class PhotoUploader implements ContentJSP
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String title;
  private String action;
  private int action_type;
  private RequestInput rinput = null;
  private HashMap hidden_parms = new HashMap();
  private String photo_tag;
  private String label;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  public PhotoUploader(String title, String action, int action_type)
  {
    this.title = title;
    this.action = action;
    this.action_type = action_type;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Content
   *--------------------------------------------------------------------------------
   */

  public boolean needFrame()
  {
    return true;

  } // end needFrame

  public int getMenuSelector()
  {
    return MENU_SELECTOR_NOCHANGE;

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return title;

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentJSP
   *--------------------------------------------------------------------------------
   */

  public String getJSPName()
  {
    return "photo_upload.jsp";

  } // end getJSPName

  public void initialize(RequestInput req)
  {
    rinput = req;

  } // end initialize

  public void terminate(RequestInput req)
  {
    rinput = null;

  } // end terminate

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final String getPageTitle()
  {
    return title;

  } // end getPageTitle

  public final String getAction()
  {
    if (rinput==null)
      return action;
    HTMLRendering html = (HTMLRendering)(rinput.queryService(HTMLRendering.class));
    return html.formatURL(action,action_type);

  } // end getAction

  public final void addHiddenParameter(String name, String value)
  {
    if (rinput==null)
      hidden_parms.put(name,value);

  } // end addHiddenParameter

  public final String getHiddenParams()
  {
    StringBuffer buf = new StringBuffer();
    Iterator it = hidden_parms.entrySet().iterator();
    while (it.hasNext())
    { // build the hidden parameters list
      Map.Entry ntry = (Map.Entry)(it.next());
      buf.append("<INPUT TYPE=\"HIDDEN\" NAME=\"").append(ntry.getKey()).append("\" VALUE=\"");
      buf.append(ntry.getValue()).append("\">\n");

    } // end while

    return buf.toString();

  } // end getHiddenParams

  public final String getPhotoTag()
  {
    return photo_tag;

  } // end getPhotoTag

  public final void setPhotoTag(String s)
  {
    if (rinput==null)
      photo_tag = s;

  } // end setPhotoTag

  public final String getLabel()
  {
    return label;

  } // end getLabel

  public final void setLabel(String s)
  {
    if (rinput==null)
      label = s;

  } // end setLabel

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  public static final PhotoUploader get(ServletRequest sreq) throws ServletException
  {
    Object obj = sreq.getAttribute(RequestImpl.REQUEST_CONTENT);
    if (obj==null)
      throw new ServletException("PhotoUploader.get: unable to get request content");
    if (obj instanceof PhotoUploader)
      return (PhotoUploader)obj;
    throw new ServletException("PhotoUploader.get: request content is not a PhotoUploader object");

  } // end get

} // end class PhotoUploader
