001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-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.core.events;
020
021import org.jgrapes.core.Channel;
022import org.jgrapes.core.ComponentType;
023import org.jgrapes.core.Components;
024import org.jgrapes.core.Event;
025
026/**
027 * Signals the removal of a component from the component tree.
028 * This event is fired on both the node's and the parent's
029 * channels.
030 */
031public class Detached extends Event<Void> {
032
033    private final ComponentType node;
034    private final ComponentType parent;
035
036    /**
037     * Creates a new event.
038     * 
039     * @param node the component being removed
040     * @param parent the component that the node is removed from
041     */
042    public Detached(ComponentType node, ComponentType parent) {
043        this.parent = parent;
044        this.node = node;
045    }
046
047    /**
048     * @return the node
049     */
050    public ComponentType node() {
051        return node;
052    }
053
054    /**
055     * @return the parent
056     */
057    public ComponentType parent() {
058        return parent;
059    }
060
061    /*
062     * (non-Javadoc)
063     * 
064     * @see java.lang.Object#toString()
065     */
066    @Override
067    public String toString() {
068        StringBuilder builder = new StringBuilder(50);
069        builder.append(Components.objectName(this))
070            .append(" [")
071            .append(parent)
072            .append(" <―/― ")
073            .append(node)
074            .append(", ");
075        if (channels().length > 0) {
076            builder.append("channels=");
077            builder.append(Channel.toString(channels()));
078        }
079        builder.append(']');
080        return builder.toString();
081    }
082
083}