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

import java.io.IOException;
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
{
  private Content inner;
  private Content[] sideboxes;
  private boolean do_configure;

  public SideBoxNestedView(RequestInput ri, Content inner) throws AccessError, DataException
  {
    this.inner = inner;
    this.sideboxes = ri.getSideBoxes();
    this.do_configure = ri.getUser().isLoggedIn();

  } // end constructor

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

  } // end needFrame

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

  } // end getMenuSelector

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

  } // end getPageTitle

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

  } // end getPageQID

  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><TR VALIGN=TOP>\n"
		+ "<TD ALIGN=LEFT>\n");

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

      out.write("</TD>\n<TD ALIGN=CENTER WIDTH=210>\n");  // break to the sidebox column

      for (int i=0; i<sideboxes.length; i++)
      { // draw in the outer framework of the current sidebox
	out.write("<TABLE ALIGN=CENTER WIDTH=200 BORDER=0 CELLPADDING=2 CELLSPACING=0><TR VALIGN=MIDDLE "
		  + "BGCOLOR=\"" + html.getColor(html.SIDEBOX_TITLE_BACKGROUND)
		  + "\"><TD ALIGN=LEFT CLASS=\"sideboxtop\">\n"
		  + html.getFontTag(html.SIDEBOX_TITLE_FOREGROUND,"sidebox-title") + "<B>"
		  + sideboxes[i].getPageTitle(out) + "</B></FONT>\n</TD></TR><TR VALIGN=TOP BGCOLOR=\""
		  + html.getColor(html.SIDEBOX_CONTENT_BACKGROUND) + "\"><TD ALIGN=LEFT CLASS=\"sidebox\">\n");

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

	// close up the framework of this sidebox
	out.write("</TD></TR></TABLE><P>\n");

      } // end for

      if (do_configure) // write the Configure button below the sideboxes
	out.write("<A HREF=\"" + html.formatURL("usr/sidebox.js.vs",html.SERVLET) + "\">"
		  + html.getButtonVisual("configure") + "</A>\n");

      // Finish up.
      out.write("</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

} // end class SideBoxNestedView
