function mpc_designcallback(blkh)
% Callback for the design button in the MPC Controller block dialog

%   Authors: R. Chen
%   Copyright 2013-2015 The MathWorks, Inc.

%% Need JAVA
if ~usejava('swing')
    errordlg(ctrlMsgUtils.message('MPC:designtool:JAVANotFound'),...
        ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal');
    return
end
%% Get model parameters
fullblockname = getfullname(blkh);    
mpcobjname = get_param(blkh,'mpcobj');
%% Return if there is unapplied changes in the block
hBlock = handle(blkh);
hSource = hBlock.getDialogSource;
hDialog = hSource.getOpenDialogs;
if isempty(hDialog)
    HasUnappliedChanges = false;
else
    HasUnappliedChanges = hDialog{1}.hasUnappliedChanges;
end
if HasUnappliedChanges
    errordlg(ctrlMsgUtils.message('MPC:designtool:UnappliedChanges'),...
        ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal');
    return
end    
%% cannot use custom state estimation
if strcmp(get_param(blkh,'state_inport'),'on')
    errordlg(ctrlMsgUtils.message('MPC:designtool:FailWithCustomEstimation'),...
        ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal');
    return
end
%% If no mpc object is specified, SCD license is needed
if isempty(mpcobjname)
    % need SCD license
    if isempty(mpcobjname) && ~mpcchecktoolboxinstalled('slcontrol')
        errordlg(ctrlMsgUtils.message('MPC:designtool:SCDNotFound'),...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal');
        return
    end
%% Otherwise, it must be a valid @mpc object in the base workspace
else
    % resolve the variable first
    try
        mpcobj = slResolve(mpcobjname, blkh);
    catch ME
        uiwait(errordlg(ctrlMsgUtils.message('MPC:designtool:MPCObjectNotFoundinWorkspace',mpcobjname),...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
        return
    end
    % it must be a valid MPC object
    if ~isa(mpcobj,'mpc')
        uiwait(errordlg(ctrlMsgUtils.message('MPC:designtool:InvalidMPCObject',fullblockname),...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
        return
    end        
    % it cannot be empty
    if isempty(mpcobj)
        uiwait(errordlg(ctrlMsgUtils.message('MPC:designtool:EmptyMPCObject',fullblockname),...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
        return
    end
    % md discrepency
    hasMD = getNMD(mpcobj)>0;
    md_inport = get_param(fullblockname,'md_inport');
    hasMDPort = strcmp(md_inport,'on');
    if hasMD ~= hasMDPort
        uiwait(errordlg('Mismatch of measured disturbance channels between MPC object and MPC block',...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
        return
    end
    % cannot use custom estimator
    if ischar(getEstGain(mpcobj)) && strcmpi(getEstGain(mpcobj), 'custom')
        uiwait(errordlg(ctrlMsgUtils.message('MPC:designtool:FailWithCustomEstimation'),...
            ctrlMsgUtils.message('MPC:designtool:DialogTitleError'),'modal'));
        return
    end
end
% close mask dialog
close_system(blkh);
% launch MPC Designer
mgr = mpcapp.manager.AppManager.getInstance();
mgr.open(blkh);

