001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.platform.forms.layout.actions;
018
019import java.io.Serializable;
020
021import javax.faces.application.FacesMessage;
022import javax.faces.context.FacesContext;
023import javax.servlet.http.HttpServletRequest;
024
025import org.nuxeo.ecm.platform.ui.web.rest.FancyNavigationHandler;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Session-scoped Seam component for layout related actions.
030 *
031 * @since 6.0
032 */
033public class NuxeoLayoutManagerBean implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    public static final String NAME = "nuxeoLayoutManagerBean";
038
039    protected Boolean devModeSet = null;
040
041    protected static String activatedMessage = "UI Development mode activated: "
042            + "keep the 'shift' key pressed and mouse hover elements in the page.";
043
044    protected static String deactivatedMessage = "UI Development mode deactivated";
045
046    public boolean isDevModeSet() {
047        if (devModeSet == null) {
048            setDevModeSet(false, Framework.isDevModeSet());
049            // maybe enable by default when dev mode is set
050            // setDevModeSet(Framework.isDevModeSet());
051        }
052        return devModeSet.booleanValue();
053    }
054
055    public void setDevModeSet(boolean activated) {
056        setDevModeSet(activated, true);
057    }
058
059    public void setDevModeSet(boolean activated, boolean addMessage) {
060        devModeSet = Boolean.valueOf(activated);
061        if (addMessage) {
062            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, activated ? activatedMessage
063                    : deactivatedMessage, null);
064            FacesContext faces = FacesContext.getCurrentInstance();
065            faces.addMessage(null, message);
066            // avoid redirect for message to be displayed
067            HttpServletRequest httpRequest = (HttpServletRequest) faces.getExternalContext().getRequest();
068            httpRequest.setAttribute(FancyNavigationHandler.DISABLE_REDIRECT_FOR_URL_REWRITE, Boolean.TRUE);
069        }
070    }
071
072}