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.gwt.client.ui.contextmenu;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.rpc.CmsRpcAction;
032import org.opencms.gwt.client.ui.CmsConfirmDialog;
033import org.opencms.gwt.client.ui.CmsPopup;
034import org.opencms.gwt.client.ui.CmsPreviewDialog;
035import org.opencms.gwt.client.ui.CmsPreviewDialog.I_PreviewInfoProvider;
036import org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler;
037import org.opencms.gwt.client.ui.history.CmsHistoryMessages;
038import org.opencms.gwt.client.ui.history.CmsResourceHistoryView;
039import org.opencms.gwt.client.ui.history.I_CmsHistoryActionHandler;
040import org.opencms.gwt.shared.CmsContextMenuEntryBean;
041import org.opencms.gwt.shared.CmsHistoryResourceBean;
042import org.opencms.gwt.shared.CmsHistoryResourceCollection;
043import org.opencms.gwt.shared.CmsPreviewInfo;
044import org.opencms.util.CmsUUID;
045
046import com.google.gwt.user.client.rpc.AsyncCallback;
047
048/**
049 * Context menu entry class for the history dialog.<p>
050 *
051 * The history dialog allows the user to preview or restore a previous version of a content.<p>
052 */
053public class CmsHistory implements I_CmsHasContextMenuCommand, I_CmsContextMenuCommand {
054
055    /**
056     * Gets the context menu command.<p>
057     *
058     * @return the context menu command
059     */
060    public static I_CmsContextMenuCommand getContextMenuCommand() {
061
062        return new CmsHistory();
063    }
064
065    /**
066     * Loads the data necessary for the history dialog and then displays that dialog.<p>
067     *
068     * @param structureId the structure id for which the history should be loaded.<p>
069     * @param handler the history action handler to use
070     */
071    public static void loadDialog(final CmsUUID structureId, final I_CmsHistoryActionHandler handler) {
072
073        CmsRpcAction<CmsHistoryResourceCollection> action = new CmsRpcAction<CmsHistoryResourceCollection>() {
074
075            @Override
076            public void execute() {
077
078                start(200, true);
079                CmsCoreProvider.getVfsService().getResourceHistory(structureId, this);
080            }
081
082            @Override
083            protected void onResponse(CmsHistoryResourceCollection result) {
084
085                stop(false);
086                final CmsPopup popup = new CmsPopup(CmsHistoryMessages.dialogTitle(), 1150);
087                popup.addDialogClose(null);
088                CmsResourceHistoryView view = new CmsResourceHistoryView(result, handler);
089                handler.setPostRevertAction(new Runnable() {
090
091                    public void run() {
092
093                        popup.hide();
094                    }
095                });
096                popup.setMainContent(view);
097                popup.setModal(true);
098                popup.setGlassEnabled(true);
099                popup.centerHorizontally(80);
100            }
101
102        };
103        action.execute();
104
105    }
106
107    /**
108     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#execute(org.opencms.util.CmsUUID, org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler, org.opencms.gwt.shared.CmsContextMenuEntryBean)
109     */
110    public void execute(
111        final CmsUUID structureId,
112        final I_CmsContextMenuHandler handler,
113        CmsContextMenuEntryBean bean) {
114
115        I_CmsHistoryActionHandler historyActionHandler = new I_CmsHistoryActionHandler() {
116
117            private Runnable m_postRevertAction;
118
119            public void revert(final CmsHistoryResourceBean historyRes) {
120
121                CmsConfirmDialog confirmation = new CmsConfirmDialog(
122                    CmsHistoryMessages.captionConfirm(),
123                    CmsHistoryMessages.textConfirm());
124                confirmation.setHandler(new I_CmsConfirmDialogHandler() {
125
126                    public void onClose() {
127
128                        // do nothing
129                    }
130
131                    public void onOk() {
132
133                        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
134
135                            @Override
136                            public void execute() {
137
138                                start(200, true);
139                                CmsCoreProvider.getVfsService().restoreResource(
140                                    structureId,
141                                    historyRes.getVersion().getVersionNumber().intValue(),
142                                    this);
143                            }
144
145                            @SuppressWarnings("synthetic-access")
146                            @Override
147                            protected void onResponse(Void result) {
148
149                                stop(false);
150                                if (m_postRevertAction != null) {
151                                    m_postRevertAction.run();
152                                }
153                                handler.refreshResource(structureId);
154                            }
155                        };
156                        action.execute();
157                    }
158                });
159                confirmation.center();
160            }
161
162            public void setPostRevertAction(Runnable action) {
163
164                m_postRevertAction = action;
165            }
166
167            public void showPreview(final CmsHistoryResourceBean historyRes) {
168
169                CmsRpcAction<CmsPreviewInfo> previewAction = new CmsRpcAction<CmsPreviewInfo>() {
170
171                    @Override
172                    public void execute() {
173
174                        start(0, true);
175                        CmsCoreProvider.getVfsService().getHistoryPreviewInfo(
176                            structureId,
177                            CmsCoreProvider.get().getLocale(),
178                            historyRes.getVersion(),
179                            this);
180                    }
181
182                    @Override
183                    protected void onResponse(CmsPreviewInfo result) {
184
185                        stop(false);
186                        CmsPreviewDialog dialog = CmsPreviewDialog.createPreviewDialog(result);
187                        dialog.setPreviewInfoProvider(new I_PreviewInfoProvider() {
188
189                            public void loadPreviewForLocale(
190                                String locale,
191                                AsyncCallback<CmsPreviewInfo> resultCallback) {
192
193                                CmsCoreProvider.getVfsService().getHistoryPreviewInfo(
194                                    structureId,
195                                    locale,
196                                    historyRes.getVersion(),
197                                    resultCallback);
198                            }
199                        });
200
201                        dialog.center();
202                    }
203                };
204                previewAction.execute();
205            }
206
207        };
208        loadDialog(structureId, historyActionHandler);
209    }
210
211    /**
212     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#getItemWidget(org.opencms.util.CmsUUID, org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler, org.opencms.gwt.shared.CmsContextMenuEntryBean)
213     */
214    public A_CmsContextMenuItem getItemWidget(
215        CmsUUID structureId,
216        I_CmsContextMenuHandler handler,
217        CmsContextMenuEntryBean bean) {
218
219        // TODO Auto-generated method stub
220        return null;
221    }
222
223    /**
224     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand#hasItemWidget()
225     */
226    public boolean hasItemWidget() {
227
228        // TODO Auto-generated method stub
229        return false;
230    }
231
232}