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 addition of a component (or subtree) to the component tree.
028 */
029public class Attached extends Event<Void> {
030
031    private final ComponentType node;
032    private final ComponentType parent;
033
034    /**
035     * Creates a new event. The event is fired on both the
036     * node's and the parent's channel. If the channels are
037     * equal, the event is sent only once. If either component
038     * doesn't have a channel, the event is sent on the
039     * broadcast channel.
040     * 
041     * @param node the component being attached
042     * @param parent the component that the node is attached to
043     */
044    public Attached(ComponentType node, ComponentType parent) {
045        this.node = node;
046        this.parent = parent;
047    }
048
049    /**
050     * Return the node that has been attached.
051     * 
052     * @return the node
053     */
054    public ComponentType node() {
055        return node;
056    }
057
058    /**
059     * Return the parent component. When the root node is added to the 
060     * component tree, the parent is <code>null</code>.
061     * 
062     * @return the parent or <code>null</code>
063     */
064    public ComponentType parent() {
065        return parent;
066    }
067
068    /*
069     * (non-Javadoc)
070     * 
071     * @see java.lang.Object#toString()
072     */
073    @Override
074    public String toString() {
075        StringBuilder builder = new StringBuilder(50);
076        builder.append(Components.objectName(this))
077            .append(" [");
078        if (parent == null) {
079            builder.append("ROOT");
080        } else {
081            builder.append(parent);
082        }
083        builder.append(" <―― ")
084            .append(node)
085            .append(", ");
086        if (channels().length > 0) {
087            builder.append("channels=");
088            builder.append(Channel.toString(channels()));
089        }
090        builder.append(']');
091        return builder.toString();
092    }
093
094}