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.ade.sitemap.client.toolbar;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.ade.sitemap.client.control.CmsSitemapController;
032import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
033import org.opencms.gwt.client.CmsCoreProvider;
034import org.opencms.gwt.client.rpc.CmsRpcAction;
035import org.opencms.gwt.client.ui.CmsAlertDialog;
036import org.opencms.gwt.client.ui.CmsPushButton;
037import org.opencms.gwt.client.ui.I_CmsButton;
038import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
039import org.opencms.gwt.client.ui.I_CmsButton.Size;
040import org.opencms.gwt.client.util.CmsMessages;
041import org.opencms.gwt.shared.CmsReturnLinkInfo;
042import org.opencms.util.CmsStringUtil;
043
044import com.google.gwt.event.dom.client.ClickEvent;
045import com.google.gwt.event.dom.client.ClickHandler;
046import com.google.gwt.user.client.Window;
047
048/**
049 * The toolbar button for jumping to the last visited container page.<p>
050 *
051 * @since 8.0.0
052 */
053public class CmsToolbarGoBackButton extends CmsPushButton {
054
055    /**
056     * Constructor.<p>
057     *
058     * @param toolbar the toolbar instance
059     * @param controller the sitemap controller
060     */
061    public CmsToolbarGoBackButton(final CmsSitemapToolbar toolbar, final CmsSitemapController controller) {
062
063        setImageClass(I_CmsButton.ButtonData.BACK.getIconClass());
064        setTitle(I_CmsButton.ButtonData.BACK.getTitle());
065        setButtonStyle(ButtonStyle.FONT_ICON, null);
066        setSize(Size.big);
067        addClickHandler(new ClickHandler() {
068
069            /**
070             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
071             */
072            public void onClick(ClickEvent event) {
073
074                toolbar.onButtonActivation(CmsToolbarGoBackButton.this);
075                CmsToolbarGoBackButton.this.clearHoverState();
076                setDown(false);
077                setEnabled(false);
078                goBack(controller.getData().getReturnCode());
079            }
080        });
081    }
082
083    /**
084     * Opens the publish dialog without changes check.<p>
085     *
086     * @param returnCode the return code previously passed to the sitemap editor
087     */
088    public static void goBack(final String returnCode) {
089
090        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(returnCode)) {
091            CmsRpcAction<CmsReturnLinkInfo> goBackAction = new CmsRpcAction<CmsReturnLinkInfo>() {
092
093                @Override
094                public void execute() {
095
096                    start(300, false);
097                    CmsCoreProvider.getService().getLinkForReturnCode(returnCode, this);
098                }
099
100                @Override
101                protected void onResponse(CmsReturnLinkInfo result) {
102
103                    stop(false);
104                    if (result.getStatus() == CmsReturnLinkInfo.Status.ok) {
105                        Window.Location.assign(result.getLink());
106                    } else if (result.getStatus() == CmsReturnLinkInfo.Status.notfound) {
107                        CmsMessages msg = org.opencms.ade.sitemap.client.Messages.get();
108                        String title = msg.key(
109                            org.opencms.ade.sitemap.client.Messages.GUI_RETURN_PAGE_NOT_FOUND_TITLE_0);
110                        String content = msg.key(
111                            org.opencms.ade.sitemap.client.Messages.GUI_RETURN_PAGE_NOT_FOUND_TEXT_0);
112                        CmsAlertDialog alert = new CmsAlertDialog(title, content);
113                        alert.center();
114                    }
115                }
116            };
117
118            goBackAction.execute();
119        } else {
120            CmsSitemapController controller = CmsSitemapView.getInstance().getController();
121            CmsClientSitemapEntry root = controller.getData().getRoot();
122            String newPath = root.getSitePath();
123            controller.leaveEditor(newPath);
124        }
125    }
126
127}