classdef PreferenceDlg < pidtool.AbstractPreferenceDlg
    % @PreferenceDlg defines preference dialog for PID Tuner

    % Author(s): R. Chen
    % Copyright 2009-2012 The MathWorks, Inc.
    
    properties(SetObservable)
        
        PIDSettings
    
    end
    
    methods

        % Constructor
        function this = PreferenceDlg(color)
            this = this@pidtool.AbstractPreferenceDlg(color);
            this.Width = 60;
            this.Height = 22;
            this.build;            
        end
        
        % refresh GUI
        function refresh(this, varargin)
            % parent
            refresh@pidtool.AbstractPreferenceDlg(this, varargin{:});
            % PID
            DataSrc = varargin{3};
            this.setPIDPreferences(DataSrc);
        end
        
    end
    
    methods (Access = protected)

        function build(this)
            % figure
            this.buildFigure;
            % pid panel
            this.buildPIDPanel;
            % color panel
            this.buildColorPanel;
            this.Handles.TunedColorLabel.setText(pidtool.utPIDgetStrings('cst','prefdlg_tunedcolor_label'));
            pidtool.utPIDaddCSH('control',this.Handles.TunedColorLabel,'pidtuner_prefdlg_tunedcolor');
            this.Handles.BaseColorLabel.setText(pidtool.utPIDgetStrings('cst','prefdlg_basecolor_label'));
            pidtool.utPIDaddCSH('control',this.Handles.BaseColorLabel,'pidtuner_prefdlg_basecolor');
            % assemble two panels
            Panel = javaObjectEDT('com.mathworks.mwswing.MJPanel');
            Panel.setLayout(java.awt.GridBagLayout);
            GBc = java.awt.GridBagConstraints;
            GBc.insets = java.awt.Insets(5,0,5,0);
            GBc.fill = java.awt.GridBagConstraints.HORIZONTAL;
            GBc.gridx = 0;
            GBc.weightx = 1;
            GBc.gridy = 0;
            Panel.add(this.Handles.PIDPanel,GBc);
            GBc.gridy = 1;
            Panel.add(this.Handles.ColorPanel,GBc);
            GBc.gridy = 2;
            GBc.weighty = 1;
            Panel.add(javaObjectEDT('com.mathworks.mwswing.MJPanel'),GBc);
            [~, PanelCONTAINER] = matlab.ui.internal.JavaMigrationTools.suppressedJavaComponent(Panel,[.1,.1,.9,.9],this.Handles.Figure);
            set(PanelCONTAINER,'units','character');
            this.Handles.PanelCONTAINER = PanelCONTAINER;            
            % button panel
            this.buildButtonPanel;            
        end

        function buildPIDPanel(this)                    
            % preference
            Prefs = cstprefs.tbxprefs;
            % common properties of GBC
            GBc = java.awt.GridBagConstraints;
            GBc.insets = java.awt.Insets(10,5,10,5);
            GBc.fill = java.awt.GridBagConstraints.HORIZONTAL;
            % panel
            Panel = javaObjectEDT('com.mathworks.mwswing.MJPanel');
            titleborder = javaMethodEDT('createTitledBorder','javax.swing.BorderFactory',pidtool.utPIDgetStrings('cst','prefdlg_box1'));
            javaObjectEDT(titleborder);
            Panel.setBorder(titleborder);
            Panel.setLayout(java.awt.GridBagLayout);
            Panel.setFont(Prefs.JavaFontB);
            % row 1:
            DesignFocusLabel = javaObjectEDT('com.mathworks.mwswing.MJLabel',pidtool.utPIDgetStrings('cst','strDesignFocusLabel'));
            DesignFocusLabel.setName('PREFERENCEDLG_DESIGNFOCUSLABEL');
            pidtool.utPIDaddCSH('control',DesignFocusLabel,'pidtuner_prefdlg_designfocus');
            DesignFocusLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            GBc.gridx = 0;
            GBc.weightx = 0;
            GBc.gridy = 0;
            GBc.anchor = java.awt.GridBagConstraints.EAST;
            Panel.add(DesignFocusLabel,GBc);
            DesignFocusComboBox = javaObjectEDT('com.mathworks.mwswing.MJComboBox',{pidtool.utPIDgetStrings('cst','strDesignFocusCombo_1'),...
                pidtool.utPIDgetStrings('cst','strDesignFocusCombo_2'),...
                pidtool.utPIDgetStrings('cst','strDesignFocusCombo_3')});
            DesignFocusComboBox.setName('PREFERENCEDLG_DESIGNFOCUSCOMBOBOX');
            GBc.gridx = 1;
            GBc.weightx = 1;
            GBc.gridy = 0;
            GBc.anchor = java.awt.GridBagConstraints.WEST;
            Panel.add(DesignFocusComboBox,GBc);
            % row 2:
            IFormulaLabel = javaObjectEDT('com.mathworks.mwswing.MJLabel',pidtool.utPIDgetStrings('cst','prefdlg_iformula_label'));
            IFormulaLabel.setName('PREFERENCEDLG_IFORMULALABEL');
            pidtool.utPIDaddCSH('control',IFormulaLabel,'pidtuner_prefdlg_iformula');
            IFormulaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            GBc.gridx = 0;
            GBc.weightx = 0;
            GBc.gridy = 1;
            GBc.anchor = java.awt.GridBagConstraints.EAST;
            Panel.add(IFormulaLabel,GBc);
            IFormulaComboBox = javaObjectEDT('com.mathworks.mwswing.MJComboBox',pidtool.utPIDgetStrings('cst','prefdlg_formula_combo',3));
            IFormulaComboBox.setName('PREFERENCEDLG_IFORMULACOMBOBOX');
            GBc.gridx = 1;
            GBc.weightx = 1;
            GBc.gridy = 1;
            GBc.anchor = java.awt.GridBagConstraints.WEST;
            Panel.add(IFormulaComboBox,GBc);
            % row 3: 
            DFormulaLabel = javaObjectEDT('com.mathworks.mwswing.MJLabel',pidtool.utPIDgetStrings('cst','prefdlg_dformula_label'));
            DFormulaLabel.setName('PREFERENCEDLG_DFORMULALABEL');
            pidtool.utPIDaddCSH('control',DFormulaLabel,'pidtuner_prefdlg_dformula');
            DFormulaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
            GBc.gridx = 0;
            GBc.weightx = 0;
            GBc.gridy = 2;
            GBc.anchor = java.awt.GridBagConstraints.EAST;
            Panel.add(DFormulaLabel,GBc);
            DFormulaComboBox = javaObjectEDT('com.mathworks.mwswing.MJComboBox',pidtool.utPIDgetStrings('cst','prefdlg_formula_combo',3));
            DFormulaComboBox.setName('PREFERENCEDLG_DFORMULACOMBOBOX');
            GBc.gridx = 1;
            GBc.weightx = 1;
            GBc.gridy = 2;
            GBc.anchor = java.awt.GridBagConstraints.WEST;
            Panel.add(DFormulaComboBox,GBc);
            %
            this.Handles.PIDPanel = Panel;
            this.Handles.DesignFocusLabel = DesignFocusLabel;
            this.Handles.DesignFocusComboBox = DesignFocusComboBox;
            this.Handles.IFormulaLabel = IFormulaLabel;
            this.Handles.IFormulaComboBox = IFormulaComboBox;
            this.Handles.DFormulaLabel = DFormulaLabel;
            this.Handles.DFormulaComboBox = DFormulaComboBox;
            
        end
        
        function setPIDPreferences(this, datasrc)
            this.Handles.DesignFocusComboBox.setSelectedIndex(...
                find(strcmp(datasrc.DesignFocus,this.DesignFocusContents))-1);
            if datasrc.SampleTime==0
                this.Handles.IFormulaComboBox.setSelectedIndex(0);
                this.Handles.DFormulaComboBox.setSelectedIndex(0);
                this.Handles.IFormulaComboBox.setEnabled(false);
                this.Handles.DFormulaComboBox.setEnabled(false);
            else
                this.Handles.IFormulaComboBox.setSelectedIndex(...
                    find(strcmp(datasrc.IFormula,this.IFormulaContents))-1);
                this.Handles.DFormulaComboBox.setSelectedIndex(...
                    find(strcmp(datasrc.DFormula,this.DFormulaContents))-1);
                this.Handles.IFormulaComboBox.setEnabled(true);
                this.Handles.DFormulaComboBox.setEnabled(true);
            end
        end
        
        function apply(this)
            % parent
            apply@pidtool.AbstractPreferenceDlg(this);
            % obtain current settings
            DesignFocus = this.DesignFocusContents{this.Handles.DesignFocusComboBox.getSelectedIndex+1};
            IFormula = this.IFormulaContents{this.Handles.IFormulaComboBox.getSelectedIndex+1};
            DFormula = this.DFormulaContents{this.Handles.DFormulaComboBox.getSelectedIndex+1};
            % check against discrete time with type PID and DFormula is T
            hTunerDlg = get(this.ParentFigure,'UserData');
            CurrentTs = hTunerDlg.DataSrc.SampleTime;
            CurrentType = hTunerDlg.DataSrc.Type;
            if CurrentTs>0 && any(strcmpi(CurrentType,{'pid','pid2','i-pd','id-p','pi-d'})) && strcmp(DFormula,'Trapezoidal')
                this.Handles.DFormulaComboBox.setSelectedIndex(...
                    find(strcmp(hTunerDlg.DataSrc.DFormula,this.DFormulaContents))-1);
                hTunerDlg.setStatusText(pidtool.utPIDgetStrings('cst','notsupportedpid'),'warn');
                return
            end
            % update Settings property
            this.PIDSettings = struct( ...
                'DesignFocus',DesignFocus,...
                'IFormula',IFormula,...
                'DFormula',DFormula);
        end
        
    end
    
end

