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.galleries.client.preview.ui;
029
030import org.opencms.ade.galleries.client.Messages;
031import org.opencms.ade.galleries.client.preview.CmsPreviewUtil;
032import org.opencms.ade.galleries.client.preview.I_CmsPreviewHandler;
033import org.opencms.ade.galleries.shared.CmsResourceInfoBean;
034import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
035import org.opencms.gwt.client.ui.CmsConfirmDialog;
036import org.opencms.gwt.client.ui.CmsPushButton;
037import org.opencms.gwt.client.ui.CmsTabbedPanel;
038import org.opencms.gwt.client.ui.CmsTabbedPanel.CmsTabbedPanelStyle;
039import org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler;
040
041import com.google.gwt.core.client.GWT;
042import com.google.gwt.dom.client.Style.Unit;
043import com.google.gwt.event.dom.client.ClickEvent;
044import com.google.gwt.uibinder.client.UiBinder;
045import com.google.gwt.uibinder.client.UiField;
046import com.google.gwt.uibinder.client.UiHandler;
047import com.google.gwt.user.client.Command;
048import com.google.gwt.user.client.Timer;
049import com.google.gwt.user.client.ui.Composite;
050import com.google.gwt.user.client.ui.FlowPanel;
051import com.google.gwt.user.client.ui.Label;
052import com.google.gwt.user.client.ui.SimplePanel;
053import com.google.gwt.user.client.ui.Widget;
054
055/**
056 * Provides a widget skeleton for the preview dialog.<p>
057 *
058 * This widget contains a panel with the resource preview and
059 * a set of tabs with resource information under the preview panel.<p>
060 *
061 * @param <T> the resource info bean type
062 *
063 * @since 8.0.
064 */
065public abstract class A_CmsPreviewDialog<T extends CmsResourceInfoBean> extends Composite {
066
067    /**
068     * @see com.google.gwt.uibinder.client.UiBinder
069     */
070    protected interface I_CmsPreviewDialogUiBinder extends UiBinder<Widget, A_CmsPreviewDialog<?>> {
071        // GWT interface, nothing to do here
072    }
073
074    /** The ui-binder instance for this class. */
075    private static I_CmsPreviewDialogUiBinder uiBinder = GWT.create(I_CmsPreviewDialogUiBinder.class);
076
077    /** The button panel. */
078    @UiField
079    protected FlowPanel m_buttonBar;
080
081    /** The select button. */
082    @UiField
083    protected CmsPushButton m_closePreview;
084
085    /** The dialog height. */
086    protected int m_dialogHeight;
087
088    /** The dialog width. */
089    protected int m_dialogWidth;
090
091    /** The dialog mode of the gallery. */
092    protected GalleryMode m_galleryMode;
093
094    /** The parent panel of the preview dialog. */
095    @UiField
096    protected FlowPanel m_parentPanel;
097
098    /** The preview height. */
099    protected int m_previewHeight;
100
101    /** The preview placeholder panel. */
102    @UiField
103    protected FlowPanel m_previewHolder;
104
105    /** The preview panel of preview dialog. */
106    @UiField
107    protected SimplePanel m_previewPanel;
108
109    /** The select button. */
110    @UiField
111    protected CmsPushButton m_selectButton;
112
113    /** The title label. */
114    @UiField
115    protected Label m_title;
116
117    /** The tabbed panel of the preview dialog. */
118    protected CmsTabbedPanel<Widget> m_tabbedPanel;
119
120    /** The tabs placeholder panel. */
121    @UiField
122    protected FlowPanel m_tabsHolder;
123
124    /**
125     * The constructor.<p>
126     *
127     * @param dialogMode the gallery dialog mode (view, widget, ade, editor, ...)
128     * @param dialogHeight the dialog height to set
129     * @param dialogWidth the dialog width to set
130     * @param disableSelection true if selection should be disabled in the preview
131     */
132    public A_CmsPreviewDialog(GalleryMode dialogMode, int dialogHeight, int dialogWidth, boolean disableSelection) {
133
134        initWidget(uiBinder.createAndBindUi(this));
135        m_title.setText(Messages.get().key(Messages.GUI_PREVIEW_TITLE_0));
136        m_galleryMode = dialogMode;
137
138        m_dialogHeight = dialogHeight;
139        m_dialogWidth = dialogWidth;
140        int detailsHeight = 235;
141        m_previewHeight = m_dialogHeight - 32 - detailsHeight;
142        m_previewHolder.getElement().getStyle().setHeight(m_previewHeight, Unit.PX);
143        m_tabsHolder.getElement().getStyle().setHeight(detailsHeight, Unit.PX);
144        m_tabbedPanel = new CmsTabbedPanel<Widget>(CmsTabbedPanelStyle.classicTabs);
145        m_tabsHolder.add(m_tabbedPanel);
146        m_selectButton.setUseMinWidth(true);
147        m_selectButton.setText(Messages.get().key(Messages.GUI_PREVIEW_BUTTON_SELECT_0));
148        m_selectButton.setVisible(false);
149        m_closePreview.setText(Messages.get().key(Messages.GUI_PREVIEW_CLOSE_BUTTON_0));
150
151        // buttons
152        switch (m_galleryMode) {
153            case editor:
154                m_selectButton.setVisible(CmsPreviewUtil.shouldShowSelectButton() && !disableSelection);
155                m_closePreview.setText(Messages.get().key(Messages.GUI_PREVIEW_CLOSE_GALLERY_BUTTON_0));
156                m_buttonBar.getElement().getStyle().setBottom(1, Unit.PX);
157                m_buttonBar.getElement().getStyle().setRight(1, Unit.PX);
158                break;
159            case widget:
160                m_selectButton.setVisible(!disableSelection);
161                m_closePreview.setText(Messages.get().key(Messages.GUI_PREVIEW_CLOSE_GALLERY_BUTTON_0));
162                break;
163            case ade:
164            case view:
165            case adeView:
166            default:
167                break;
168        }
169    }
170
171    /**
172     * Displays a confirm save changes dialog with the given message.
173     * May insert individual message before the given one for further information.<p>
174     * Will call the appropriate command after saving/cancel.<p>
175     *
176     * @param message the message to display
177     * @param onConfirm the command executed after saving
178     * @param onCancel the command executed on cancel
179     */
180    public void confirmSaveChanges(String message, final Command onConfirm, final Command onCancel) {
181
182        CmsConfirmDialog confirmDialog = new CmsConfirmDialog("Confirm", message);
183        confirmDialog.setHandler(new I_CmsConfirmDialogHandler() {
184
185            /**
186             * @see org.opencms.gwt.client.ui.I_CmsCloseDialogHandler#onClose()
187             */
188            public void onClose() {
189
190                if (onCancel != null) {
191                    onCancel.execute();
192                }
193            }
194
195            /**
196             * @see org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler#onOk()
197             */
198            public void onOk() {
199
200                if (onConfirm != null) {
201                    onConfirm.execute();
202                }
203            }
204        });
205        confirmDialog.center();
206    }
207
208    /**
209     * Fills the content of the tabs panel.<p>
210     *
211     * @param resourceInfo the bean containing the parameter
212     */
213    public abstract void fillContent(T resourceInfo);
214
215    /**
216     * Returns the gallery mode.<p>
217     *
218     * @return the gallery mode
219     */
220    public GalleryMode getGalleryMode() {
221
222        return m_galleryMode;
223    }
224
225    /**
226     * Returns if there are any changes that need saving, before the preview may be closed.<p>
227     *
228     * @return <code>true</code> if changed
229     */
230    public abstract boolean hasChanges();
231
232    /**
233     * Will be triggered, when the select button is clicked.<p>
234     *
235     * @param event the click event
236     */
237    @UiHandler("m_closePreview")
238    public void onCloseClick(ClickEvent event) {
239
240        saveChanges(null);
241        getHandler().closePreview();
242    }
243
244    /**
245     * Will be triggered, when the select button is clicked.<p>
246     *
247     * @param event the click event
248     */
249    @UiHandler("m_selectButton")
250    public void onSelectClick(ClickEvent event) {
251
252        if (m_galleryMode == GalleryMode.editor) {
253            // note: the select button isn't necessarily visible in editor mode (depending on the WYSIWYG editor), but
254            // if it is, we want it to save the data and close the gallery dialog
255            if (getHandler().setDataInEditor()) {
256                // do this after a delay, so we don't get ugly Javascript errors when the iframe is closed.
257                Timer timer = new Timer() {
258
259                    @Override
260                    public void run() {
261
262                        CmsPreviewUtil.closeDialog();
263                    }
264
265                };
266                timer.schedule(1);
267            }
268        } else {
269            saveChanges(null);
270            getHandler().selectResource();
271        }
272    }
273
274    /**
275     * Removes the preview.<p>
276     */
277    public void removePreview() {
278
279        removeFromParent();
280    }
281
282    /**
283     * Saves the changes for this dialog.<p>
284     *
285     * @param afterSaveCommand the command to execute after saving the changes
286     */
287    public abstract void saveChanges(Command afterSaveCommand);
288
289    /**
290     * Returns the preview handler.<p>
291     *
292     * @return the preview handler
293     */
294    protected abstract I_CmsPreviewHandler<T> getHandler();
295}