<commit_before>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 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, 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.gwt.client.ui.contextmenu;

import org.opencms.gwt.client.CmsCoreProvider;
import org.opencms.gwt.client.property.CmsPropertySubmitHandler;
import org.opencms.gwt.client.property.CmsSimplePropertyEditorHandler;
import org.opencms.gwt.client.property.CmsVfsModePropertyEditor;
import org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton;
import org.opencms.gwt.client.rpc.CmsRpcAction;
import org.opencms.gwt.client.ui.input.form.CmsDialogFormHandler;
import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler;
import org.opencms.gwt.shared.CmsContextMenuEntryBean;
import org.opencms.gwt.shared.property.CmsPropertiesBean;
import org.opencms.util.CmsUUID;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * The class for the "edit properties" context menu entries.<p>
 *
 * @since 8.0.0
 */
public final class CmsEditProperties implements I_CmsHasContextMenuCommand {

    /**
     * Helper class which encapsulates the differences between the contexts where the property edit dialog is opened.<p>
     */
    public static class PropertyEditingContext {

        /** The dialog instance. */
        protected CmsFormDialog m_formDialog;

        /** The form handler. */
        protected CmsDialogFormHandler m_formHandler;

        /** The cancel handler. */
        protected Runnable m_cancelHandler;

        /**
         * Creates the property definition button.<p>
         *
         * @return the property definition button
         */
        public CmsPropertyDefinitionButton createPropertyDefinitionButton() {

            return new CmsPropertyDefinitionButton() {

                /**
                 * @see org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton#onBeforeEditPropertyDefinition()
                 */
                @Override
                public void onBeforeEditPropertyDefinition() {

                    m_formDialog.hide();
                }
            };

        }

        /**
         * Gets the form dialog.<p>
         *
         * @return the form dialog
         */
        public CmsFormDialog getDialog() {

            return m_formDialog;
        }

        /**
         * Initializes the close handler of the dialog.<p>
         */
        public void initCloseHandler() {

            if (m_cancelHandler != null) {
                m_formDialog.addCloseHandler(new CloseHandler<PopupPanel>() {

                    public void onClose(CloseEvent<PopupPanel> event) {

                        if (!m_formHandler.isSubmitting()) {
                            m_cancelHandler.run();
                        }
                    }
                });
            }
        }

        /**
         * Sets the cancel handler.<p>
         *
         * @param cancelHandler the cancel handler
         */
        public void setCancelHandler(Runnable cancelHandler) {

            m_cancelHandler = cancelHandler;
        }

        /**
         * Sets the form dialog.<p>
         *
         * @param formDialog the form dialog
         */
        public void setDialog(CmsFormDialog formDialog) {

            m_formDialog = formDialog;
        }

        /**
         * Sets the form handler.<p>
         *
         * @param formHandler the form handler
         */
        public void setFormHandler(CmsDialogFormHandler formHandler) {

            m_formHandler = formHandler;
        }

    }

    /**
     * Hidden utility class constructor.<p>
     */
    private CmsEditProperties() {

        // nothing to do
    }

