001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base;
020
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.EnumSet;
024import java.util.Set;
025import org.jgrapes.webconsole.base.events.AddConletRequest;
026import org.jgrapes.webconsole.base.events.AddConletType;
027import org.jgrapes.webconsole.base.events.DisplayNotification;
028import org.jgrapes.webconsole.base.events.OpenModalDialog;
029import org.jgrapes.webconsole.base.events.RenderConlet;
030
031/**
032 * This interface provides web console component related constants. 
033 * Web console components need not implement this interface, not even 
034 * as a marker interface. They only have to expose a specific response 
035 * behavior to certain events. An overview of the event sequences is 
036 * provided by the package description. Make sure to read this 
037 * description first.
038 */
039public interface Conlet {
040
041    /**
042     * The render modes.
043     */
044    @SuppressWarnings("PMD.FieldNamingConventions")
045    enum RenderMode {
046        /**
047         * Provide content to display in a card on the overview tab
048         * (by firing a {@link RenderConlet} event). 
049         */
050        Preview,
051        /**
052         * Provide content to display in a tab of its own
053         * (by firing a {@link RenderConlet} event).
054         */
055        View,
056        /**
057         * Provide a dialog for modifying a conlet instance's
058         * properties, typically by opening a modal dialog
059         * (see {@link OpenModalDialog}).
060         */
061        Edit,
062        /**
063         * Provide help information, typically by opening a modal dialog
064         * (see {@link OpenModalDialog}) or using a notification
065         * (see {@link DisplayNotification}).
066         */
067        Help,
068        /**
069         * Provide content that is to be embedded in an area of the
070         * console page (see 
071         * {@link AddConletType#addPageContent(String, java.util.Map)}) 
072         * or in some other conlet's content (see {@link RenderConlet}).
073         * 
074         * Conlets that want to include another conlet's 
075         * "content" must generate an HTML element as container 
076         * (typically a `div` or `span`) with an attribute 
077         * "data-conlet-type" and classes "conlet conlet-content". 
078         * Additional attributes "data-conlet-..." 
079         * are considered properties of the conlet instance to be added 
080         * and are passed to the server with the {@link AddConletRequest}. 
081         * 
082         * Property "id" may be used if already known, but is usually 
083         * assigned later by the component implementation when the 
084         * component is rendered. Property "state" is reserved. 
085         * Its value is provided by the console. The handler for 
086         * {@link AddConletRequest} should pass the value of "state"
087         * back as attribute `data-conlet-state` of the root 
088         * (or first) element of the content. This allows the console 
089         * to match the content provided with the conlet content container
090         * that caused the request.
091         */
092        Content,
093        /** 
094         * Modifier, indicates that a {@link #Preview} may not be removed.
095         */
096        StickyPreview,
097        /** 
098         * Modifier, forces rendered view to be put in foreground. 
099         */
100        Foreground;
101
102        /**
103         * The basic modes (the modes without modifiers).
104         */
105        @SuppressWarnings("PMD.VariableNamingConventions")
106        public static final Set<RenderMode> basicModes
107            = Collections.unmodifiableSet(asSet(Preview, View, Edit, Help));
108
109        /**
110         * Utility method that creates a {@link Set} of render modes
111         * from enumerated values.
112         *
113         * @param modes the modes
114         * @return the sets the
115         */
116        public static Set<RenderMode> asSet(RenderMode... modes) {
117            return EnumSet.copyOf(Arrays.asList(modes));
118        }
119
120        /**
121         * Retrieves the modifiers.
122         */
123        public static Set<RenderMode> modifiers(Set<RenderMode> modes) {
124            Set<RenderMode> result = EnumSet.copyOf(modes);
125            result.removeAll(basicModes);
126            return result;
127        }
128
129        /**
130         * Adds the modifiers from the given set to this basic mode.
131         *
132         * @param modifiers the set with modifiers
133         * @return the augmented basic mode
134         */
135        public Set<RenderMode> addModifiers(Set<RenderMode> modifiers) {
136            Set<RenderMode> result = modifiers(modifiers);
137            result.add(this);
138            return result;
139        }
140    }
141
142}