001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: BuiltinModes.java 28460 2008-01-03 15:34:05Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.forms.layout.api;
023
024/**
025 * List of built-in modes.
026 *
027 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
028 */
029public class BuiltinModes {
030
031    public static final String ANY = "any";
032
033    public static final String VIEW = "view";
034
035    public static final String EDIT = "edit";
036
037    public static final String BULK_EDIT = "bulkEdit";
038
039    public static final String CREATE = "create";
040
041    public static final String SEARCH = "search";
042
043    public static final String SUMMARY = "summary";
044
045    /**
046     * @since 5.4.2
047     */
048    public static final String CSV = "csv";
049
050    /**
051     * @since 5.4.2
052     */
053    public static final String PDF = "pdf";
054
055    /**
056     * @since 5.4.2
057     */
058    public static final String PLAIN = "plain";
059
060    /**
061     * @since 6.0
062     */
063    public static final String DEV = "dev";
064
065    private BuiltinModes() {
066    }
067
068    /**
069     * Returns true if given layout mode is mapped by default to the edit widget mode.
070     */
071    public static boolean isBoundToEditMode(String layoutMode) {
072        if (layoutMode != null) {
073            if (layoutMode.startsWith(CREATE) || layoutMode.startsWith(EDIT) || layoutMode.startsWith(SEARCH)
074                    || layoutMode.startsWith(BULK_EDIT)) {
075                return true;
076            }
077        }
078        return false;
079    }
080
081    /**
082     * Returns the default mode to use for a widget, given the layout mode.
083     * <p>
084     * Returns {@link BuiltinWidgetModes#EDIT} for all modes bound to edit, {@link BuiltinWidgetModes#VIEW} for modes
085     * {@link #VIEW}, {@link #HEADER} and {@link #SUMMARY}. {@link #PDF} and {@link #CSV} are respectively bound to
086     * {@link BuiltinWidgetModes#PDF} and {@link BuiltinWidgetModes#CSV}. In other cases, returns
087     * {@link BuiltinWidgetModes#PLAIN}.
088     * <p>
089     * This method is not called when mode is explicitely set on the widget.
090     */
091    public static String getWidgetModeFromLayoutMode(String layoutMode) {
092        if (layoutMode != null) {
093            if (isBoundToEditMode(layoutMode)) {
094                return BuiltinWidgetModes.EDIT;
095            } else if (layoutMode.startsWith(VIEW) || layoutMode.startsWith(SUMMARY)) {
096                return BuiltinWidgetModes.VIEW;
097            } else if (layoutMode.startsWith(CSV)) {
098                return BuiltinWidgetModes.CSV;
099            } else if (layoutMode.startsWith(PDF)) {
100                return BuiltinWidgetModes.PDF;
101            }
102        }
103        return BuiltinWidgetModes.PLAIN;
104    }
105
106}