    /**
     * Starts the property editor for the resource with the given structure id.<p>
     *
     * @param structureId the structure id of a resource
     * @param contextMenuHandler the context menu handler
     * @param editName if true, provides a field for changing the file name
     * @param cancelHandler callback which is executed if the user cancels the property dialog
     * @param editContext the editing context
     */
    public static void editProperties(
        final CmsUUID structureId,
        final I_CmsContextMenuHandler contextMenuHandler,
        final boolean editName,
        final Runnable cancelHandler,
        final PropertyEditingContext editContext) {

        CmsRpcAction<CmsPropertiesBean> action = new CmsRpcAction<CmsPropertiesBean>() {

            @Override
            public void execute() {

                start(0, true);
                CmsCoreProvider.getVfsService().loadPropertyData(structureId, this);
            }

            @Override
            protected void onResponse(CmsPropertiesBean result) {

                CmsSimplePropertyEditorHandler handler = new CmsSimplePropertyEditorHandler(contextMenuHandler);
                editContext.setCancelHandler(cancelHandler);

                handler.setPropertiesBean(result);
                handler.setEditableName(editName);
                CmsVfsModePropertyEditor editor = new CmsVfsModePropertyEditor(
                    result.getPropertyDefinitions(),
                    handler);

                editor.setShowResourceProperties(!handler.isFolder());
                editor.setReadOnly(result.isReadOnly());<commit_msg>Fixed wrong use of template selection widget in property editor.<commit_after>/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 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, 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.gwt.client.ui.contextmenu;

import org.opencms.gwt.client.CmsCoreProvider;
import org.opencms.gwt.client.property.CmsPropertySubmitHandler;
import org.opencms.gwt.client.property.CmsSimplePropertyEditorHandler;
import org.opencms.gwt.client.property.CmsVfsModePropertyEditor;
import org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton;
import org.opencms.gwt.client.rpc.CmsRpcAction;
import org.opencms.gwt.client.ui.input.form.CmsDialogFormHandler;
import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler;
import org.opencms.gwt.shared.CmsContextMenuEntryBean;
import org.opencms.gwt.shared.property.CmsPropertiesBean;
import org.opencms.util.CmsUUID;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.PopupPanel;

/**
 * The class for the "edit properties" context menu entries.<p>
 *
 * @since 8.0.0
 */
public final class CmsEditProperties implements I_CmsHasContextMenuCommand {

    /**
     * Helper class which encapsulates the differences between the contexts where the property edit dialog is opened.<p>
     */
    public static class PropertyEditingContext {

        /** The dialog instance. */
        protected CmsFormDialog m_formDialog;

        /** The form handler. */
        protected CmsDialogFormHandler m_formHandler;

        /** The cancel handler. */
        protected Runnable m_cancelHandler;

        /**
         * Creates the property definition button.<p>
         *
         * @return the property definition button
         */
        public CmsPropertyDefinitionButton createPropertyDefinitionButton() {

            return new CmsPropertyDefinitionButton() {

                /**
                 * @see org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton#onBeforeEditPropertyDefinition()
                 */
                @Override
                public void onBeforeEditPropertyDefinition() {

                    m_formDialog.hide();
                }
            };

        }

        /**
         * Gets the form dialog.<p>
         *
         * @return the form dialog
         */
        public CmsFormDialog getDialog() {

            return m_formDialog;
        }

        /**
         * Initializes the close handler of the dialog.<p>
         */
        public void initCloseHandler() {

            if (m_cancelHandler != null) {
                m_formDialog.addCloseHandler(new CloseHandler<PopupPanel>() {

                    public void onClose(CloseEvent<PopupPanel> event) {

                        if (!m_formHandler.isSubmitting()) {
                            m_cancelHandler.run();
                        }
                    }
                });
            }
        }

        /**
         * Sets the cancel handler.<p>
         *
         * @param cancelHandler the cancel handler
         */
        public void setCancelHandler(Runnable cancelHandler) {

            m_cancelHandler = cancelHandler;
        }

        /**
         * Sets the form dialog.<p>
         *
         * @param formDialog the form dialog
         */
        public void setDialog(CmsFormDialog formDialog) {

            m_formDialog = formDialog;
        }

        /**
         * Sets the form handler.<p>
         *
         * @param formHandler the form handler
         */
        public void setFormHandler(CmsDialogFormHandler formHandler) {

            m_formHandler = formHandler;
        }

    }

    /**
     * Property editor handler which uses a text box for the template selection.<p>
     */
    protected static class PropertyEditorHandler extends CmsSimplePropertyEditorHandler {

        /**
         * Creates a new instance.<p>
         *
         * @param handler the handler
         */
        public PropertyEditorHandler(I_CmsContextMenuHandler handler) {
            super(handler);
        }

        /**
         * @see org.opencms.gwt.client.property.CmsSimplePropertyEditorHandler#useAdeTemplates()
         */
        @Override
        public boolean useAdeTemplates() {

            return false;
        }

    }

    /**
     * Hidden utility class constructor.<p>
     */
    private CmsEditProperties() {

        // nothing to do
    }

    /**
     * Starts the property editor for the resource with the given structure id.<p>
     *
     * @param structureId the structure id of a resource
     * @param contextMenuHandler the context menu handler
     * @param editName if true, provides a field for changing the file name
     * @param cancelHandler callback which is executed if the user cancels the property dialog
     * @param editContext the editing context
     */
    public static void editProperties(
        final CmsUUID structureId,
        final I_CmsContextMenuHandler contextMenuHandler,
        final boolean editName,
        final Runnable cancelHandler,
        final PropertyEditingContext editContext) {

        CmsRpcAction<CmsPropertiesBean> action = new CmsRpcAction<CmsPropertiesBean>() {

            @Override
            public void execute() {

                start(0, true);
                CmsCoreProvider.getVfsService().loadPropertyData(structureId, this);
            }

            @Override
            protected void onResponse(CmsPropertiesBean result) {

                PropertyEditorHandler handler = new PropertyEditorHandler(contextMenuHandler);
                editContext.setCancelHandler(cancelHandler);

                handler.setPropertiesBean(result);
                handler.setEditableName(editName);
                CmsVfsModePropertyEditor editor = new CmsVfsModePropertyEditor(
                    result.getPropertyDefinitions(),
                    handler);

                editor.setShowResourceProperties(!handler.isFolder());
                editor.setReadOnly(result.isReadOnly());
{'fraction_non_alphanumeric': 0.07741367934902134, 'fraction_numerical': 0.0029323363389780806, 'mean_word_length': 2.2605162523900573, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873175', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3421, 'n_words': 974}