001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.ade.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.CmsContainerpageController;
031import org.opencms.ade.containerpage.client.CmsContainerpageEvent;
032import org.opencms.ade.containerpage.client.CmsContainerpageEvent.EventType;
033import org.opencms.ade.containerpage.client.CmsContainerpageHandler;
034import org.opencms.ade.containerpage.client.I_CmsContainerpageEventHandler;
035import org.opencms.ade.publish.client.CmsPublishEvent;
036import org.opencms.ade.publish.client.I_CmsPublishEventHandler;
037import org.opencms.gwt.client.CmsCoreProvider;
038import org.opencms.gwt.client.rpc.CmsRpcAction;
039import org.opencms.gwt.client.ui.A_CmsToolbarButton;
040import org.opencms.gwt.client.ui.I_CmsButton;
041import org.opencms.gwt.client.ui.css.I_CmsToolbarButtonLayoutBundle;
042import org.opencms.gwt.client.util.CmsStyleVariable;
043
044import com.google.gwt.user.client.Timer;
045
046/**
047 * Class for the toolbar button to display elements information.<p>
048 */
049public class CmsToolbarElementInfoButton extends A_CmsToolbarButton<CmsContainerpageHandler>
050implements I_CmsContainerpageEventHandler, I_CmsPublishEventHandler {
051
052    /** The container page controller. */
053    CmsContainerpageController m_controller;
054
055    /** Style variable to change the button appearance depending on whether the page or elements have been changed. */
056    private CmsStyleVariable m_changedStyleVar;
057
058    /**
059     * Constructor.<p>
060     *
061     * @param handler the container-page handler
062     * @param controller the container page controller
063     */
064    public CmsToolbarElementInfoButton(CmsContainerpageHandler handler, CmsContainerpageController controller) {
065
066        super(I_CmsButton.ButtonData.ELEMENT_INFO, handler);
067        m_changedStyleVar = new CmsStyleVariable(this);
068        controller.addContainerpageEventHandler(this);
069        CmsCoreProvider.get().getEventBus().addHandler(CmsPublishEvent.TYPE, this);
070        m_controller = controller;
071        setChanged(false);
072    }
073
074    /**
075     * @see org.opencms.ade.containerpage.client.I_CmsContainerpageEventHandler#onContainerpageEvent(org.opencms.ade.containerpage.client.CmsContainerpageEvent)
076     */
077    public void onContainerpageEvent(CmsContainerpageEvent event) {
078
079        if ((event.getEventType() == EventType.elementEdited) || (event.getEventType() == EventType.pageSaved)) {
080            asyncUpdate();
081        }
082    }
083
084    /**
085     * @see org.opencms.ade.publish.client.I_CmsPublishEventHandler#onPublish(org.opencms.ade.publish.client.CmsPublishEvent)
086     */
087    public void onPublish(CmsPublishEvent e) {
088
089        Timer timer = new Timer() {
090
091            @Override
092            public void run() {
093
094                asyncUpdate();
095            }
096        };
097        // wait 5 seconds, which should be enough for small publish jobs
098        // (if it's not enough, the user can still reload the page)
099        timer.schedule(5000);
100    }
101
102    /**
103     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
104     */
105    public void onToolbarActivate() {
106
107        CmsContainerpageHandler handler = getHandler();
108        handler.openElementsInfo();
109        setEnabled(false);
110    }
111
112    /**
113     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
114     */
115    public void onToolbarDeactivate() {
116
117        setEnabled(true);
118    }
119
120    /**
121     * Changes the "changed" state of the button.<p>
122     *
123     * @param changed the new value for the "changed" state
124     */
125    public void setChanged(boolean changed) {
126
127        m_changedStyleVar.setValue(
128            changed
129            ? I_CmsToolbarButtonLayoutBundle.INSTANCE.toolbarButtonCss().elementInfoChanged()
130            : I_CmsToolbarButtonLayoutBundle.INSTANCE.toolbarButtonCss().elementInfoUnchanged());
131    }
132
133    /**
134     * @see com.google.gwt.user.client.ui.Widget#onLoad()
135     */
136    @Override
137    protected void onLoad() {
138
139        super.onLoad();
140        Timer timer = new Timer() {
141
142            @Override
143            public void run() {
144
145                asyncUpdate();
146            }
147
148        };
149        timer.schedule(500);
150    }
151
152    /**
153     * Fires off an RPC request to check for the elements state and then updates the button accordingly.<p>
154     */
155    void asyncUpdate() {
156
157        CmsRpcAction<Boolean> action = new CmsRpcAction<Boolean>() {
158
159            @Override
160            public void execute() {
161
162                start(200, false);
163                m_controller.getContainerpageService().checkContainerpageOrElementsChanged(
164                    CmsCoreProvider.get().getStructureId(),
165                    CmsContainerpageController.get().getData().getDetailId(),
166                    CmsContainerpageController.get().getData().getLocale(),
167                    this);
168            }
169
170            @Override
171            protected void onResponse(Boolean result) {
172
173                stop(false);
174                boolean changed = result.booleanValue();
175                setChanged(changed);
176            }
177
178        };
179        action.execute();
180    }
181
182}