⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmswidgetdialogparameter.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsWidgetDialogParameter.java,v $
 * Date   : $Date: 2006/03/27 14:52:43 $
 * Version: $Revision: 1.13 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * 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.
 *
 * 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 org.opencms.workplace;

import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.A_CmsWidget;
import org.opencms.widgets.CmsWidgetException;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetParameter;
import org.opencms.widgets.Messages;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;

/**
 * Implements the widget parameter interface for the use of OpenCms widgets on dialogs that
 * are not based on XML contents.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.13 $ 
 * 
 * @since 6.0.0 
 */
public class CmsWidgetDialogParameter implements I_CmsWidgetParameter {

    /** The name of the default dialog page. */
    public static final String DEFAULT_DIALOG_PAGE = "default";

    /** The maximum number of occurences of a widget dialog element in a list of elements. */
    public static final int MAX_OCCURENCES = 200;

    /** The (optional) base collection for read / writing collection based parameters. */
    protected Object m_baseCollection;

    /** The (optional) base object for read / writing the parameter value to. */
    protected Object m_baseObject;

    /** The (optinal) object property to read / write this parameter value to. */
    protected String m_baseObjectProperty;

    /** The default value of the parameter. */
    protected String m_defaultValue;

    /** The name of the dialog (page) the widget is used on. */
    protected String m_dialogPage;

    /** Indicates if the widget value has an error. */
    protected Throwable m_error;

    /** The id of the parameter on the form. */
    protected String m_id;

    /** The index of this parameter in the (optional) list of parameters. */
    protected int m_index;

    /** The maximum number of occurences of this parameter. */
    protected int m_maxOccurs;

    /** The minimum number of occurences of this parameter. */
    protected int m_minOccurs;

    /** The name of the parameter. */
    protected String m_name;

    /** Optional localized key prefix identificator. */
    protected String m_prefix;

    /** The value of the parameter. */
    protected String m_value;

    /** The widget used for the parameter. */
    protected I_CmsWidget m_widget;

    /**
     * Create a new Widget parameter.<p>
     * 
     * @param base the base of the parameter
     * @param index the index of this parameter in the list 
     */
    public CmsWidgetDialogParameter(CmsWidgetDialogParameter base, int index) {

        this(
            null,
            base.m_defaultValue,
            base.getName(),
            base.getWidget(),
            base.getDialogPage(),
            base.getMinOccurs(),
            base.getMaxOccurs(),
            index);

        m_baseObject = base.m_baseObject;
        m_baseObjectProperty = base.m_baseObjectProperty;
        m_baseCollection = base.m_baseCollection;
        m_prefix = base.m_prefix;
    }

    /**
     * Create a new Widget parameter.<p>
     * 
     * @param base the base of the parameter
     * @param index the index of this parameter in the list
     * @param originalIndex the original index in the previous version of the list
     */
    public CmsWidgetDialogParameter(CmsWidgetDialogParameter base, int index, int originalIndex) {

        this(
            null,
            base.m_defaultValue,
            base.getName(),
            base.getWidget(),
            base.getDialogPage(),
            base.getMinOccurs(),
            base.getMaxOccurs(),
            index);

        m_baseObject = base.m_baseObject;
        m_baseObjectProperty = base.m_baseObjectProperty;
        m_baseCollection = base.m_baseCollection;

        if (m_baseCollection != null) {
            if (m_baseCollection instanceof List) {
                // base object is a list - make sure to set possible old value 
                List baseList = (List)m_baseCollection;
                if (originalIndex < baseList.size()) {
                    Object o = baseList.get(originalIndex);
                    if (o != null) {
                        m_value = o.toString();
                    }
                }
            } else if (m_baseCollection instanceof SortedMap) {
                // base object is a sorted map - make sure to set possible old value 
                SortedMap baseMap = (SortedMap)m_baseCollection;
                List keyList = new ArrayList(baseMap.keySet());
                if (originalIndex < keyList.size()) {
                    Object key = keyList.get(originalIndex);
                    Object value = baseMap.get(key);
                    StringBuffer val = new StringBuffer();
                    val.append(key != null ? key.toString() : "");
                    val.append('=');
                    val.append(value != null ? value.toString() : "");
                    m_value = val.toString();
                }
            }
        }
    }

    /**
     * Create a new Widget parameter based on a given object's property.<p>
     * 
     * @param base the base object to map the parameter to / from
     * @param property the base object property to map the parameter to / from
     * @param widget the widget used for this parameter
     */
    public CmsWidgetDialogParameter(Object base, String property, I_CmsWidget widget) {

        this(base, property, DEFAULT_DIALOG_PAGE, widget);
    }

    /**
     * Create a new Widget parameter based on a given object's property.<p>
     * 
     * @param base the base object to map the parameter to / from
     * @param property the base object property to map the parameter to / from
     * @param dialogPage the dialog page to use the widget on
     * @param widget the widget used for this parameter
     */
    public CmsWidgetDialogParameter(Object base, String property, String dialogPage, I_CmsWidget widget) {

        this(base, property, null, dialogPage, widget, 1, 1);
    }

