/*
* 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.conf.impl;
import java.util.Locale;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.panels.conf.ListConfig;
import de.odysseus.calyxo.panels.conf.PanelConfig;
import de.odysseus.calyxo.panels.conf.ParamConfig;
/**
* Panel configuration implementation.
* This implementation uses inheritance. A base (top level) panel name may
* be specified in the <code>super</code> attribute. A panels's inheritance
* path is locale-dependent, that is, the super panel has to be computed
* dynamically.
* Also, top level panels have a generalization path containing panel
* definitions for generalized locales.
*
* @author Christoph Beck
*/
public class PanelConfigImpl extends NamespaceConfigImpl implements PanelConfig {
private String name;
private String template;
private String super_;
private PanelsRootConfigImpl root;
private PanelConfigImpl nearestAncestorPanel;
private PanelConfigImpl generalizedPanel;
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
*/
protected String _getElementName() {
return "panel";
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
*/
protected void _putAttributes(Attributes map) {
super._putAttributes(map);
map.put("name", name);
map.put("template", template);
map.put("super", super_);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getDynamicAttributes()
*/
protected String[] _getDynamicAttributes() {
return new String[]{ "template" };
}
/**
* Initialize root element and nearest panel ancestor
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init()
*/
protected void _init() throws ConfigException {
super._init();
root = (PanelsRootConfigImpl)_getNearestAncestor(PanelsRootConfigImpl.class);
nearestAncestorPanel = (PanelConfigImpl)_getNearestAncestor(PanelConfigImpl.class);
}
/**
* Initialize base panel by locale
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init2()
*/
protected void _init2() throws ConfigException {
super._init2();
if (nearestAncestorPanel == null) { // top level
PanelsConfigImpl panels =
(PanelsConfigImpl)_getNearestAncestor(PanelsConfigImpl.class);
Locale generalizedLocale = root.generalize(panels.getLocale());
if (generalizedLocale != null) {
generalizedPanel =
(PanelConfigImpl)root.findPanelConfig(name, generalizedLocale);
}
}
}
/**
* Search receiver and generalizations for a non-null <code>super</code>
* property value
*/
String findSuperByGeneralization() {
if (super_ != null) {
return super_;
} else if (generalizedPanel != null) {
return generalizedPanel.findSuperByGeneralization();
}
return null;
}
/**
* Search receiver and generalizations for a non-null <code>template</code>
* property value
*/
String findTemplateByGeneralization() {
if (template != null) {
return template;
} else if (generalizedPanel != null) {
return generalizedPanel.findTemplateByGeneralization();
}
return null;
}
/**
* Search receiver and generalizations for a contained panel wit
* given name
*/
PanelConfig findPanelConfigByGeneralization(String name) {
PanelConfig panel = getPanelConfig(name);
if (panel == null && generalizedPanel != null) {
panel = generalizedPanel.findPanelConfigByGeneralization(name);
}
return panel;
}
/**
* Search receiver and generalizations for a contained param with
* given name
*/
ParamConfig findParamConfigByGeneralization(String name) {
ParamConfig param = getParamConfig(name);
if (param == null && generalizedPanel != null) {
param = generalizedPanel.findParamConfigByGeneralization(name);
}
return param;
}
/**
* Search receiver and generalizations for a contained list with
* given name
*/
ListConfig findListConfigByGeneralization(String name) {
ListConfig list = getListConfig(name);
if (list == null && generalizedPanel != null) {
list = generalizedPanel.findListConfigByGeneralization(name);
}
return list;
}
/**
* Search base panel.
* First, search for a panel's <code>super</code> property in the
* receiver and its generalizations according to the specified locale.
* If found, use it to lookup a toplevel panel with that name and the
* specified locale.
* Otherwise, get the nearest ancestor panel and (recursively)
* search its base panels (with their generalizations) for a panel
* component with the same name as the receiver.
* @param locale the desired locale
* @return the receiver's base panel according to specified locale
*/
PanelConfigImpl lookupBase(Locale locale) {
String base = findSuperByGeneralization();
if (base != null) {
return (PanelConfigImpl)root.findPanelConfig(base, locale);
}
if (nearestAncestorPanel != null) { // inner panel
PanelConfigImpl panel = nearestAncestorPanel.lookupBase(locale);
while (panel != null) {
PanelConfig result = panel.findPanelConfigByGeneralization(name);
if (result != null) {
return (PanelConfigImpl)result;
}
panel = panel.lookupBase(locale);
}
}
return null;
}
/**
* Get super panel element
*/
public String getSuper() {
return super_;
}
/**
* Set super panel element
*/
public void setSuper(String string) {
super_ = string;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.panels.conf.PanelConfig#getName()
*/
public String getName() {
return name;
}
/**
* Set panel name
*/
public void setName(String string) {
name = string;
}
/**
* Get template path
*/
public String getTemplate() {
return template;
}
/**
* Set template path
*/
public void setTemplate(String string) {
template = string;
}
/**
* Search by inheritance for a panel with <code>template</code>
* property set.
* @param locale desired locale
* @return the receiver's template path according to specified locale
*/
public String findTemplate(Locale locale) {
PanelConfigImpl panel = this;
while (panel != null) {
String path = panel.findTemplateByGeneralization();
if (path != null) {
return path;
}
panel = panel.lookupBase(locale);
}
return null;
}
/**
* Search nested list with specified name for given locale.
* For each panel <em>p</em> on the locale-dependent inheritance path
* search <em>p</em> and its generalizations for a list with specified name.
* @see de.odysseus.calyxo.panels.conf.PanelConfig#findListConfig(java.lang.String, java.util.Locale)
*/
public ListConfig findListConfig(String name, Locale locale) {
PanelConfigImpl panel = this;
while (panel != null) {
ListConfig result = panel.findListConfigByGeneralization(name);
if (result != null) {
return result;
}
panel = panel.lookupBase(locale);
}
return null;
}
/**
* Search nested panel with specified name for given locale.
* For each panel <em>p</em> on the locale-dependent inheritance path
* search <em>p</em> and its generalizations for a panel with specified name.
* @see de.odysseus.calyxo.panels.conf.PanelConfig#findPanelConfig(java.lang.String, java.util.Locale)
*/
public PanelConfig findPanelConfig(String name, Locale locale) {
PanelConfigImpl panel = this;
while (panel != null) {
PanelConfig result = panel.findPanelConfigByGeneralization(name);
if (result != null) {
return result;
}
panel = panel.lookupBase(locale);
}
return null;
}
/**
* Search nested list with specified name for given locale.
* For each panel <em>p</em> on the locale-dependent inheritance path
* search <em>p</em> and its generalizations for a param with specified name.
* @see de.odysseus.calyxo.panels.conf.PanelConfig#findParamConfig(java.lang.String, java.util.Locale)
*/
public ParamConfig findParamConfig(String name, Locale locale) {
PanelConfigImpl panel = this;
while (panel != null) {
ParamConfig result = panel.findParamConfigByGeneralization(name);
if (result != null) {
return result;
}
panel = panel.lookupBase(locale);
}
return null;
}
}