001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.client;
029
030import org.opencms.ui.editors.CmsEditorStateExtension;
031import org.opencms.ui.shared.rpc.I_CmsEditorStateRPC;
032import org.opencms.util.CmsStringUtil;
033
034import com.google.gwt.core.client.Scheduler;
035import com.google.gwt.core.client.Scheduler.RepeatingCommand;
036import com.vaadin.client.ComponentConnector;
037import com.vaadin.client.ServerConnector;
038import com.vaadin.client.extensions.AbstractExtensionConnector;
039import com.vaadin.shared.ui.Connect;
040
041/**
042 * Client connector to the editor state extension.<p>
043 */
044@Connect(CmsEditorStateExtension.class)
045public class CmsEditorStateConnector extends AbstractExtensionConnector {
046
047    /** The serial version id. */
048    private static final long serialVersionUID = 7273974997472100908L;
049
050    /** The number of export method attemps. */
051    int m_exportRuns;
052
053    /** The RPC proxy. */
054    I_CmsEditorStateRPC m_rpc;
055
056    /**
057     * Constructor.<p>
058     */
059    public CmsEditorStateConnector() {
060
061        m_rpc = getRpcProxy(I_CmsEditorStateRPC.class);
062    }
063
064    /**
065     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
066     */
067    @Override
068    protected void extend(final ServerConnector target) {
069
070        m_exportRuns = 0;
071        Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
072
073            public boolean execute() {
074
075                m_exportRuns++;
076                String frameName = ((ComponentConnector)target).getWidget().getElement().getAttribute("name");
077                if (CmsStringUtil.isEmptyOrWhitespaceOnly(frameName)) {
078                    frameName = "edit";
079                }
080                return !exportMethod(frameName) && (m_exportRuns < 5);
081            }
082        }, 50);
083    }
084
085    /**
086     * Exports the setEditorChangedState method to the edit frame.<p>
087     *
088     * @param frameName the edit frame name
089     *
090     * @return <code>true</code> indicates the frame was found and the method exported
091     */
092    native boolean exportMethod(String frameName)/*-{
093        var self = this;
094        var frame = $wnd.frames[frameName];
095        if (frame == null && $wnd.frames.length > 0) {
096            for (i = 0; i < $wnd.frames.length; i++) {
097                if ($wnd.frames[i].frameElement.name == frameName) {
098                    frame = $wnd.frames[i];
099                    break;
100                }
101            }
102        }
103
104        if (frame != null) {
105            frame.cmsSetEditorChangedState = function(changed) {
106                self.@org.opencms.ui.client.CmsEditorStateConnector::setEditorChangedState(Z)(changed);
107            }
108            return true;
109        } else
110            return false;
111    }-*/;
112
113    /**
114     * Sets the editor changed state.<p>
115     *
116     * @param changed indicates if the editor content was changed
117     */
118    private void setEditorChangedState(boolean changed) {
119
120        m_rpc.setHasChanges(changed);
121    }
122
123}