Package de.odysseus.calyxo.panels.conf.impl

Source Code of de.odysseus.calyxo.panels.conf.impl.PanelsConfigImpl

/*
* 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.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.impl.ConfigImpl;
import de.odysseus.calyxo.base.util.ListOrderedMap;
import de.odysseus.calyxo.panels.conf.PanelConfig;
import de.odysseus.calyxo.panels.conf.PanelsConfig;

/**
* Panels configuration implementation.
*
* @author Christoph Beck
*/
public class PanelsConfigImpl extends ConfigImpl implements PanelsConfig {
  private String language;
  private String country;
  private String variant;

  private Map panels = ListOrderedMap.decorate(new HashMap());

  private Locale locale;

  /**
   * Merge other panels into receiver.
   * Simply add all panels.
   */
  void merge(PanelsConfigImpl other) throws ConfigException {
    if (other == null) {
      return;
    }
    Iterator configs = other.getPanelConfigs();
    while (configs.hasNext()) {
      add((PanelConfigImpl)configs.next());
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
   */
  protected String _getElementName() {
    return "panels";
  }

  /*
   * (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("language", language);
    map.put("country", country);
    map.put("variant", variant);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_addChildren(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Elements)
   */
  protected void _addChildren(Elements list) {
    super._addChildren(list);
    list.add(getPanelConfigs());
  }

  /**
   * Initialize locale.
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init()
   */
  protected void _init() throws ConfigException {
    super._init();
    locale = createLocale();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.panels.conf.PanelsConfig#getPanelConfigs()
   */
  public Iterator getPanelConfigs() {
    return panels.values().iterator();
  }

  /**
   * Add panel element
   */
  public void add(PanelConfig value) throws ConfigException {
    if (panels.containsKey(value.getName())) {
      throw new ConfigException("Duplicate panel '" + value.getName() + "' in " + toInlineString());
    }
    panels.put(value.getName(), value);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.panels.conf.PanelsConfig#getPanelConfig(java.lang.String)
   */
  public PanelConfig getPanelConfig(String name) {
    return (PanelConfig)panels.get(name);
  }

  /**
   * Create locale from language, country and variant settings.
   * @throws ConfigException if settings are invalid
   */
  private Locale createLocale() throws ConfigException {
    if (language == null) {
      if (country != null || variant != null) {
        throw new ConfigException("Must not specify country or variant without language in '" + toInlineString() + "'");
      }
      return new Locale("", "");
    } else {
      if (country == null) {
        if (variant != null) {
          throw new ConfigException("Must not specify variant without country in '" + toInlineString() + "'");
        }
        return new Locale(language, "");
      } else {
        if (variant == null) {
          return new Locale(language, country)
        } else {
          return new Locale(language, country, variant);
        }
      }
    }
  }

  /**
   * Get language
   */
  public String getLanguage() {
    return language;
  }

  /**
   * Set language
   */
  public void setLanguage(String string) {
    language = string;
  }

  /**
   * Get country
   */
  public String getCountry() {
    return country;
  }

  /**
   * Set country
   */
  public void setCountry(String string) {
    country = string;
  }

  /**
   * Get variant
   */
  public String getVariant() {
    return variant;
  }

  /**
   * Set variant
   */
  public void setVariant(String string) {
    variant = string;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.panels.conf.PanelsConfig#getLocale()
   */
  public Locale getLocale() {
    return locale;
  }
}
TOP

Related Classes of de.odysseus.calyxo.panels.conf.impl.PanelsConfigImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.