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.components.extensions.CmsPollServerExtension;
031import org.opencms.ui.shared.rpc.I_CmsPollServerRpc;
032
033import com.google.gwt.core.client.Scheduler;
034import com.google.gwt.core.client.Scheduler.RepeatingCommand;
035import com.google.gwt.event.logical.shared.AttachEvent;
036import com.google.gwt.event.logical.shared.AttachEvent.Handler;
037import com.google.gwt.user.client.ui.Widget;
038import com.vaadin.client.ComponentConnector;
039import com.vaadin.client.ServerConnector;
040import com.vaadin.client.extensions.AbstractExtensionConnector;
041import com.vaadin.shared.ui.Connect;
042
043/**
044 * This connector will manipulate the CSS classes of the extended widget depending on the scroll position.<p>
045 */
046@Connect(CmsPollServerExtension.class)
047public class CmsPollServerConnector extends AbstractExtensionConnector {
048
049    /** Polls the server. */
050    protected class PollingCommand implements RepeatingCommand {
051
052        /** If polling is cancelled. */
053        private boolean m_cancelled;
054
055        /**
056         * @see com.google.gwt.core.client.Scheduler.RepeatingCommand#execute()
057         */
058        public boolean execute() {
059
060            if (!m_cancelled) {
061                try {
062                    m_rpc.poll();
063                } catch (@SuppressWarnings("unused") Throwable t) {
064                    m_cancelled = true;
065                }
066            }
067            return !m_cancelled;
068        }
069
070        /**
071         * Cancels polling.<p>
072         */
073        void cancel() {
074
075            m_cancelled = true;
076        }
077    }
078
079    /** The polling delay in milliseconds. */
080    private static final int POLLING_DELAY_MS = 30 * 1000;
081
082    /** The serial version id. */
083    private static final long serialVersionUID = -3661096843568550285L;
084
085    /** The RPC proxy. */
086    I_CmsPollServerRpc m_rpc;
087
088    /** The polling command. */
089    private PollingCommand m_command;
090
091    /** The widget to enhance. */
092    private Widget m_widget;
093
094    /**
095     * Constructor.<p>
096     */
097    public CmsPollServerConnector() {
098        m_rpc = getRpcProxy(I_CmsPollServerRpc.class);
099    }
100
101    /**
102     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
103     */
104    @Override
105    protected void extend(ServerConnector target) {
106
107        // Get the extended widget
108        m_widget = ((ComponentConnector)target).getWidget();
109        m_widget.addAttachHandler(new Handler() {
110
111            public void onAttachOrDetach(AttachEvent event) {
112
113                if (event.isAttached()) {
114                    startPolling();
115                } else {
116                    stopPolling();
117                }
118            }
119        });
120        startPolling();
121    }
122
123    /**
124     * Starts polling the server.<p>
125     */
126    void startPolling() {
127
128        m_rpc.poll();
129        if (m_command == null) {
130            m_command = new PollingCommand();
131            Scheduler.get().scheduleFixedDelay(m_command, POLLING_DELAY_MS);
132        }
133    }
134
135    /**
136     * Stops polling the server.<p>
137     */
138    void stopPolling() {
139
140        if (m_command != null) {
141            m_command.cancel();
142            m_command = null;
143        }
144    }
145}