classdef ControllerDesignPanelAdvanced < pidtool.AbstractControllerDesignPanel
    % @ControllerDesignPanelAdvanced subclass

    % Author(s): R. Chen
    % Copyright 2009-2010 The MathWorks, Inc.
    
    methods

        %% Constructor
        function this = ControllerDesignPanelAdvanced(varargin)
            this = this@pidtool.AbstractControllerDesignPanel(varargin{:});
        end
        
        %% Update GUI components without firing event
        % set crossover spinner value without firing event
        function setWCSpinnerValue(this)
            this.localSetWCSpinnerValue(this.WC);
        end

        % set crossover text field value without firing event
        function setWCTextFieldValue(this)
            newWC = this.WC;
            this.Handles.CrossoverTextField.setText(sprintf('%0.3g %s',newWC,this.FreqUnitString));    
            javaMethodEDT('setDot',this.Handles.CrossoverTextField.getCaret(),0); % reset cursor to left
        end
        
        % set phase margin spinner value without firing event
        function setPMSpinnerValue(this)
            this.localSetPMSpinnerValue(this.PM);
        end
        
        % set phase margin text field value without firing event
        function setPMTextFieldValue(this)
            newPM = this.PM;
            this.Handles.PhaseMarginTextField.setText(sprintf('%d deg',newPM));    
            javaMethodEDT('setDot',this.Handles.PhaseMarginTextField.getCaret(),0); % reset cursor to left
        end
        
        % refresh crossover slider labels
        function refreshCrossoverSliderLabel(this)
            % reset label
            newCenterWC = this.CenterWC;
            labelTable = java.util.Hashtable;
            if 0.1*newCenterWC>this.MinWC
                hObj = javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%0.3g',0.1*newCenterWC));
            else
                hObj = javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%0.3g',this.MinWC));
            end
            labelTable.put(java.lang.Integer(0),hObj);
            hObj = javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%0.3g',newCenterWC));
            labelTable.put(java.lang.Integer(50),hObj);
            if 10*newCenterWC<this.MaxWC
                hObj = javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%0.3g',10*newCenterWC));
            else
                hObj = javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%0.3g',this.MaxWC));                                                
            end
            labelTable.put(java.lang.Integer(100),hObj);
            this.Handles.CrossoverSlider.setLabelTable(labelTable);
            this.Handles.CrossoverSlider.setPaintLabels(true);
        end
        
    end

    methods (Access = protected)
        
        %% Build
        function build(this)
            % super
            build@pidtool.AbstractControllerDesignPanel(this);
            % row 1 - WC label
            this.Handles.CrossoverLabel.setName('PIDTUNER_CROSSOVERLABEL');
            this.Handles.CrossoverLabel.setText(pidtool.utPIDgetStrings('cst','tunerdlg_wcslider'));
            pidtool.utPIDaddCSH('control',this.Handles.CrossoverLabel,'pidtuner_crossoverfreq');
            % row 2 - WC slider panel
            this.Handles.SlowButton.setName('PIDTUNER_LEFTBUTTON');
            this.Handles.SlowButton.setToolTipText(pidtool.utPIDgetStrings('cst','leftbutton_tooltip'));
            this.Handles.FastButton.setName('PIDTUNER_RIGHTBUTTON');            
            this.Handles.FastButton.setToolTipText(pidtool.utPIDgetStrings('cst','rightbutton_tooltip'));            
            this.Handles.CrossoverSlider.setName('PIDTUNER_CROSSOVERSLIDER');
            % row 4 - WC spinner and text field
            this.Handles.CrossoverTextField.setName('PIDTUNER_CROSSOVERTEXTFIELD');
            this.Handles.CrossoverSpinner.setName('PIDTUNER_CROSSOVERSPINNER');
            % row 3 - PM label
            this.Handles.PhaseMarginLabel.setName('PIDTUNER_PHASEMARGINLABEL');
            this.Handles.PhaseMarginLabel.setText(pidtool.utPIDgetStrings('cst','tunerdlg_pmslider'));
            pidtool.utPIDaddCSH('control',this.Handles.PhaseMarginLabel,'pidtuner_phasemargin');
            % row 4 - PM slider
            this.Handles.PhaseMarginSlider.setName('PIDTUNER_PHASEMARGINSLIDER');
            labelTable = java.util.Hashtable;
            labelTable.put(java.lang.Integer(0),javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%d',0)));
            labelTable.put(java.lang.Integer(45),javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%d',45)));
            labelTable.put(java.lang.Integer(90),javaObjectEDT('com.mathworks.mwswing.MJLabel',sprintf('%d',90)));
            this.Handles.PhaseMarginSlider.setLabelTable(labelTable);
            this.Handles.PhaseMarginSlider.setPaintLabels(true);
            % row 4 - PM spinner and text field
            this.Handles.PhaseMarginSpinnerModel.setMinimum(java.lang.Double(0));
            this.Handles.PhaseMarginSpinnerModel.setMaximum(java.lang.Double(90));
            this.Handles.PhaseMarginSpinnerModel.setStepSize(java.lang.Double(1));
            this.Handles.PhaseMarginTextField.setName('PIDTUNER_PHASEMARGINTEXTFIELD');
            this.Handles.PhaseMarginSpinner.setName('PIDTUNER_PHASEMARGINSPINNER');
        end

        %% Callbacks
        % WC spinner
        function CrossoverSpinnerCallback(this)
            % get WC
            newWC = this.Handles.CrossoverSpinner.getValue;
            % update WC
            this.Property_Listeners(2).Enabled = false;
            this.WC = newWC;
            this.Property_Listeners(2).Enabled = true;
        end

        % WC editor
        function CrossoverEditorCallback(this)
            % get WC
            Editor = this.Handles.CrossoverTextField;
            newWC = str2double(strrep(char(Editor.getText),sprintf('%s',this.FreqUnitString),''));
            if isnan(newWC) || ~isscalar(newWC) || ~isreal(newWC) || newWC<this.MinWC || newWC>this.MaxWC || ~isfinite(newWC)
                this.setWCTextFieldValue;
                return
            end
            % update WC (need to send event to refresh unit)
            this.WC = newWC;
        end

        % PM spinner
        function PhaseMarginSpinnerCallback(this)
            % get PM 
            newPM = this.Handles.PhaseMarginSpinner.getValue;
            % update PM
            this.Property_Listeners(5).Enabled = false;
            this.PM = newPM;    
            this.Property_Listeners(5).Enabled = true;
        end

        % PM editor
        function PhaseMarginEditorCallback(this)
            % get PM 
            Editor = this.Handles.PhaseMarginTextField;
            newPM = round(str2double(strrep(char(Editor.getText),'deg','')));
            if isnan(newPM) || ~isscalar(newPM) || ~isreal(newPM) || newPM<0 || newPM>90 || ~isfinite(newPM)
                this.setPMTextFieldValue;
                return
            end
            % update PM (need to send event to refresh unit or round off)
            this.PM = newPM;    
        end

    end
    
end

