001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://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        m_rpc = getRpcProxy(I_CmsEditorStateRPC.class);
061    }
062
063    /**
064     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
065     */
066    @Override
067    protected void extend(final ServerConnector target) {
068
069        m_exportRuns = 0;
070        Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
071
072            public boolean execute() {
073
074                m_exportRuns++;
075                String frameName = ((ComponentConnector)target).getWidget().getElement().getAttribute("name");
076                if (CmsStringUtil.isEmptyOrWhitespaceOnly(frameName)) {
077                    frameName = "edit";
078                }
079                return !exportMethod(frameName) && (m_exportRuns < 5);
080            }
081        }, 50);
082    }
083
084    /**
085     * Exports the setEditorChangedState method to the edit frame.<p>
086     *
087     * @param frameName the edit frame name
088     *
089     * @return <code>true</code> indicates the frame was found and the method exported
090     */
091    native boolean exportMethod(String frameName)/*-{
092                var self = this;
093                var frame = $wnd.frames[frameName];
094                if (frame == null && $wnd.frames.length > 0) {
095                        for (i = 0; i < $wnd.frames.length; i++) {
096                                if ($wnd.frames[i].frameElement.name == frameName) {
097                                        frame = $wnd.frames[i];
098                                        break;
099                                }
100                        }
101                }
102
103                if (frame != null) {
104                        frame.cmsSetEditorChangedState = function(changed) {
105                                self.@org.opencms.ui.client.CmsEditorStateConnector::setEditorChangedState(Z)(changed);
106                        }
107                        return true;
108                } else
109                        return false;
110    }-*/;
111
112    /**
113     * Sets the editor changed state.<p>
114     *
115     * @param changed indicates if the editor content was changed
116     */
117    private void setEditorChangedState(boolean changed) {
118
119        m_rpc.setHasChanges(changed);
120    }
121
122}