/*
 * 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@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.view;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import javax.servlet.ServletException;
import com.silverwrist.venice.core.UserContext;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;

public class SideBoxNestedView implements ContentDirect, ContentMetadata
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Content m_inner;         // the inner content object to be displayed
  private Content[] m_sideboxes;   // the sideboxes to be displayed
  private boolean m_configure;     // display the sidebox Configure button?

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public SideBoxNestedView(RequestInput ri, Content inner) throws AccessError, DataException
  {
    m_inner = inner;
    m_sideboxes = ri.getSideBoxes();
    m_configure = ri.getUser().isLoggedIn();

  } // end constructor

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

  public boolean needFrame()
  {
    return m_inner.needFrame();

  } // end needFrame

  public int getMenuSelector()
  {
    return m_inner.getMenuSelector();

  } // end getMenuSelector

  public String getPageTitle(RequestOutput ro)
  {
    return m_inner.getPageTitle(ro);

  } // end getPageTitle

  public String getPageQID()
  {
    return m_inner.getPageQID();

  } // end getPageQID

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentDirect
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));

    try
    { // Write out the start of the content structure.
      out.write("<table border=\"0\" align=\"center\" width=\"100%\" cellpadding=\"4\" cellspacing=\"0\">\n"
		+ "<tr valign=\"top\"><td align=\"left\">\n");

      // Write out the inner content.
      out.output(m_inner);

      out.write("</td>\n<td align=\"center\" width=\"210\"><table border=\"0\" cellpadding=\"0\" "
		+ "cellspacing=\"0\">\n");  // break to the sidebox column

      for (int i=0; i<m_sideboxes.length; i++)
      { // draw in the outer framework of the current sidebox
	out.write("<tr valign=\"top\"><td align=\"center\"><table align=\"center\" width=\"200\" border=\"0\" "
		  + "cellpadding=\"2\" cellspacing=\"0\">\n<tr valign=\"middle\" bgcolor=\""
		  + html.getColor(html.SIDEBOX_TITLE_BACKGROUND) + "\"><td align=\"left\" class=\"sideboxtop\">"
		  + html.getFontTag(html.SIDEBOX_TITLE_FOREGROUND,"sidebox-title") + "<b>"
		  + m_sideboxes[i].getPageTitle(out) + "</b></font></td></tr>\n<tr valign=\"top\" bgcolor=\""
		  + html.getColor(html.SIDEBOX_CONTENT_BACKGROUND) + "\"><td align=\"left\" class=\"sidebox\">\n");

	out.output(m_sideboxes[i]);    // output the sidebox content

	// close up the framework of this sidebox
	out.write("</td></tr></table></td></tr><tr><td>&nbsp;</td></tr>\n");

      } // end for

      if (m_configure) // write the Configure button below the sideboxes
	out.write("<tr valign=\"middle\"><td align=\"center\"><a href=\""
		  + html.formatURL("usr/sidebox.js.vs",html.SERVLET) + "\">" + html.getButtonVisual("configure")
		  + "</a></td></tr>\n");

      // Finish up.
      out.write("</table></td>\n</tr></table>");

    } // end try
    catch (ServletException se)
    { // have to remap ServletException 'cos it can't be thrown
      throw new IOException("Error rendering page component: " + se.getMessage());

    } // end catch

  } // end render

  /*--------------------------------------------------------------------------------
   * Implementations from interface ContentMetadata
   *--------------------------------------------------------------------------------
   */

  public Map getMetadata()
  {
    if (m_inner instanceof ContentMetadata)
      return ((ContentMetadata)m_inner).getMetadata();
    else
      return Collections.EMPTY_MAP;

  } // end getMetadata

  public Map getHTTPMetadata()
  {
    if (m_inner instanceof ContentMetadata)
      return ((ContentMetadata)m_inner).getHTTPMetadata();
    else
      return Collections.EMPTY_MAP;

  } // end getHTTPMetadata

} // end class SideBoxNestedView
