/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.panels;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.ModuleSupport;
import de.odysseus.calyxo.panels.conf.PanelConfig;
import de.odysseus.calyxo.panels.conf.PanelsRootConfig;
/**
* Panels support class. There is one instance per module, which may be
* retreived via the <code>getInstance()</code> methods.
* <p/>
* The panels support provides lookup of top level panels by name as well as
* access to the panels context of the current request.
*
* @author Christoph Beck
*/
public class PanelsSupport {
/**
* Module scope key used to save global panels support.
*/
public static final String PANELS_SUPPORT_KEY = "de.odysseus.calyxo.panels";
/**
* Request scope key used to save the context stack
*/
private static final String PANELS_CONTEXT_KEY =
"de.odysseus.calyxo.panels.context";
/**
* Lookup panels support for module determined by specified request
*/
public static final PanelsSupport getInstance(HttpServletRequest request) {
ModuleContext context =
ModuleSupport.getInstance(request).getModuleContext(request);
return (PanelsSupport)context.getAttribute(PANELS_SUPPORT_KEY);
}
/**
* Convenience method.
* @see #getInstance(HttpServletRequest)
*/
public static final PanelsSupport getInstance(PageContext pageContext) {
return getInstance((HttpServletRequest)pageContext.getRequest());
}
/**
* Lookup panels support for specified module
*/
public static final PanelsSupport getInstance(ModuleContext context) {
return (PanelsSupport)context.getAttribute(PANELS_SUPPORT_KEY);
}
private PanelsRootConfig config;
/**
* Constructor.
* @param config the panel root configuration element
*/
PanelsSupport(PanelsRootConfig config) {
this.config = config;
}
/**
* Find toplevel panel element by name and locale
*/
public PanelConfig findPanelConfig(String name, Locale locale) {
return config.findPanelConfig(name, locale);
}
/**
* Get the panels context for the specified request. Optionally create
* and install a new instance if necessary.
* @param request the request we process
* @param locale desired locale (used for panel lookups)
* @return panels context for specified request.
*/
public PanelsContext getOrCreateContext(HttpServletRequest request, Locale locale) {
PanelsContext context =
(PanelsContext)request.getAttribute(PANELS_CONTEXT_KEY);
if (context == null) {
context = new PanelsContext(this, locale);
request.setAttribute(PANELS_CONTEXT_KEY, context);
}
return context;
}
/**
* Get the panels context for the specified request.
* @param request the request we process
* @return panels context for specified request.
*/
public final PanelsContext getContext(HttpServletRequest request) {
return (PanelsContext)request.getAttribute(PANELS_CONTEXT_KEY);
}
/**
* Convenience method.
* @see #getContext(HttpServletRequest)
*/
public final PanelsContext getContext(PageContext pageContext) {
return getContext((HttpServletRequest)pageContext.getRequest());
}
}