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.ui;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.main.CmsUIServlet;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.components.CmsBasicDialog;
035import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
036import org.opencms.ui.components.extensions.CmsWindowExtension;
037import org.opencms.ui.login.CmsLoginHelper;
038import org.opencms.ui.util.CmsDisplayType;
039import org.opencms.util.CmsRequestUtil;
040import org.opencms.workplace.CmsWorkplaceManager;
041import org.opencms.workplace.CmsWorkplaceSettings;
042
043import java.io.Serializable;
044import java.util.Map;
045import java.util.concurrent.ConcurrentHashMap;
046
047import javax.servlet.http.HttpSession;
048
049import com.google.common.collect.Multimap;
050import com.vaadin.server.VaadinRequest;
051import com.vaadin.server.VaadinService;
052import com.vaadin.server.VaadinServlet;
053import com.vaadin.server.WrappedHttpSession;
054import com.vaadin.ui.Button;
055import com.vaadin.ui.Component;
056import com.vaadin.ui.Notification;
057import com.vaadin.ui.Notification.Type;
058import com.vaadin.ui.TooltipConfiguration;
059import com.vaadin.ui.UI;
060import com.vaadin.ui.Window;
061import com.vaadin.v7.ui.Label;
062import com.vaadin.v7.ui.VerticalLayout;
063
064/**
065 * Abstract UI class providing access to the OpenCms context.<p>
066 */
067public abstract class A_CmsUI extends UI {
068
069    /** The last offline project attribute key. */
070    public static final String LAST_OFFLINE_PROJECT = "lastOfflineProject";
071
072    /** Serial version id. */
073    private static final long serialVersionUID = 989182479322461838L;
074
075    /** UI attribute storage. */
076    private Map<String, Serializable> m_attributes;
077
078    /** The display type at the time the UI was initialized. */
079    private CmsDisplayType m_displayType;
080
081    /** Extension used for opening new browser windows. */
082    private CmsWindowExtension m_windowExtension;
083
084    /**
085     * Constructor.<p>
086     */
087    public A_CmsUI() {
088
089        m_windowExtension = new CmsWindowExtension(this);
090        m_attributes = new ConcurrentHashMap<String, Serializable>();
091        getLoadingIndicatorConfiguration().setFirstDelay(600);
092        TooltipConfiguration tooltipConfig = getTooltipConfiguration();
093        tooltipConfig.setOpenDelay(750);
094        tooltipConfig.setQuickOpenDelay(750);
095
096    }
097
098    /**
099     * Returns the current UI.<p>
100     *
101     * @return the current UI
102     */
103    public static A_CmsUI get() {
104
105        return (A_CmsUI)(UI.getCurrent());
106    }
107
108    /**
109     * Returns the current cms context.<p>
110     *
111     * @return the current cms context
112     */
113    public static CmsObject getCmsObject() {
114
115        return ((CmsUIServlet)VaadinServlet.getCurrent()).getCmsObject();
116    }
117
118    /**
119     * Changes to the given project. Will update session and workplace settings.<p>
120     *
121     * @param project the project to change to
122     */
123    public void changeProject(CmsProject project) {
124
125        CmsObject cms = getCmsObject();
126        if (!cms.getRequestContext().getCurrentProject().equals(project)) {
127            cms.getRequestContext().setCurrentProject(project);
128            getWorkplaceSettings().setProject(project.getUuid());
129            OpenCms.getSessionManager().updateSessionInfo(cms, getHttpSession());
130            if (!project.isOnlineProject()) {
131                setAttribute(LAST_OFFLINE_PROJECT, project);
132            }
133        }
134    }
135
136    /**
137     * Changes to the given site. Will update session and workplace settings.<p>
138     *
139     * @param siteRoot the site to change to
140     */
141    public void changeSite(String siteRoot) {
142
143        if (!getCmsObject().getRequestContext().getSiteRoot().equals(siteRoot)) {
144            getCmsObject().getRequestContext().setSiteRoot(siteRoot);
145            getWorkplaceSettings().setSite(siteRoot);
146            OpenCms.getSessionManager().updateSessionInfo(getCmsObject(), getHttpSession());
147        }
148    }
149
150    /**
151     * Closes all opened dialog windows.<p>
152     */
153    public void closeWindows() {
154
155        for (Window window : getWindows()) {
156            window.close();
157        }
158    }
159
160    /**
161     * Returns the requested UI attribute.<p>
162     *
163     * @param key the attribute key
164     *
165     * @return the attribute
166     */
167    public Serializable getAttribute(String key) {
168
169        return m_attributes.get(key);
170    }
171
172    /**
173     * Gets the display type from the time when the UI was initialized.<p>
174     *
175     * @return the display type
176     */
177    public CmsDisplayType getDisplayType() {
178
179        return m_displayType;
180    }
181
182    /**
183     * Returns the HTTP session.<p>
184     *
185     * @return the HTTP session
186     */
187    public HttpSession getHttpSession() {
188
189        return ((WrappedHttpSession)getSession().getSession()).getHttpSession();
190    }
191
192    /**
193     * Returns the last used offline project.<p>
194     *
195     * @return the last used offline project
196     */
197    public CmsProject getLastOfflineProject() {
198
199        return (CmsProject)getAttribute(LAST_OFFLINE_PROJECT);
200    }
201
202    /**
203     * Gets the request parameters with which the application was loaded.<p>
204     *
205     * @return the request parameters
206     */
207    public Multimap<String, String> getParameters() {
208
209        return CmsRequestUtil.getParameters(getPage().getLocation());
210    }
211
212    /**
213     * Returns the workplace settings.<p>
214     *
215     * @return the workplace settings
216     */
217    public CmsWorkplaceSettings getWorkplaceSettings() {
218
219        CmsWorkplaceSettings settings = (CmsWorkplaceSettings)getSession().getSession().getAttribute(
220            CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS);
221        if (settings == null) {
222            settings = CmsLoginHelper.initSiteAndProject(getCmsObject());
223            VaadinService.getCurrentRequest().getWrappedSession().setAttribute(
224                CmsWorkplaceManager.SESSION_WORKPLACE_SETTINGS,
225                settings);
226        }
227        return settings;
228    }
229
230    /**
231     * Tries to open a new browser window, and shows a warning if opening the window fails (usually because of popup blockers).<p>
232     *
233     * @param link the URL to open in the new window
234     * @param target the target window name
235     */
236    public void openPageOrWarn(String link, String target) {
237
238        openPageOrWarn(link, target, CmsVaadinUtils.getMessageText(org.opencms.ui.Messages.GUI_POPUP_BLOCKED_0));
239    }
240
241    /**
242     * Tries to open a new browser window, and shows a warning if opening the window fails (usually because of popup blockers).<p>
243     *
244     * @param link the URL to open in the new window
245     * @param target the target window name
246     * @param warning the warning to show if opening the window fails
247     */
248    public void openPageOrWarn(String link, String target, final String warning) {
249
250        m_windowExtension.open(link, target, new Runnable() {
251
252            public void run() {
253
254                Notification.show(warning, Type.ERROR_MESSAGE);
255            }
256        });
257    }
258
259    /**
260     * Reloads the current UI.<p>
261     */
262    public void reload() {
263
264        getPage().reload();
265    }
266
267    /**
268     * Sets an UI attribute.<p>
269     *
270     * @param key the attribute key
271     * @param value the attribute value
272     */
273    public void setAttribute(String key, Serializable value) {
274
275        m_attributes.put(key, value);
276    }
277
278    /**
279     * Replaces the ui content with a single dialog.<p>
280     *
281     * @param caption the caption
282     * @param dialog the dialog content
283     */
284    public void setContentToDialog(String caption, CmsBasicDialog dialog) {
285
286        setContent(new Label());
287        Window window = CmsBasicDialog.prepareWindow(DialogWidth.narrow);
288        window.setContent(dialog);
289        window.setCaption(caption);
290        window.setClosable(false);
291        addWindow(window);
292        window.center();
293    }
294
295    /**
296     * Replaces the ui content with a single dialog.<p>
297     *
298     * TODO: In the future this should only handle window creation, refactor dialog contents to CmsBasicDialog
299     *
300     * @param caption the caption
301     * @param component the dialog content
302     */
303    public void setContentToDialog(String caption, Component component) {
304
305        setContent(new Label());
306        Window window = CmsBasicDialog.prepareWindow(DialogWidth.narrow);
307        CmsBasicDialog dialog = new CmsBasicDialog();
308        VerticalLayout result = new VerticalLayout();
309        dialog.setContent(result);
310        window.setContent(dialog);
311        window.setCaption(caption);
312        window.setClosable(false);
313        addWindow(window);
314        window.center();
315        if (component instanceof I_CmsHasButtons) {
316            I_CmsHasButtons hasButtons = (I_CmsHasButtons)component;
317            for (Button button : hasButtons.getButtons()) {
318                dialog.addButton(button);
319            }
320
321        }
322        result.addComponent(component);
323
324    }
325
326    /**
327     * Displays an error message in a centered box.<p>
328     *
329     * @param error the error message to display
330     */
331    public void setError(String error) {
332
333        setContentToDialog("Error", new Label(error));
334    }
335
336    /**
337     * @see com.vaadin.ui.UI#init(com.vaadin.server.VaadinRequest)
338     */
339    @Override
340    protected void init(VaadinRequest request) {
341
342        m_displayType = CmsDisplayType.getDisplayType(getPage().getBrowserWindowWidth());
343    }
344
345}