---------------

function ScenarioUpdateListener(varargin)

this = varargin{end};
this.setNewDate;

% ------------------------------------------------------

function LocalRefreshTsLabel(varargin)

% Refreshes the controller sampling period label.

%disp('In LocalRefreshTsLabel')
this = varargin{end};
Controller = this.ControllerName;
ControllerNode = this.getMPCControllers.find('Label',Controller);
Ts = ControllerNode.Ts;        % String format
if isempty(Ts)
    Ts = '1';   % Default if controller node hasn't been opened yet.
end
this.Ts = evalin('base', Ts);  % Double
TsValue = this.Handles.TsValue;
Str = java.lang.String(LocalDouble2Str(this.Ts));
awtinvoke(TsValue,'setText(Ljava.lang.String;)',Str);

% ------------------------------------------------------

function setDefaultValues(this)

% Sets default values and initial conditions.

S = this.getRoot;
[NumMV, NumMD, NumUD, NumMO, NumUO, NumIn, NumOut] = getMPCsizes(S); %#ok<ASGLU>
OutIx =  1:NumOut;
if ~isempty(S.iNO)
    OutIx(S.iNO) = [];
    NumOut = length(OutIx);
end
% Initialize tabular data
Setpoints = cell(NumOut,8);
NumUMD = NumUD + NumMO + NumMV;
UnMeasDist = cell(NumUMD, 7);
Setpoints(:,:) = {java.lang.String(' ')};
Setpoints(:,3) = {'Constant'};
Setpoints(:,4) = {java.lang.String('0.0')};
Setpoints(:,8) = {false};
ix = [1 2 4];
jx = [1 4 5];
Setpoints(:,ix) = S.OutData(OutIx,jx);
if NumMD > 0
    MeasDist = cell(NumMD, 8);
    MeasDist(:,:) = {java.lang.String(' ')};
    MeasDist(:,3) = {'Constant'};
    MeasDist(:,4) = {java.lang.String('0.0')};
    MeasDist(:,8) = {false};
    MeasDist(:,ix) = S.InData(S.iMD,jx);
    this.Handles.MeasDist.setCellData(MeasDist);
end
UnMeasDist(:,:) = {java.lang.String(' ')};
UnMeasDist(:,3) = {'Constant'};
UnMeasDist(:,4) = {java.lang.String('0.0')};

ix = [1 2];
jx = [1 4];
if NumUD > 0
    UnMeasDist(1:NumUD,ix) = S.InData(S.iUD,jx);
end
UnMeasDist(NumUD+1:NumUD+NumMO,ix) = S.OutData(S.iMO,jx);
UnMeasDist(NumUD+NumMO+1:end,ix) = S.InData(S.iMV,jx);

% Move local data to permanent storage
this.Handles.Setpoints.setCellData(Setpoints);
this.Handles.UnMeasDist.setCellData(UnMeasDist);

% -----------------------------------------------------

function localPanelListener(eventSrc, eventData, this, thisJava)

% Updates the dialog panel in response to changes in the underlying UDD
% objects.

Prop = eventSrc.Name;
Val = get(this,Prop);
switch Prop
    case 'Tend'
        % Source is a JTextField
        Str=java.lang.String(Val);
        awtinvoke(thisJava,'setText(Ljava.lang.String;)',Str);
    case {'ClosedLoop', 'ConstraintsEnforced'}
        % Source is a check box
        awtinvoke(thisJava,'setSelected(Z)',(Val>0));
%     otherwise
%         errordlg(['Unexpected property code "',Prop,'" in localPanelListener']);
end

% ---------------------------------------------

function LocalCBs(eventSrc, eventData, this, thisProp, thisJava)

% Handles callbacks for standard java controls.

switch thisProp
    case 'Tend'
        % Store if valid.  Must be scalar >0 0.
        Num = char(thisJava.getText);
        Value = LocalStr2Double(Num);
        if ~isnan(Value) == 1 && Value > 0
            % return numerical equivalent
            String = LocalDouble2Str(Value);
            this.Tend = String;
        else
            % Invalid, so replace java display with UDD value
            localFieldError(thisJava, 'Duration must be positive, finite.');
            String = java.lang.String(this.Tend);
        end
        awtinvoke(thisJava,'setText(Ljava.lang.String;)',String);
    case 'Loops'
        % Source is a check box
        this.ClosedLoop = thisJava.isSelected;
    case 'Constraints'
        % Source is a check box
        this.ConstraintsEnforced = thisJava.isSelected;
    case 'RunButton'
        % User has pushed the run button
        this.runSimulation;
%     otherwise
%         errordlg(sprintf('Unexpected property code "%s" in LocalCBs', thisProp));
end

% --------------------------------------------------------------
function LocalTuningAdvisor(eventSrc, eventData, this)
% get controller node
Controllers = this.getMPCControllers;
Controller = Controllers.find('Label', this.Handles.ControllerCombo.getSelectedItem);
% time varying and non-diagonal weights are not supported in GUI
MPCobj = Controller.getController;
if iscell(MPCobj.Weights.ManipulatedVariables) || size(MPCobj.Weights.ManipulatedVariables,1)>1 || ...
    iscell(MPCobj.Weights.ManipulatedVariablesRate) || size(MPCobj.Weights.ManipulatedVariablesRate,1)>1 || ...
    iscell(MPCobj.Weights.OutputVariables) || size(MPCobj.Weights.OutputVariables,1)>1 
    uiwait(errordlg(ctrlMsgUtils.message('MPC:designtool:InvalidWeightsForAdvisor'), ctrlMsgUtils.message('MPC:designtool:DialogTitleError'), 'modal'));
    return
end
% create tuning advisor dialog
if ~ishandle(this.Advisor)
    this.Advisor = mpcobjects.advisor(this);
end
if isempty(this.Advisor.Handles)
    this.Advisor.createDialog(this.getMPCSims.CurrentScenario,this.ControllerName,Controller);
end
this.Advisor.show;

% --------------------------------------------------------------
function Value = LocalStr2Double(String)
try
    Value = evalin('base', String);
    if ~isreal(Value) || length(Value) ~= 1
        Value = NaN;
    end
catch ME %#ok<*NASGU>
    Value = NaN;
end

% -------------------------------------------------------------------------
function Str = LocalDouble2Str(Value)
Str = num2str(Value);

% --------------------------------------------------------------------------- %

function Valid = localFieldError(thisJava,Message)
% Temporarily disable the field's loss of focus callback and display an
% error message.

CB=get(thisJava,'FocusLostCallback');
set(thisJava,'FocusLostCallback',[]);
waitfor(errordlg(Message,ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
set(thisJava,'FocusLostCallback',CB);
awtinvoke(thisJava,'requestFocus(Z)',true);
Valid = false;

% -------------------------------------------------------------------------
% Before R2011a, local function SignalCheckFcn has been an input to the
% TableObject and saved with MPC project.  It is thus required to be
% present in this file to ensure a successful loading. The following
% function serves this purpose only and is not used at all after loading.
% DO NOT REMOVE OR CHANGE THIS FUNCTION
function OK = SignalCheckFcn(String, row, col) %#ok<INUSD,DEFNU,STOUT>

