Package dk.valtech.octools.widgets

Source Code of dk.valtech.octools.widgets.CustomValuesSelectWidget

/*

  This library is part of octools
  Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

 
  Alkacon OpenCms and the OpenCms logo are registered trademarks of
  Alkacon Software GmbH in Germany, the USA and other countries
 
  For further information about Alkacon Software GmbH, please see the
  company website: http://www.alkacon.com

  For further information about OpenCms, please see the
  project website: http://www.opencms.org
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
package dk.valtech.octools.widgets;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.widgets.CmsSelectWidget;
import org.opencms.widgets.CmsSelectWidgetOption;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetDialog;
import org.opencms.widgets.I_CmsWidgetParameter;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.content.CmsXmlContentValueSequence;

/**
* @author Stefan Uldum Grinsted (stefan.grinsted@valtech.dk)
*
* This widget generates a selectbox with values from a xml-content file from
* a specified location.
*
* The configuration string for this widget must only contain the full vfs-path
* to the xmlcontent file, that contains the options for the widget.
* The xmlcontent file must use the schema supplied with this module:
*  -  widgetoptions.xsd
*
* See the files in examples/ for a detailes examples of usage.
*
*/
public class CustomValuesSelectWidget extends CmsSelectWidget {
  private static final Log LOG = CmsLog.getLog(CustomValuesSelectWidget.class);
  static {
    LOG.info(CustomValuesSelectWidget.class + " loaded");
  }
 
  private static final String OPTION_ELEMENT = "Option";
  private static final String VALUE_ELEMENT = "Value";
  private static final String TEXT_ELEMENT = "Text";
 
  private CmsXmlContent xmlcontent;
  private Locale locale;
  private CmsObject cms;
 
  /**
   *
   */
  public CustomValuesSelectWidget() {
    super();
  }
 
  /**
   * @param configuration
   */
  public CustomValuesSelectWidget(String configuration) {
    super(configuration);
  }
 
 
  /* (non-Javadoc)
   * @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
   */
  protected List<CmsSelectWidgetOption> parseSelectOptions(CmsObject cms,
      I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
    locale = cms.getRequestContext().getLocale();
    this.cms = cms;
   
    LOG.debug("Entered parseSelectOptions()");
    LOG.debug("getConfiguration() contains: " + getConfiguration());
   
    List<CmsSelectWidgetOption> options = new ArrayList<CmsSelectWidgetOption>();
    try {
      // Load the xmlcontent containing the options
      CmsFile xmlfile = cms.readFile(cms.readResource(getConfiguration()));
      xmlcontent = CmsXmlContentFactory.unmarshal(cms, xmlfile);
     
      // loop through the options
      CmsXmlContentValueSequence seq = xmlcontent.getValueSequence("Option", locale);
      int count = seq.getElementCount();
      for (int i = 1; i <= count; i++) {
        String value = getValue(VALUE_ELEMENT, i);
        String text = getValue(TEXT_ELEMENT, i);
        CmsSelectWidgetOption option = new CmsSelectWidgetOption(value, false, text);
        options.add(option);
      }
    } catch (CmsException e) {
      CmsSelectWidgetOption option = new CmsSelectWidgetOption("error", true, "Error: Could not load the options from '" + getConfiguration() + "'");
      options.add(option);
    }
   
    LOG.debug("Returning: " + options.toString());
    return options;
  }
 
  public List<CmsSelectWidgetOption> getSelectOptions(CmsObject cms) {
    return parseSelectOptions(cms, null, null);
  }
 
  /**
   * Returns the value of the given elementName from the given number of a
   * sequence. If the element could not be found, null is returned.
   *
   * @param elementName
   *            the element name
   * @param number
   *            the seuence number of the OPTION_ELEMENT
   * @return the value of the the elementName if found, null otherwise.
   */
  private String getValue(String elementName, int number) {
    String value = null;
    String elementPath = OPTION_ELEMENT + "[" + number +"]/" + elementName;
    if (xmlcontent.hasValue(elementPath, locale)) {
      value = xmlcontent.getValue(elementPath, locale).getStringValue(cms);
    }
    return value;
  }
 
  /* (non-Javadoc)
   * @see org.opencms.widgets.CmsComboWidget#newInstance()
   */
  public I_CmsWidget newInstance() {
    return new CustomValuesSelectWidget(getConfiguration());
  }
}
TOP

Related Classes of dk.valtech.octools.widgets.CustomValuesSelectWidget

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.