/*
 * LookAndFeelSwitchingPageBroker.java
 *
 * Copyright © 1994-2004, Oracle. All rights reserved. Oracle grants
 * you ("Licensee") a non-exclusive, royalty-free, worldwide license to use,
 * modify and redistribute this software in source and binary code form, provided
 * that (i) this copyright notice and license appear on all copies of the
 * software; (ii) Licensee does not utilize the software in a manner disparaging
 * to Oracle; and (iii) Licensee does not utilize the software in a manner that
 * violates the terms of any Licensee agreement with Oracle.
 */

package samples.uix;

import oracle.cabo.servlet.BajaContext;
import oracle.cabo.servlet.Page;

import oracle.cabo.servlet.event.EventResult;
import oracle.cabo.servlet.event.PageEvent;

import oracle.cabo.servlet.xml.UIXPageBroker;

import javax.servlet.http.HttpSession;



public class LookAndFeelSwitchingPageBroker extends UIXPageBroker
{

    // Default constructor
    public LookAndFeelSwitchingPageBroker()
    {
      
    }
    
    // Retrieves the configuration name from the http session
    protected String getConfigurationName(BajaContext bajaContext, Page page)
    {
        HttpSession httpSession = bajaContext.getServletRequest().getSession(false);
        if (httpSession == null)
            return null;
        else
            return (String)httpSession.getAttribute(_LAF_NAME_PROPERTY);

    }

    protected boolean isCacheable(BajaContext bajacontext, Page page, Throwable throwable, PageEvent pageevent)
    {
        return true;
    }
    
    // Handles the switching of the look and feel whne the user submits the new choice
    public static EventResult setLookAndFeel(BajaContext bajaContext, Page page, PageEvent pageEvent)
    {
        String laf = pageEvent.getParameter("laf");
        if (laf != null)
        {
            HttpSession httpSession = bajaContext.getServletRequest().getSession(true);
            httpSession.setAttribute(_LAF_NAME_PROPERTY, laf);
        }
        return new EventResult(page);
    }

    // The name of the look and feel parameter
    private static final String _LAF_NAME_PROPERTY = "lookAndFeelName";
}
