classdef (Hidden) SystemPanelMLOnline < handle
    % System selection section in Model Reduction App
    % compatible with MATLAB Online       
    
    % Author(s): Suat Gumussoy
    % Copyright 2016 The MathWorks, Inc. 
    
    properties(Access = protected, AbortSet = true)
        InspectDialog
        ToolData
        Widgets
        Name
    end
    properties (Transient)
        ToolDataListener
        SystemChangedListener
    end    
    methods
        function this = SystemPanelMLOnline(ToolData)
            this.ToolData = ToolData;
            createPanel(this);
            this.Name = 'SystemPanelDialog';
            this.ToolDataListener = addlistener(this.ToolData,'ToolDataChanged',@(es,ed) updateUI(this));  
        end 
        function delete(this)
            delete(this.ToolDataListener);
            delete(this.SystemChangedListener);
        end          
        function Widgets = getWidgets(this)
            Widgets = this.Widgets;
        end
        function updateUI(this)
            % disable listeners
            this.ToolDataListener.Enabled = false;
            this.SystemChangedListener.Enabled = false;
            
            % update inspect dialog
            if (~isempty(this.InspectDialog) && isvalid(this.InspectDialog))
                updateUI(this.InspectDialog);
            end
            % update order text
            this.Widgets.OrderNumberLabel.Text = sprintf('%d %s',order(getTargetSystem(this.ToolData)),...
                getString(message('Control:mrtool:States')));
%             % update items (ver1)
%             OldItems = this.Widgets.SystemComboBox.Items;
%             AddItems = setdiff(getTargetListName(this.ToolData),OldItems);
%             for ct=1:length(AddItems)
%                 addItem(this.Widgets.SystemComboBox,AddItems{ct});                                
%             end            
%             DelItems = setdiff(OldItems,getTargetListName(this.ToolData));
%             for ct=1:length(DelItems)
%                 removeItem(this.Widgets.SystemComboBox,DelItems{ct});
%             end
%             this.Widgets.SystemComboBox.Value = this.ToolData.getTargetName; 
            % update items (ver2)
            this.Widgets.SystemComboBox.replaceAllItems(getTargetListName(this.ToolData)');
            if ~strcmp(this.Widgets.SystemComboBox.Value,this.ToolData.getTargetName)
                this.Widgets.SystemComboBox.Value = this.ToolData.getTargetName;
                drawnow; % to update combo bax value and prevent g2061788
                setTarget(this,this.ToolData.getTargetName);
            end
                            
            % After Rong's sugesstion to prevent race conditions on MO
            pause(0.3);
            
            % enable listeners
            this.ToolDataListener.Enabled = true;
            this.SystemChangedListener.Enabled = true;
        end
        function openInspectDialog(this)
            if isempty(this.InspectDialog) || ~isvalid(this.InspectDialog)
                this.InspectDialog = mrtool.dialogs.InspectDialog(this.ToolData);
            end
            show(this.InspectDialog,this.Widgets.InspectButton);
        end         
    end
    methods (Access=protected)    
        function SystemSection = createPanel(this)
            import matlab.ui.internal.toolstrip.*
            % Strings
            SystemStr = getString(message('Control:mrtool:SystemLabel'));
            SystemTooltip = getString(message('Control:mrtool:SystemTooltip'));
            % Labels                                 
            SystemLabel = Label(sprintf('%s: ',SystemStr));
            OrderLabel = Label(sprintf('%s: ',getString(message('Control:mrtool:Order'))));
            OrderNumberLabel = Label(sprintf('%d %s',order(getTargetSystem(this.ToolData)),...
                getString(message('Control:mrtool:States'))));
                          
            Items = this.ToolData.getTargetListName;
            SystemComboBox = DropDown(Items');
            SystemComboBox.Value = this.ToolData.getTargetName;
            SystemComboBox.Description = SystemTooltip;
            this.SystemChangedListener = addlistener(SystemComboBox, 'ValueChanged', @(es,ed) setTarget(this,ed.EventData.NewValue));
            
            % Layout 
            % section
            SystemSection = Section(getString(message('Control:mrtool:SystemSection')));
            SystemSection.Tag = 'SystemSection';
            % column1
            column1 = Column();
            add(SystemSection,column1)
            add(column1,SystemLabel);
            add(column1,OrderLabel);
            
            % column2
            fScaledSize = com.mathworks.util.ResolutionUtils.scaleSize(70);
            column2 = Column('Width',fScaledSize);            
            add(SystemSection,column2)            
            add(column2,SystemComboBox);            
            add(column2,OrderNumberLabel);            
            
            % Store widgets
            this.Widgets =  struct(...
                'Section',SystemSection,...
                'SystemLabel',SystemLabel,...
                'SystemComboBox',SystemComboBox,...
                'OrderLabel',OrderLabel,...
                'OrderNumberLabel',OrderNumberLabel);
        end        
        function setTarget(this,SelectedItem)
            TargetList = this.ToolData.getTargetList;
            SystemIndex = cellfun(@(x) strcmp(x,SelectedItem),{TargetList.Name});                                    
            NewTarget = TargetList(SystemIndex);
            if ~isempty(NewTarget)
                setToolTarget(this.ToolData,NewTarget);
            end
        end      
    end
end