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.HashSet;
024import java.util.Set;
025
026/**
027 * This interface provides web console component related constants. 
028 * Web console components need not implement this interface, not even 
029 * as a marker interface. They only have to expose a specific response 
030 * behavior to certain events. An overview of the event sequences is 
031 * provided by the package description. Make sure to read this 
032 * description first.
033 */
034public interface Conlet {
035
036    /**
037     * The render modes.
038     */
039    enum RenderMode {
040        Preview, View, Edit, Help,
041        /** Modifier, indicates that a {@link #Preview} may not be removed. */
042        StickyPreview,
043        /** Modifier, forces rendered view to be put in foreground. */
044        Foreground;
045
046        /**
047         * Utility method that creates a {@link Set} of render modes
048         * from enumerated values.
049         *
050         * @param modes the modes
051         * @return the sets the
052         */
053        public static Set<RenderMode> asSet(RenderMode... modes) {
054            return new HashSet<>(Arrays.asList(modes));
055        }
056
057        /**
058         * The basic modes (the modes without modifiers).
059         */
060        public static Set<RenderMode> basicModes = Collections.unmodifiableSet(
061            asSet(RenderMode.Preview, RenderMode.View, RenderMode.Edit,
062                RenderMode.Help));
063
064        /**
065         * Retrieves the modifiers.
066         */
067        public static Set<RenderMode> modifiers(Set<RenderMode> modes) {
068            Set<RenderMode> result = new HashSet<>(modes);
069            result.removeAll(basicModes);
070            return result;
071        }
072
073        /**
074         * Adds the modifiers from the given set to this basic mode.
075         *
076         * @param modifiers the set with modifiers
077         * @return the augmented basic mode
078         */
079        public Set<RenderMode> addModifiers(Set<RenderMode> modifiers) {
080            Set<RenderMode> result = modifiers(modifiers);
081            result.add(this);
082            return result;
083        }
084    }
085
086}