classdef Connector < handle
    %Creates a line connecting two ports attached to tool components.
    %
    %  L = toolpack.Connector(Source,Destination) creates a Connector object
    %  that connects two Port objects by channeling the event generated by
    %  Source to Destination.
    %
    %  The following properties and methods are defined by this class. For
    %  inherited properties and methods, type DOC followed by the full
    %  classname.
    %
    %  Connector Properties:
    %     Name                     - name of the connector
    %     Source                   - source port
    %     Destination              - destination port
    %
    %  Connector Methods:
    %     N/A
    %
    %  See also Port.
    
    % Author(s): Murad Abu-Khalaf , October 8, 2010
    % Revised:
    % Copyright 2010-2011 The MathWorks, Inc.
    
    
    %% ------ PROPERTIES
    properties (Access = public)        
        % Handle to Source port
        Source
        
        % Handle to Destination port
        Destination
    end
    
    properties (SetAccess = public, GetAccess = public)
        % Name of the connector
        Name
    end
    
    properties (Access = private)
        % Listeners to the tool component model (WHY?)
        SourcePortListeners
    end
    
    properties (Access = protected)
        % Class version
        Version = toolpack.ver();
    end
    
    %% ------ CONSTRUCTION
    methods
        function this = Connector(varargin)
            % Constructor requiring the arguments Source, Destination.
            if nargin > 1
                this.Source = varargin{1};
                this.Destination = varargin{2};
                lsnr1 = event.listener(this.Source, 'PortChanged', @this.transmit);
                lsnr2 = event.listener(this.Source,'ObjectBeingDestroyed',@(x,y) this.delete);
                lsnr3 = event.listener(this.Destination,'ObjectBeingDestroyed',@(x,y) this.delete);
                this.SourcePortListeners = [lsnr1;lsnr2;lsnr3];
            else
                this.Source = {};
                this.Destination = {};
            end
        end
        function delete(this) %#ok<MANU>
%             disp([class(this) ' is deleting...']);
        end
    end
    
    %% ------ SERILIZATION
    methods
        % Never saved or loaded like AbstractGraphicalComponent??
        %         function S = saveobj(obj) %#ok<STOUT,MANU>
        %         end
        %
        %         function obj = reload(obj, S) %#ok<INUSD>
        %         end
    end
        
    %% ------ Execution
    methods (Access = private, Sealed)
        
        function transmit(this, varargin)
            % Exchange port data between source and destination ports.
            this.Destination.Data = this.Source.Data;
        end
        
    end
    methods (Access = public)
        
        function r = getSourcePort(this, varargin)
            % Returns the source port
            r = this.Source;
        end
        
        function r = getDestinationPort(this, varargin)
            % Returns the destination port
            r = this.Destination;
        end
        
    end
end