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.sitemap.client.alias;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.gwt.client.ui.CmsPopup;
032import org.opencms.gwt.client.ui.CmsPushButton;
033
034import com.google.gwt.core.client.Scheduler;
035import com.google.gwt.core.client.Scheduler.RepeatingCommand;
036import com.google.gwt.event.logical.shared.CloseEvent;
037import com.google.gwt.event.logical.shared.CloseHandler;
038import com.google.gwt.user.client.rpc.AsyncCallback;
039import com.google.gwt.user.client.ui.PopupPanel;
040
041/**
042 * The alias editor.<p>
043 */
044public class CmsAliasEditor {
045
046    /** The interval used for upating the editing status on the server. */
047    public static final int STATUS_UPDATE_INTERVAL = 10000;
048
049    /** Flag to indicate that the alias editor is not being used anymore. */
050    boolean m_finished;
051
052    /** The controller. */
053    private CmsAliasTableController m_controller;
054
055    /** The view. */
056    private CmsAliasView m_view;
057
058    /**
059     * Creates a new instance.<p>
060     */
061    public CmsAliasEditor() {
062
063        m_controller = new CmsAliasTableController();
064        m_view = new CmsAliasView(m_controller);
065        m_controller.setView(m_view);
066    }
067
068    /**
069     * Checks whether the alias editor is finished.<p>
070     *
071     * @return true if the alias editor is finished
072     */
073    public boolean isFinished() {
074
075        return m_finished;
076    }
077
078    /**
079     * Opens the alias editor.<p>
080     *
081     * In addition to displaying the alias editor, this also sets up a timer which regularly informs the server
082     * that the alias table for the current site is being edited by the current user. This timer is deactivated
083     * when the dialog is closed.<p>
084     */
085    public void show() {
086
087        final CmsPopup popup = new CmsPopup(CmsAliasMessages.messageTitleAliasEditor());
088        popup.setGlassEnabled(true);
089        popup.setModal(true);
090
091        final RepeatingCommand updateCommand = new RepeatingCommand() {
092
093            public boolean execute() {
094
095                if (!isFinished() && popup.isVisible() && popup.isAttached()) {
096                    updateAliasEditorStatus(true);
097                    return true;
098                } else {
099                    return false;
100                }
101            }
102        };
103
104        popup.setMainContent(m_view);
105        popup.addCloseHandler(new CloseHandler<PopupPanel>() {
106
107            public void onClose(CloseEvent<PopupPanel> event) {
108
109                setFinished(true);
110                updateAliasEditorStatus(false);
111            }
112        });
113        for (CmsPushButton button : m_view.getButtonBar()) {
114            popup.addButton(button);
115        }
116        m_view.setPopup(popup);
117        popup.setWidth(946);
118        popup.addDialogClose(null);
119        m_controller.load(new Runnable() {
120
121            public void run() {
122
123                popup.centerHorizontally(80);
124                Scheduler.get().scheduleFixedDelay(updateCommand, STATUS_UPDATE_INTERVAL);
125            }
126        });
127
128    }
129
130    /**
131     * Sets the 'finished' flag.<p>
132     *
133     * @param finished the new value of the 'finished' flag
134     */
135    protected void setFinished(boolean finished) {
136
137        m_finished = finished;
138    }
139
140    /**
141     * Asynchronously updates the alias editor status.<p>
142     *
143     * @param editing the status we want to set
144     */
145    protected void updateAliasEditorStatus(boolean editing) {
146
147        CmsSitemapView.getInstance().getController().getService().updateAliasEditorStatus(
148            editing,
149            new AsyncCallback<Void>() {
150
151                public void onFailure(Throwable caught) {
152
153                    // do nothing
154                }
155
156                public void onSuccess(Void result) {
157
158                    // do nothing
159                }
160
161            });
162    }
163}