% Displayer - base class handling common display functionality
%   Assumes objects support hGetDisplayItems and hGetLink.

% Copyright 2011-2018 The MathWorks, Inc.

classdef ( Hidden ) Displayer < parallel.internal.display.AbstractDisplayer
    properties ( Constant )
        DisplayObjectFunction = 'parallel.internal.display.Displayer.displayObject'
    end
    methods
        function obj = Displayer( someObj, requiredType )
            obj@parallel.internal.display.AbstractDisplayer( ...
                class( someObj ), requiredType );
            obj.errorIfWrongType( someObj );
        end
    end

    methods ( Static, Access = protected )
        function propStruct = getDisplayPropsAsStruct( toDisp, propNames, varargin )
            % Gets the specified properties from the toDisp object and
            % returns them as a struct whose fieldnames are the property
            % names.
            % Default version of getDisplayPropsAsStruct ignores the varargin.
            vals = toDisp.hGetDisplayPropertiesNoError( propNames );
            propStruct = cell2struct( vals, propNames, 2 );
        end
    end

    methods ( Access = protected )
        function displaySpecificItems(obj, toDisp)
            % Gets the specified properties from the toDisp object and
            % displays them.
            if ~isscalar(toDisp)
                error(message('parallel:cluster:DisplaySpecificItemsNotScalar'));
            end
            diFactory = parallel.internal.display.DisplayableItemFactory(obj.DisplayHelper);
            [objectPropertyMap, propNames] = toDisp.hGetDisplayItems(diFactory);
            if isempty(propNames)
                return;
            end
            for ii = 1:numel(propNames)
                displayValue = objectPropertyMap(propNames{ii});
                displayValue.displayInMATLAB(propNames{ii});
            end
        end

        function link = hGetLink(obj, toDisp,  displayValue, optionalMemento)
            if obj.ShowLinks
                if (nargin < 4)
                    link = hGetLink(toDisp,  displayValue);
                else
                    link = hGetLink(toDisp,  displayValue, optionalMemento);
                end
            else
                % Getting a link will always return an HTMLDisplayType,
                % but if we are not building a hyperlink, we do not
                % include the matlab command.
                link = parallel.internal.display.HTMLDisplayType(displayValue);
            end
        end
    end

    methods( Static )
        % Display object when the hyperlink is clicked in the display.
        function displayObject(serializedMemento)
            try
                disp(createObjectFromMemento(parallel.internal.display.Memento.deserialize(serializedMemento)))
            catch err
                if feature( 'hotlinks')
                    helpString1 = '<a href="matlab: helpPopup parallel.Cluster">parallel.Cluster</a>';
                    helpString2 = '<a href="matlab: helpPopup parallel.Job">parallel.Job</a>';
                    helpString3 = '<a href="matlab: helpPopup parallel.Task">parallel.Task</a>';
                else
                    helpString1 =  'parallel.Cluster';
                    helpString2 =  'parallel.Job';
                    helpString3 =  'parallel.Task';
                end
                error(message('parallel:cluster:BasicLinkDispFailure', helpString1, helpString2, helpString3));
            end
        end
    end
end
