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.events;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Set;
024import org.jgrapes.core.Event;
025import org.jgrapes.webconsole.base.Conlet.RenderMode;
026import org.jgrapes.webconsole.base.RenderSupport;
027
028/**
029 * A notification that a conlet view was deleted in the browser.
030 */
031@SuppressWarnings("PMD.DataClass")
032public class ConletDeleted extends Event<Void> {
033
034    private final RenderSupport renderSupport;
035    private final String conletId;
036    private final Set<RenderMode> renderModes;
037    private Map<? extends Object, ? extends Object> properties;
038
039    /**
040     * Creates a new event.
041     *
042     * @param renderSupport the render support from the web console in case
043     * the response requires it
044     * @param conletId the web console component model that the notification is
045     * directed at
046     * @param renderModes the kind of views that have been deleted. If
047     * empty, the last view has been deleted, i.e. the conlet is no longer
048     * used in the browser.
049     */
050    public ConletDeleted(RenderSupport renderSupport,
051            String conletId, Set<RenderMode> renderModes) {
052        this.renderSupport = renderSupport;
053        this.conletId = conletId;
054        this.renderModes = renderModes;
055        this.properties = new HashMap<>();
056    }
057
058    /**
059     * Creates a new event.
060     *
061     * @param renderSupport the render support from the web console in case
062     * the response requires it
063     * @param conletId the web console component model that the notification is
064     * directed at
065     * @param renderModes the kind of views that have been deleted. If
066     * empty, the last view has been deleted, i.e. the conlet is no longer
067     * used in the browser.
068     * @param properties optional values for properties of the 
069     * web console component instance
070     */
071    public ConletDeleted(RenderSupport renderSupport,
072            String conletId, Set<RenderMode> renderModes,
073            Map<? extends Object, ? extends Object> properties) {
074        this.renderSupport = renderSupport;
075        this.conletId = conletId;
076        this.renderModes = renderModes;
077        this.properties = new HashMap<>(properties);
078    }
079
080    /**
081     * Returns the render support.
082     * 
083     * @return the render support
084     */
085    public RenderSupport renderSupport() {
086        return renderSupport;
087    }
088
089    /**
090     * Returns the web console component id.
091     * 
092     * @return the web console component id
093     */
094    public String conletId() {
095        return conletId;
096    }
097
098    /**
099     * Returns the render modes that have been deleted. An empty
100     * set indicates that all views have been deleted, i.e.
101     * the conlet is no longer used in the browser.
102     *
103     * @return the render modes
104     */
105    public Set<RenderMode> renderModes() {
106        return renderModes;
107    }
108
109    /**
110     * Returns the properties. Every event returns a mutable map,
111     * thus allowing event handlers to modify the map even if
112     * none was passed to the constructor.
113     */
114    public Map<Object, Object> properties() {
115        if (properties == null) {
116            properties = new HashMap<>();
117        }
118        @SuppressWarnings("unchecked")
119        Map<Object, Object> props = (Map<Object, Object>) properties;
120        return props;
121    }
122}