classdef(Hidden,Sealed) PPTab < internal.EconModeler.Tabs.AbstractContextualTab
    %PPTAB Tab used to control the PP Tests in the econModeler App
    
    %Copyright 2017-2018 The MathWorks, Inc.
    
    properties (Access = protected)
        LagSpinner
        SigSpinner
        ModelDropDown
        TestStatDropDown
        AddButton
        ClearButton
        Listener
    end
    
    methods (Access = ?internal.EconModeler.ContextualTabList)
        function this = PPTab(session)
            import matlab.ui.internal.toolstrip.*
            
            this@internal.EconModeler.Tabs.AbstractContextualTab();
            
            %Create the Parameter Section
            parametersSection = Section(getString(message('econ:econModeler:ParametersSection')));
            parametersSection.Tag = 'parametersSection';
            this.Tab.add(parametersSection);
            
            % Lags and Significance Level Labels
            col1 = parametersSection.addColumn('HorizontalAlignment','right');
            lagLabel = Label(getString(message('econ:econModeler:NumberOfLags')));
            lagLabel.Tag = 'lagLabel';
            lagLabel.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col1.add(lagLabel);
            
            sigLabel = Label(getString(message('econ:econModeler:SignificanceLevel')));
            sigLabel.Tag = 'significanceLevelLabel';
            sigLabel.Description = getString(message('econ:econModeler:SignificanceLevelDescription'));
            col1.add(sigLabel);
            
            %Add Lag and SignificanceLevel Controls
            col2 = parametersSection.addColumn();
            this.LagSpinner = Spinner([0 100],0);
            this.LagSpinner.StepSize = 1;
            this.LagSpinner.Tag = 'lagSpinner';
            this.LagSpinner.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col2.add(this.LagSpinner);
            
            this.SigSpinner = Spinner([0.001,0.999],0.05);
            this.SigSpinner.NumberFormat = 'double';
            this.SigSpinner.StepSize = 0.01;
            this.SigSpinner.DecimalFormat = '2f';
            this.SigSpinner.Tag = 'significanceLevelSpinner';
            this.SigSpinner.Description = getString(message('econ:econModeler:SignificanceLevelDescription'));
            col2.add(this.SigSpinner);
            
            %Add Model and Test Statitic Labels
            col3 = parametersSection.addColumn('HorizontalAlignment','right');
            modelLabel = Label(getString(message('econ:econModeler:Model')));
            modelLabel.Tag = 'modelLabel';
            modelLabel.Description = getString(message('econ:econModeler:ModelVariantDescription'));
            col3.add(modelLabel);
            
            testStatLabel = Label(getString(message('econ:econModeler:TestStat')));
            testStatLabel.Tag = 'testStatLabel';
            testStatLabel.Description = getString(message('econ:econModeler:TestStatDescription'));
            col3.add(testStatLabel);
            
            col4 = parametersSection.addColumn();
            this.ModelDropDown = DropDown({'AR',getString(message('econ:econModeler:AR'));...
                'ARD',getString(message('econ:econModeler:ARD'));...
                'TS',getString(message('econ:econModeler:TS'))});
            this.ModelDropDown.Value = 'AR'; 
            this.ModelDropDown.Tag = 'modelDropDown';
            this.ModelDropDown.Description = getString(message('econ:econModeler:ModelVariantDescription'));
            col4.add(this.ModelDropDown);
            
            this.TestStatDropDown = DropDown({'t1',getString(message('econ:econModeler:t1'));...
                't2',getString(message('econ:econModeler:t2'))});
            this.TestStatDropDown.Value = 't1';
            this.TestStatDropDown.Tag = 'testStatDropDown';
            this.TestStatDropDown.Description = getString(message('econ:econModeler:TestStatDescription')); 
            col4.add(this.TestStatDropDown);
            
            %Test Section
            testsSection = Section(getString(message('econ:econModeler:Tests')));
            testsSection.Tag = 'testsSection';
            this.Tab.add(testsSection);
            
            %Add Button
            col5 = testsSection.addColumn();
            this.AddButton = Button(getString(message('econ:econModeler:AddTest')),Icon.RUN_24);
            this.AddButton.Tag = 'addButton';
            this.AddButton.Description = getString(message('econ:econModeler:AddTestDescription'));
            this.AddButton.ButtonPushedFcn = @this.addTest;
            col5.add(this.AddButton);
            
            %Clear Button 
            col6 = testsSection.addColumn();
            this.ClearButton = Button(getString(message('econ:econModeler:ClearTests')),Icon.CLEAR_24);
            this.ClearButton.Tag = 'clearButton';
            this.ClearButton.Description = getString(message('econ:econModeler:ClearTestsDescription'));
            this.ClearButton.ButtonPushedFcn = @this.clearTests;
            this.ClearButton.Enabled = false;
            col6.add(this.ClearButton);
            
            this.TabGroup.Tag = 'ppTabGroup';
            session.addTabGroup(this.TabGroup);
        end
    end
    
    methods(Access = protected)
        function addTest(this,~,~)
            parameters = {this.LagSpinner.Value,...
                this.ModelDropDown.Value,...
                this.TestStatDropDown.Value,...
                this.SigSpinner.Value};
            this.Figure.addTest(parameters);
        end
        function clearTests(this,~,~)
            this.Figure.clearTests();
        end
         function enableClear(this,~,~)
            this.ClearButton.Enabled = this.Figure.NumberOfSelections~=0;
        end
        function str = getTitle(~)
            str = getString(message('econ:econModeler:PPName'));
        end
        function setTabTag(this)
            this.Tab.Tag = 'ppTab';
        end
    end
    
    methods(Access = {?internal.EconModeler.econModelerSession,...
            ?internal.EconModeler.Tabs.AbstractContextualTab})
        function setFig(this,fig)
            %Associate a new figure with this tab
            
            %The figure must be an PPFigure
            validateattributes(fig,{'internal.EconModeler.EconComponents.Figures.PPFigure'},{'scalar'});
            this.Figure = fig;
            
            %Set listner for changes in table size
            this.Listener = event.listener(fig,'DataChanged',@this.enableClear);
            this.enableClear;
        end
    end
end

