classdef TheaterPlotter < handle & matlab.mixin.Heterogeneous
%

%   Copyright 2017 The MathWorks, Inc.
    
    properties
        %Tag   Tag to associate with the plotter
        %   Tag to associate with the plotter, specified as a
        %   character vector.  Tags provide a way to identify
        %   plotter objects, for example when searching using 
        %   findPlotter.  The default value is 'PlotterN', 
        %   where N is an integer that corresponds to the Nth
        %   plotter associated with the theaterPlot.
        Tag
    end
    
    properties (Dependent)
        %DisplayName  The name of the entry to show in the legend
        %   The name of the entry to show in the legend. If no name is
        %   specified, no entry is shown.
        DisplayName
    end
    
    properties (Hidden)
        ParentPlot
    end
    
    methods
        function obj = TheaterPlotter(tp,varargin)
            if nargin > 1
                [varargin{:}] = convertStringsToChars(varargin{:});
            end
            obj.ParentPlot = tp;
            addPlotter(tp,obj);
            obj.Tag = ['Plotter' num2str(getTotalPlotters(tp))];
            hAxes = tp.Parent;
            if ishghandle(hAxes)
                p = inputParser;
                initParser(obj, p);
                parse(p,varargin{:});
                pr = p.Results;
                if isempty(pr.Tag)
                    validateattributes(pr.Tag,{'char'},{},class(obj),'Tag');
                else
                    validateattributes(pr.Tag,{'char'},{'vector'},class(obj),'Tag');
                end
                
                if isempty(pr.DisplayName)
                    validateattributes(pr.DisplayName,{'char'},{},...
                        class(obj),'DisplayName');
                else
                    validateattributes(pr.DisplayName,{'char'},{'vector'},...
                        class(obj),'DisplayName');
                end
                
                obj.Tag = pr.Tag;
                set(hAxes.Legend, 'AutoUpdate', 'on');
                createGraphics(obj,hAxes,p);
                set(hAxes.Legend, 'AutoUpdate', 'off');
            else
                error(message('shared_radarfusion:theaterPlot:InvalidAxes'));
            end
        end
        
        function value = get.DisplayName(obj)
            value = getDisplayName(obj);
        end
        
        function set.DisplayName(obj,value)
            setDisplayName(obj,value);
        end
        
        function delete(obj)
            tp = obj.ParentPlot;
            
            % The TP can be destroyed first, which means there's nothing
            % to do here.
            if isvalid(tp)
                removePlotter(tp, obj);
                hAxes = tp.Parent;
                if ishghandle(hAxes)
                    set(hAxes.Legend, 'AutoUpdate', 'on');
                    deleteGraphics(obj);
                    set(hAxes.Legend, 'AutoUpdate', 'off');
                end
            end
        end
    end
    
    methods (Sealed)
        function b = eq(obj, otherObj)
            b = eq@handle(obj, otherObj);
        end
    end
        
    methods (Access = protected)
        function initParser(obj, p)
            addParameter(p,'DisplayName','');
            addParameter(p,'Tag',obj.Tag);
        end
    end
        
    methods (Abstract, Access = protected)
        value = getPrimaryObject(obj)
        createGraphics(obj, hAxes, pr)
        deleteGraphics(obj)
    end
        
    methods (Access = protected)
        function value = getDisplayName(obj)
            hObject = getPrimaryObject(obj);
            if ishghandle(hObject) && strcmp(hObject.HandleVisibility,'on')
                value = hObject.DisplayName;
            else
                value = '';
            end
        end
    end
    
    methods (Hidden)
        function setDisplayName(obj, value)
            hObject = getPrimaryObject(obj);
            if ishghandle(hObject)
                tp = obj.ParentPlot;
                hAxes = tp.Parent;
                hAxes.Legend.AutoUpdate = 'on';
                hObject.DisplayName = value;
                hObject.HandleVisibility = 'on';
                hAxes.Legend.AutoUpdate = 'off';
            end
        end
    end
end