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;
029
030import org.opencms.ade.galleries.client.ui.CmsGalleryDialog;
031import org.opencms.ade.galleries.client.ui.CmsGalleryPopup;
032import org.opencms.ade.galleries.client.ui.CmsResultListItem;
033import org.opencms.ade.galleries.shared.CmsGalleryDataBean;
034import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
035import org.opencms.ade.galleries.shared.CmsResultItemBean;
036import org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration;
037import org.opencms.gwt.client.dnd.CmsDNDHandler;
038import org.opencms.gwt.client.rpc.CmsRpcPrefetcher;
039import org.opencms.gwt.client.ui.CmsErrorDialog;
040import org.opencms.gwt.client.ui.CmsPopup;
041import org.opencms.gwt.client.ui.CmsTabbedPanel.CmsTabbedPanelStyle;
042import org.opencms.gwt.client.ui.I_CmsAutoHider;
043
044import com.google.gwt.user.client.rpc.SerializationException;
045import com.google.gwt.user.client.ui.Widget;
046
047/**
048 * Factory class to create gallery dialog with or without parameter.<p>
049 *
050 * @since 8.0.
051 */
052public final class CmsGalleryFactory {
053
054    /**
055     * Prevent instantiation.<p>
056     */
057    private CmsGalleryFactory() {
058
059        // empty
060    }
061
062    /**
063     * Returns a gallery dialog object.<p>
064     *
065     * @param popup the parent popup widget
066     *
067     * @return gallery dialog
068     */
069    @SuppressWarnings("unused")
070    public static CmsGalleryDialog createDialog(final CmsPopup popup) {
071
072        CmsGalleryDialog galleryDialog = new CmsGalleryDialog(new I_CmsGalleryHandler() {
073
074            public boolean filterDnd(CmsResultItemBean resultBean) {
075
076                // TODO: Auto-generated method stub
077                return true;
078            }
079
080            public Widget getAdditionalTypeTabControl() {
081
082                return null;
083            }
084
085            public I_CmsAutoHider getAutoHideParent() {
086
087                return popup;
088            }
089
090            public CmsDNDHandler getDndHandler() {
091
092                return null;
093            }
094
095            public void processResultItem(CmsResultListItem item) {
096
097                // do nothing
098            }
099
100        }, CmsTabbedPanelStyle.buttonTabs);
101        try {
102            CmsGalleryDataBean data = getGalleryDataFromDict();
103            CmsGallerySearchBean search = getSearchBeanFromDict();
104            new CmsGalleryController(new CmsGalleryControllerHandler(galleryDialog), data, search);
105        } catch (SerializationException e) {
106            CmsErrorDialog.handleException(
107                new Exception(
108                    "Deserialization of gallery data failed. This may be caused by expired java-script resources, please clear your browser cache and try again.",
109                    e));
110        }
111
112        return galleryDialog;
113    }
114
115    /**
116     * Creates a new gallery dialog.<p>
117     *
118     * @param galleryHandler the gallery handler
119     * @param data the gallery data
120     *
121     * @return the gallery dialog instance
122     */
123    @SuppressWarnings("unused")
124    public static CmsGalleryDialog createDialog(I_CmsGalleryHandler galleryHandler, CmsGalleryDataBean data) {
125
126        CmsGalleryDialog galleryDialog = new CmsGalleryDialog(galleryHandler);
127        new CmsGalleryController(new CmsGalleryControllerHandler(galleryDialog), data, null);
128        return galleryDialog;
129    }
130
131    /**
132     * Creates a gallery widget pop-up.<p>
133     *
134     * @param handler the widget handler, used to set the widgets value
135     * @param conf the gallery configuration
136     *
137     * @return the generated pop-up
138     */
139    public static CmsGalleryPopup createGalleryPopup(
140        I_CmsGalleryWidgetHandler handler,
141        I_CmsGalleryConfiguration conf) {
142
143        return new CmsGalleryPopup(handler, conf);
144    }
145
146    /**
147     * Deserializes the prefetched gallery data.<p>
148     *
149     * @return the gallery data
150     *
151     * @throws SerializationException in case deserialization fails
152     */
153    private static CmsGalleryDataBean getGalleryDataFromDict() throws SerializationException {
154
155        return (CmsGalleryDataBean)CmsRpcPrefetcher.getSerializedObjectFromDictionary(
156            CmsGalleryController.getGalleryService(),
157            CmsGalleryDataBean.DICT_NAME);
158    }
159
160    /**
161     * Deserializes the prefetched gallery search.<p>
162     *
163     * @return the gallery data
164     *
165     * @throws SerializationException in case deserialization fails
166     */
167    private static CmsGallerySearchBean getSearchBeanFromDict() throws SerializationException {
168
169        return (CmsGallerySearchBean)CmsRpcPrefetcher.getSerializedObjectFromDictionary(
170            CmsGalleryController.getGalleryService(),
171            CmsGallerySearchBean.DICT_NAME);
172    }
173}