classdef Icon < handle
    % Icon component.
    %
    % Example:
    
    % Copyright 2010-2015 The MathWorks, Inc.
    
    % ----------------------------------------------------------------------------
    
    properties (Constant, Hidden, Access = public)
        % Path to resources directory for icons
        Path = fullfile(matlabroot, 'toolbox', 'control', ...
            'ctrlguis', '+ctrlguis', '+csdesignerapp', 'resources');
    end
    
    % ----------------------------------------------------------------------------
    methods (Static)
        function icon = getIcon(filename, Version)
            if nargin == 1
                % Default to version 2
                Version = 2;
            end
            % Helper method to get standard icons.
            iconfile = fullfile(ctrlguis.csdesignerapp.Icon.Path, filename);
            if  Version == 2
                icon = toolpack.component.Icon(iconfile);
            elseif Version == 3
                icon = matlab.ui.internal.toolstrip.Icon(iconfile);
            else
                icon = [];
            end
        end
        
        function showIcons
            % Helper method to show all standard icons on a figure window.
            clsName = 'ctrlguis.csdesignerapp.Icon';
            ctrlguis.csdesignerapp.Icon.showIcons_(clsName);
        end
    end
    
    % Standard 16x16 icons
    methods (Sealed, Static)
        function icon = ZOOM_IN_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('ZoomIn_16x16.png');
        end
        function icon = ZOOM_OUT_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('ZoomOut_16x16.png');
        end
        function icon = PAN_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Pan_16x16.png');
        end
        function icon = CLEAR_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Clear_16x16.png');
        end
        function icon = REAL_ZERO_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('RealZero_16x16.png');
        end
        function icon = COMPLEX_ZERO_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('ComplexZero_16x16.png');
        end
        function icon = REAL_POLE_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('RealPole_16x16.png');
        end
        function icon = COMPLEX_POLE_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('ComplexPole_16x16.png');
        end
        function icon = ARROW_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Arrow_16x16.png');
        end
        function icon = MULTIMODEL_16
            icon = ctrlguis.csdesignerapp.Icon.getIcon('MultimodelConfiguration_16px.png');
        end
    end
    
    % Standard 60x40 icons
    methods (Sealed, Static)
        function icon = BODE_EDITOR_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('BodeEditor_60x40.png');
        end
        function icon = NICHOLS_EDITOR_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('NicholsEditor_60x40.png');
        end
        function icon = RLOCUS_EDITOR_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('RLocusEditor_60x40.png');
        end
        function icon = IMC_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('IMCTuning_60x40.png');
        end
        function icon = PID_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('PIDTuning_60x40.png');
        end
        function icon = PID_CLASSIC_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('PIDCLassicTuning_60x40.png');
        end
        function icon = OPTIMIZATION_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('OptimizationTuning_60x40.png');
        end
        function icon = LQG_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('LQGTuning_60x40.png');
        end
        function icon = LOOPSHAPE_TUNING_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('LoopShapeTuning_60x40.png');
        end
    end
    
    % Architecture icons
    methods (Sealed, Static)
        function icon = CONFIGURATION_1
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config1.png');
        end
        function icon = CONFIGURATION_1_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config1_60x40.png');
        end
        function icon = CONFIGURATION_1_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig1Thumb.png');
        end
        function icon = CONFIGURATION_2
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config2.png');
        end
        function icon = CONFIGURATION_2_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config2_60x40.png');
        end
        function icon = CONFIGURATION_2_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig2Thumb.png');
        end
        function icon = CONFIGURATION_3
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config3.png');
        end
        function icon = CONFIGURATION_3_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config3_60x40.png');
        end
        function icon = CONFIGURATION_3_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig3Thumb.png');
        end
        function icon = CONFIGURATION_4
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config4.png');
        end
        function icon = CONFIGURATION_4_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config4_60x40.png');
        end
        function icon = CONFIGURATION_4_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig4Thumb.png');
        end
        function icon = CONFIGURATION_5
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config5.png');
        end
        function icon = CONFIGURATION_5_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config5_60x40.png');
        end
        function icon = CONFIGURATION_5_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig5Thumb.png');
        end
        function icon = CONFIGURATION_6
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config6.png');
        end
        function icon = CONFIGURATION_6_60x40
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Config6_60x40.png');
        end
        function icon = CONFIGURATION_6_THUMB
            icon = ctrlguis.csdesignerapp.Icon.getIcon('SISOConfig6Thumb.png');
        end
    end
    
    % Home tab icons
    methods (Sealed, Static)
        function icon = SIGNAL_UP
            icon = ctrlguis.csdesignerapp.Icon.getIcon('UpArrow_16.png');
        end
        function icon = SIGNAL_DOWN
            icon = ctrlguis.csdesignerapp.Icon.getIcon('DownArrow_16.png');
        end
        function icon = ADD_RESPONSES
            icon = ctrlguis.csdesignerapp.Icon.getIcon('AddResponses_24px.png');
        end
        function icon = EDIT_ARCHITECTURE
            icon = ctrlguis.csdesignerapp.Icon.getIcon('Edit_Architecture_24px.png');
        end
        function icon = LINEARIZATION
            icon = ctrlguis.csdesignerapp.Icon.getIcon('ConfigureLinearizationSettings_16px.png');
        end
        function icon = TUNING_METHODS
            icon = ctrlguis.csdesignerapp.Icon.getIcon('TuningMethods_24px.png');
        end
        function icon = UPDATE_MODEL
            icon = ctrlguis.csdesignerapp.Icon.getIcon('UpdateModel_24px.png');
        end
        function icon = COMPARE_DESIGN
            icon = ctrlguis.csdesignerapp.Icon.getIcon('CompareDesign_24px.png');
        end
        function icon = STORE_DESIGN
            icon = ctrlguis.csdesignerapp.Icon.getIcon('StoreDesign_24px.png');
        end
        function icon = RETRIEVE_DESIGN
            icon = ctrlguis.csdesignerapp.Icon.getIcon('RetrieveDesign_24px.png');
        end
        function icon = MULTIMODEL_CONFIGURATION
            icon = ctrlguis.csdesignerapp.Icon.getIcon('MultimodelConfiguration_24px.png');
        end
    end
    % ----------------------------------------------------------------------------
    methods (Static, Access = protected)
        function showIcons_(clsName)
            % Find all static methods returning an icon.
            info = meta.class.fromName(clsName);
            list = info.MethodList;
            [~, I] = sort({list.Name});
            list = list(I);
            
            mthd = {};
            for k = 1:length(list)
                m = list(k);
                if m.Static && isempty(m.InputNames) && ...
                        ~isempty(m.OutputNames) && strcmp(m.OutputNames, 'icon')
                    % Static methods returning icons have no inputs and have "icon"
                    % as the output argument.
                    mthd{end+1} = m.Name;
                end
            end
            
            % Add icons to a panel
            len = ceil(length(mthd)/5); % 5 columns and as many rows as needed.
            f = '';
            for k = 1:len
                f = sprintf('%sf:d:g,',f);
            end
            f(end) = ''; % Remove last ','.
            panel = toolpack.component.TSPanel('f:d:g,f:d:g,f:d:g,f:d:g,f:d:g',f);
            for k = 1:length(mthd)
                try
                    icon = eval([clsName, '.' mthd{k}]);
                    label = toolpack.component.TSLabel(mthd{k}, icon);
                catch E
                    % Icon not implemented yet.
                    fprintf('%s\n', E.message);
                    label = toolpack.component.TSLabel(mthd{k});
                end
                i = rem(k-1,5); % col
                j = (k-i-1)/5;  % row
                str = sprintf('xy(%d,%d)', i+1, j+1);
                panel.add(label,str);
            end
            
            % Show icons on a panel
            fig = figure(...
                'IntegerHandle','off', ...
                'Menubar','None',...
                'Toolbar','None',...
                'Name','Icon Catalog. To use: toolpack.component.Icon.<NAME>', ...
                'NumberTitle','off', ...
                'Visible','on', ...
                'Resize', 'on', ...
                'Units','pixels');
            set(fig, 'ResizeFcn', @localFigureResize)
            pos = get(fig, 'Position');
            scroll = javaObjectEDT('javax.swing.JScrollPane', panel.Peer);
            [~, container] = matlab.ui.internal.JavaMigrationTools.suppressedJavaComponent(scroll,[0 0 pos(3) pos(4)],fig);
            set(fig, 'UserData', {panel, container});
        end
    end
end

% ----------------------------------------------------------------------------
function localFigureResize(fig,~)
% Handle figure resize
pos = get(fig, 'Position');
ud  = get(fig, 'UserData');
container = ud{2};
set(container, 'Position', [0 0 pos(3) pos(4)]);
end