    /**
     * Create a new Widget parameter based on a given object's property.<p>
     * 
     * @param base the base object to map the parameter to / from
     * @param property the base object property to map the parameter to / from
     * @param htmlName the form id name to use in the generated HTML
     * @param dialogPage the dialog page to use the widget on
     * @param widget the widget used for this parameter
     * 
     */
    public CmsWidgetDialogParameter(Object base, String property, String htmlName, String dialogPage, I_CmsWidget widget) {

        this(base, property, htmlName, null, dialogPage, widget, 1, 1);
    }

    /**
     * Create a new Widget parameter based on a given object's property.<p>
     * 
     * @param base the base object to map the parameter to / from
     * @param property the base object property to map the parameter to / from
     * @param defaultValue the default value to use for this parameter
     * @param dialogPage the dialog page to use the widget on
     * @param widget the widget used for this paramete
     * @param minOccurs the required minimum numer of occurences of this parameter
     * @param maxOccurs the maximum allowed numer of occurences of this parameter
     */
    public CmsWidgetDialogParameter(
        Object base,
        String property,
        String defaultValue,
        String dialogPage,
        I_CmsWidget widget,
        int minOccurs,
        int maxOccurs) {

        this(base, property, property, defaultValue, dialogPage, widget, minOccurs, maxOccurs);
    }

    /**
     * Create a new Widget parameter based on a given object's property.<p>
     * 
     * @param base the base object to map the parameter to / from
     * @param property the base object property to map the parameter to / from
     * @param htmlName the form id name to use in the generated HTML
     * @param defaultValue the default value to use for this parameter
     * @param dialogPage the dialog page to use the widget on
     * @param widget the widget used for this paramete
     * @param minOccurs the required minimum numer of occurences of this parameter
     * @param maxOccurs the maximum allowed numer of occurences of this parameter
     */
    public CmsWidgetDialogParameter(
        Object base,
        String property,
        String htmlName,
        String defaultValue,
        String dialogPage,
        I_CmsWidget widget,
        int minOccurs,
        int maxOccurs) {

        if (htmlName == null) {
            htmlName = property;
        }

        if ((base instanceof List) || (base instanceof SortedMap)) {

            // this is a list, use custom list mappings
            init(null, defaultValue, htmlName, widget, dialogPage, 0, MAX_OCCURENCES, 0);

            m_baseObject = null;
            m_baseObjectProperty = null;
            m_baseCollection = base;

        } else {

            // generic object:use reflection to map object properties
            init(null, defaultValue, htmlName, widget, dialogPage, minOccurs, maxOccurs, 0);

            m_baseObject = base;
            m_baseObjectProperty = property;
            m_baseCollection = null;

            PropertyUtilsBean bean = new PropertyUtilsBean();

            // make sure the base object has the requested property
            if (!bean.isReadable(m_baseObject, m_baseObjectProperty)
                || !bean.isWriteable(m_baseObject, m_baseObjectProperty)) {

                throw new CmsIllegalArgumentException(Messages.get().container(
                    Messages.ERR_NO_PROPERTY_2,
                    base.getClass().getName(),
                    property));
            }

            Object value;
            try {
                value = bean.getNestedProperty(m_baseObject, m_baseObjectProperty);
            } catch (Exception e) {
                throw new CmsRuntimeException(Messages.get().container(
                    Messages.ERR_PROPERTY_READ_2,
                    property,
                    base.getClass().getName()), e);
            }

            if (value != null) {

                if ((value instanceof List) || (value instanceof SortedMap)) {
                    m_baseCollection = value;
                    m_minOccurs = 0;
                    m_maxOccurs = MAX_OCCURENCES;
                } else {
                    m_defaultValue = String.valueOf(value);
                    m_value = m_defaultValue;
                    if ((m_minOccurs == 0) && !m_value.equals(defaultValue)) {
                        // if value is different from default ensure this widget is displayed
                        m_minOccurs = 1;
                    }
                }
            }
        }
    }

    /**
     * Create a new Widget parameter.<p>
     * 
     * @param name the name of the parameter
     * @param widget the widget used for this parameter
     */
    public CmsWidgetDialogParameter(String name, I_CmsWidget widget) {

        this(null, null, name, widget, DEFAULT_DIALOG_PAGE, 1, 1, 0);
    }

    /**
     * Create a new Widget parameter.<p>
     * 
     * @param name the name of the parameter
     * @param widget the widget used for this parameter
     * @param minOccurs the required minimum numer of occurences of this parameter
     * @param maxOccurs the maximum allowed numer of occurences of this parameter
     */
    public CmsWidgetDialogParameter(String name, I_CmsWidget widget, int minOccurs, int maxOccurs) {

        this(null, null, name, widget, DEFAULT_DIALOG_PAGE, minOccurs, maxOccurs, 0);
    }

    /**
     * Create a new Widget parameter with specified occurence settings.<p>
     * 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -