% MJSComputeCloudDisplayer - Displayer subclass to display MJSComputeCloud objects.

%   Copyright 2011-2017 The MathWorks, Inc.

classdef ( Hidden, Sealed ) MJSComputeCloudDisplayer < parallel.internal.cluster.ClusterDisplayer

    properties (Constant, GetAccess = private)
        VectorDisplayProps = {'Type', 'Profile' 'Name'};
        VectorTableColumns = { ...
            % 'Title', 'Resizeable', 'MinimumWidth'
            '',        true,         length('1'); ...% This is the index column for the vector display
            'Type',    false,        length('MJSComputeCloud'); ...
            'Profile', true,         length('Profile'); ...
            'Name',    true,         length('Name'); ...
        }
    end

    methods
        function obj = MJSComputeCloudDisplayer(someObj)
            obj@parallel.internal.cluster.ClusterDisplayer(someObj, class(someObj));
        end
    end

    methods (Access = protected)
        
        function doSingleDisplay(obj, toDisp)
            obj.DisplayHelper.displayMainHeading( '%s %s', obj.formatDocLink(class(toDisp)), getString(message('parallel:cluster:Cluster')) ); 
            
            obj.DisplayHelper.displaySubHeading(getString(message('parallel:cluster:Properties')));
            obj.displaySpecificItems(toDisp);
            
            % The state has already been retrieved in the
            % displaySpecificItems call above. Rather than making another
            % remote call we will simply extract it here.
            clusterState = toDisp.hGetCachedClusterState();
            
            % We specifically do not want to display the associated jobs if
            % the cluster is not in an available state.
            if clusterState.isStateClusterAvailable()
                obj.DisplayHelper.displaySubHeading('%s', char(obj.hGetJobsLink(toDisp, getString(message('parallel:cluster:AssociatedJobs')) )));
                [p, q, r, f] = obj.getNumJobs(toDisp);
                obj.DisplayHelper.displayProperty('Number Pending',  p);
                obj.DisplayHelper.displayProperty('Number Queued',   q);
                obj.DisplayHelper.displayProperty('Number Running',  r);
                obj.DisplayHelper.displayProperty('Number Finished', f);
            end
        end
        
        function doVectorDisplay(obj, toDisp)
            import parallel.internal.display.Displayer
            import parallel.internal.cluster.MJSComputeCloudDisplayer

            tableData = cell(numel(toDisp), size(MJSComputeCloudDisplayer.VectorTableColumns, 1));
            for ii = 1:numel(toDisp)
                if isvalid(toDisp(ii))
                    propsToDisp = Displayer.getDisplayPropsAsStruct(toDisp(ii), MJSComputeCloudDisplayer.VectorDisplayProps);
                    tableData(ii, :) = {...
                        toDisp(ii).hGetLink(ii), ...
                        propsToDisp.Type, ...
                        propsToDisp.Profile, ...
                        propsToDisp.Name};
                else
                    tableData(ii, :) = {parallel.internal.display.AbstractDisplayer.DeletedString, '', ''};
                end
            end

            obj.DisplayHelper.displayDimensionHeading(size(toDisp), obj.formatDocLink(class(toDisp)));
            obj.DisplayHelper.displayTable(MJSComputeCloudDisplayer.VectorTableColumns, tableData, obj.DisplayHelper.DoNotDisplayIndexColumn);
        end
    end
    
    methods( Access = protected )
        function [p, q, r, f] = getNumJobs(~,toDisp)
            [p, q, r, f] = toDisp.hEnumerateJobsInStates();
        end
    end

end
