classdef(Hidden,Sealed) LMCTab < internal.EconModeler.Tabs.AbstractContextualTab
    %LMCTAB Tab used to control the LMC Tests in the econModeler App
    
    %Copyright 2017-2018 The MathWorks, Inc.
    
    properties(Access = private)
        LagSpinner
        SigSpinner
        TrendCheckBox
        TestDropDown
        AddButton
        ClearButton
        Listener
    end
    
    methods(Access = ?internal.EconModeler.ContextualTabList)
        function this = LMCTab(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.01,0.1],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);
            
            col3 = parametersSection.addColumn();
            this.TrendCheckBox = CheckBox(getString(message('econ:econModeler:IncludeTrend')));
            this.TrendCheckBox.Value = true;
            this.TrendCheckBox.Tag = 'trendCheckBox';
            this.TrendCheckBox.Description = getString(message('econ:econModeler:IncludeTrendDescription'));
            col3.add(this.TrendCheckBox);
            
            panel = Panel();
            col4 = panel.addColumn('HorizontalAlignment','right');
            testLabel = Label(getString(message('econ:econModeler:TestType')));
            testLabel.Tag = 'testLabel';
            testLabel.Description = getString(message('econ:econModeler:TestTypeDescription'));
            col4.add(testLabel);
            
            col5 = panel.addColumn();
            this.TestDropDown = DropDown({'var1',getString(message('econ:econModeler:Original'));...
                'var2',getString(message('econ:econModeler:Modified'))});
            this.TestDropDown.Value = 'var2';
            this.TestDropDown.Tag = 'testDropDown';
            this.TestDropDown.Description = getString(message('econ:econModeler:TestTypeDescription'));
            col5.add(this.TestDropDown);
            
            col3.add(panel);
            
            %Test Section
            testsSection = Section(getString(message('econ:econModeler:Tests')));
            testsSection.Tag = 'testsSection';
            this.Tab.add(testsSection);
            
            %Add Button
            col4 = 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;
            col4.add(this.AddButton);
            
            %Clear Button 
            col5 = 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;
            col5.add(this.ClearButton);
            
            this.TabGroup.Tag = 'lmcTabGroup';
            session.addTabGroup(this.TabGroup);
        end
    end
    
     methods(Access = protected)
        function addTest(this,~,~)
            parameters = {this.LagSpinner.Value,...
                this.TrendCheckBox.Value,...
                this.TestDropDown.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:LMCName'));
        end
        function setTabTag(this)
            this.Tab.Tag = 'lmcTab';
        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 LMCFigure
             validateattributes(fig,{'internal.EconModeler.EconComponents.Figures.LMCFigure'},{'scalar'});
             this.Figure = fig;
             
            %Set listner for changes in table size
            this.Listener = event.listener(fig,'DataChanged',@this.enableClear);
            this.enableClear;
         end
     end
end

