classdef (Hidden) ReduceSystemTabMLOnline < handle
    % Reduce System Tab for Model Reduction App 
    % compatible with MATLAB Online
    
    % Author(s): Suat Gumussoy
    % Copyright 2016 The MathWorks, Inc.    
    
    properties(Access = public)
        Tool
        Tab
        Widgets             
    end
    
    methods
        function this = ReduceSystemTabMLOnline(Tool)
            this.Tab = matlab.ui.internal.toolstrip.Tab(getString(message('Control:mrtool:ReduceSystemTab')));
            this.Tab.Tag = 'ReduceSystemTab';
            this.Tool = Tool;
            createWidgets(this)
            addListeners(this)            
        end
        function delete(this)
        end        
        function Tab = getTab(this)
            Tab = this.Tab;
        end        
        function Widgets = getWidgets(this)
           Widgets = this.Widgets; 
        end                   
    end
    
    methods (Access = private)    
        %% Widgets
        function createWidgets(this)
            import matlab.ui.internal.toolstrip.*
            %% FILE SECTION WIDGETS
            % Strings
            FileStr = getString(message('Control:mrtool:FileSection'));
            OpenSessionStr = getString(message('Control:mrtool:OpenSession'));
            OpenSessionTooltip = getString(message('Control:mrtool:OpenSessionTooltip'));
            SaveSessionStr = getString(message('Control:mrtool:SaveSession'));
            SaveSessionTooltip = getString(message('Control:mrtool:SaveSessionTooltip'));
            % Icons
            OpenIcon = Icon.OPEN_24;
            SaveIcon = Icon.SAVE_24;
            % Section, Widgets  
            FileSection = Section(FileStr);
            add(this.Tab,FileSection);
            column1 = Column();
            column2 = Column();
                                  
            OpenSessionButton = Button(OpenSessionStr,OpenIcon);
            OpenSessionButton.Description = OpenSessionTooltip; 
            add(FileSection,column1);
            add(column1,OpenSessionButton)
                        
            SaveSessionButton = Button(SaveSessionStr,SaveIcon);
            SaveSessionButton.Description = SaveSessionTooltip; 
            add(FileSection,column2);
            add(column2,SaveSessionButton)

            this.Widgets.FileSection =  struct(...
                'OpenButton',OpenSessionButton, ...
                'SaveButton',SaveSessionButton);
            
            %% IMPORT/EXPORT SECTION WIDGETS
            % Strings
            ImportExportSectionStr = getString(message('Control:mrtool:ImportExportSection'));
            ImportStr = getString(message('Control:mrtool:ImportModel'));
            ImportTooltip = getString(message('Control:mrtool:ImportTooltip'));
            ExportStr = getString(message('Control:mrtool:ExportModel'));
            ExportTooltip = getString(message('Control:mrtool:ExportTooltip'));            
            % Icons
            ImportButtonIcon = Icon.IMPORT_24;
            ExportButtonIcon = Icon.EXPORT_24;
            % Section, Widgets
            ImportExportSection = Section(ImportExportSectionStr);
            add(this.Tab,ImportExportSection);
            column3 = Column();
            column4 = Column();                                                     
                        
            ImportButton = Button(ImportStr,ImportButtonIcon);
            ImportButton.Description = ImportTooltip;
            add(ImportExportSection,column3);
            add(column3,ImportButton)            
            
            ExportButton = Button(ExportStr,ExportButtonIcon);
            ExportButton.Description = ExportTooltip;
            add(ImportExportSection,column4);
            add(column4,ExportButton)                

            % Store widgets
            this.Widgets.ImportExportSection =  struct(...
                'ImportButton',ImportButton, ...
                'ExportButton',ExportButton);         
            
            %% MODEL REDUCTION TECHNIQUES WIDGETS
            % Strings
            TechniquesSectionStr = getString(message('Control:mrtool:TechniquesSection'));
            BalancedTruncationStr =  getString(message('Control:mrtool:BalancedTruncation'));
            BalancedTruncationToolTip =  getString(message('Control:mrtool:BalancedTruncationToolTip'));
            ModeSelectionStr =  getString(message('Control:mrtool:ModeSelection'));
            ModeSelectionToolTip = getString(message('Control:mrtool:ModeSelectionToolTip'));
            PoleZeroSimplificationStr =  getString(message('Control:mrtool:PoleZeroSimplification'));
            PoleZeroSimplificationToolTip = getString(message('Control:mrtool:PoleZeroSimplificationToolTip'));
            % Icons
            BalancedTruncationButtonIcon = mrtool.guiutil.getmrToolIcon('BalancedTruncation24');            
            ModeSelectionButtonIcon = mrtool.guiutil.getmrToolIcon('ModeSelection24');
            PoleZeroSimplificationButtonIcon = mrtool.guiutil.getmrToolIcon('PoleZeroSimplification24');
            % Section, Widgets                       
            TechniquesSection = Section(TechniquesSectionStr);
            add(this.Tab,TechniquesSection);
            column5 = Column();
            column6 = Column(); 
            column7 = Column(); 
                        
            BalancedTruncationButton = Button(BalancedTruncationStr,BalancedTruncationButtonIcon);
            BalancedTruncationButton.Description = BalancedTruncationToolTip;    
            add(TechniquesSection,column5);
            add(column5,BalancedTruncationButton);               
            
            ModeSelectionButton = Button(ModeSelectionStr,ModeSelectionButtonIcon);            
            ModeSelectionButton.Description = ModeSelectionToolTip;  
            add(TechniquesSection,column6);
            add(column6,ModeSelectionButton);
                        
            PoleZeroSimplificationButton = Button(PoleZeroSimplificationStr,PoleZeroSimplificationButtonIcon);            
            PoleZeroSimplificationButton.Description = PoleZeroSimplificationToolTip; 
            add(TechniquesSection,column7);
            add(column7,PoleZeroSimplificationButton);            

            % Store widgets
            this.Widgets.TechniquesSection =  struct(...
                'BalancedTruncationButton',BalancedTruncationButton, ...
                'ModeSelectionButton',ModeSelectionButton,...
                'PoleZeroSimplificationButton',PoleZeroSimplificationButton);                                 
        end
        function addListeners(this)
            % import and export model buttons
            addlistener(this.Widgets.ImportExportSection.ImportButton,'ButtonPushed', @(hSrc,hData) showImportDialog(this.Tool,this.Widgets.ImportExportSection.ImportButton));
            addlistener(this.Widgets.ImportExportSection.ExportButton,'ButtonPushed', @(hSrc,hData) showExportDialog(this.Tool,this.Widgets.ImportExportSection.ExportButton));             
            % open and save session button
            Widgets = this.Widgets.FileSection;
            addlistener(Widgets.SaveButton,'ButtonPushed', @(hSrc,hData) promptForSaveSession(this.Tool,false));
            addlistener(Widgets.OpenButton,'ButtonPushed', @(hSrc,hData) promptForLoadSession(this.Tool));              
            Widgets = this.Widgets.TechniquesSection;
            % balanced truncation button
            addlistener(Widgets.BalancedTruncationButton,'ButtonPushed', @(hSrc,hData) openBalancedTruncationTool(this.Tool,getSelectedModel(this.Tool)));
            % mode selection button
            addlistener(Widgets.ModeSelectionButton,'ButtonPushed', @(hSrc,hData) openModeSelectionTool(this.Tool,getSelectedModel(this.Tool)));
            % pole-zero simplification button
            addlistener(Widgets.PoleZeroSimplificationButton,'ButtonPushed', @(hSrc,hData) openPoleZeroSimplificationTool(this.Tool,getSelectedModel(this.Tool)));
            
            addlistener(this.Tool,'ModelsUpdated', @(es,ed) callbackModelsUpdated(this));
        end
        function callbackModelsUpdated(this)
            if isempty(this.Tool.Models) || ~any(isvalid(this.Tool.Models))
                % no models
                flagSaveExport = false;
                flagMethods = false;
            else
                Model = getSelectedModel(this.Tool);
                if isempty(Model)
                    % no valid model
                    flagSaveExport = true;
                    flagMethods = false;                    
                else
                    % there is a valid model                    
                    flagSaveExport = true;
                    flagMethods = true;
                end
            end            
            this.Widgets.FileSection.SaveButton.Enabled = flagSaveExport;
            this.Widgets.ImportExportSection.ExportButton.Enabled = flagSaveExport;
            
            this.Widgets.TechniquesSection.BalancedTruncationButton.Enabled = flagMethods;
            this.Widgets.TechniquesSection.ModeSelectionButton.Enabled = flagMethods;
            this.Widgets.TechniquesSection.PoleZeroSimplificationButton.Enabled = flagMethods;            
        end
    end    
end
