classdef ProfilingStyler < handle
% Model styling to highlight profiling results
    
%   Copyright 2015 The MathWorks, Inc.

    properties(Access = private) 
        BlockStyle;
        ModelStyle;
        ModelSelectorName;
        BlockSelectorName;
        Styler;
    end
    
    methods
       
        function style = get.ModelStyle(this)
            if isempty(this.ModelStyle)
                % Create the style
                style = diagram.style.Style;
                style.set('FillColor', [0.86, 0.86, 1]);
                style.set('FillStyle', 'Solid');
                style.set('StrokeColor', [0.0 0.0 0.0 1.0]);
                this.ModelStyle = style;
            end
            style = this.ModelStyle;
        end
       
        function style = get.BlockStyle(this)
            if isempty(this.BlockStyle)
                % Create the style
                style = diagram.style.Style;
                style.set('FillColor', [0.69, 0.69, 1]);
                style.set('FillStyle', 'Solid');
                style.set('StrokeColor', [0.0 0.0 0.0 1.0]);
                this.BlockStyle = style;
            end
            style = this.BlockStyle;
        end
        
    end
    
    methods (Hidden=true, Access=public)
        
        function delete(this)
            if ~isempty(this.Styler)
                % Remove all highlighting belonging to this instance
                destroy(this.Styler);
            end
        end
        
    end
                 
    methods(Access = public)
        
        function applyHighlighting(this, itemsToStyle, itemStyling)
            
            cellArrayItemsToStyle = num2cell(itemsToStyle(:)');
            
            % Model styling
            lModelSelectorName = this.ModelSelectorName;
            modelIdx = strcmp(itemStyling, 'model');
            if any(modelIdx)
                applyClass(this.Styler, cellArrayItemsToStyle(modelIdx),...
                           lModelSelectorName);
            end
            
            % Block styling
            lBlockSelectorName = this.BlockSelectorName;
            blockIdx = strcmp(itemStyling, 'block');
            if any(blockIdx)
                applyClass(this.Styler, cellArrayItemsToStyle(blockIdx),...
                           lBlockSelectorName);
            end
        
        end
                
        function this = ProfilingStyler
            
            [~, uniqueName] = fileparts(tempname);
            stylerName = sprintf('MathWorks.ProfilingHighlight%s', uniqueName);
            
            diagram.style.createStyler(stylerName);
            this.Styler = diagram.style.getStyler(stylerName);
            
            % Model styling
            lModelSelectorName = sprintf('ModelProfiling.%s', uniqueName); 
            lModelSelector = diagram.style.ClassSelector(lModelSelectorName);
            addRule(this.Styler, this.ModelStyle, lModelSelector);
            this.ModelSelectorName = lModelSelectorName;
            
            % Block styling
            lBlockSelectorName = sprintf('BlockProfiling.%s', uniqueName); 
            this.BlockSelectorName = lBlockSelectorName;
            lBlockSelector = diagram.style.ClassSelector(lBlockSelectorName);
            addRule(this.Styler, this.BlockStyle, lBlockSelector);
        end        

    
    end
end

