function disctoolhandle = slmdldiscui(varargin)
% GUI for discretizing a simulink model
%
%

% Copyright 1990-2011 The MathWorks, Inc.

if (~usejava('Swing'))
  error(message('Simulink:ModelDiscretizer:GraphicDiscretizerNotSupported'));
end

persistent DISCWINDOW;

if nargin > 0
    [varargin{:}] = convertStringsToChars(varargin{:});
end

if nargout > 0 
    disctoolhandle = []; % in case of early exit
end

n = nargin;
import com.mathworks.toolbox.mdldisc.*;
[discrules, type] = rules;
ctlmsg = DAStudio.message('Simulink:ModelDiscretizer:NoRequiredControlLicenseMsg1'); 
dspmsg =  DAStudio.message('Simulink:ModelDiscretizer:NoRequiredDSPLicenseMsg'); 
if(type == 3)
    callDiagnosticViewer(DAStudio.message('Simulink:ModelDiscretizer:NoRequiredDSPAndControlLicenseMsg', ctlmsg,dspmsg));
    return;
else 
    if(type == 2)
        callDiagnosticViewer(DAStudio.message('Simulink:ModelDiscretizer:NoRequiredControlLicenseMsg2',  ctlmsg));
    return;
    end
end

if isempty(DISCWINDOW)
  if n == 1
    sys = varargin{1};    
    if (isstr(sys) && ~isvarname(sys))
      error(DAStudio.message('Simulink:ModelDiscretizer:InvalidModelNameMsg', sys ));
    end
    MDLDISC = MdlDisc(getdiscdata(sys));
    DISCWINDOW = MdlDiscWindow(MDLDISC);
    javaMethodEDT('show', DISCWINDOW);
  end
  if n == 0
    MDLDISC = MdlDisc;
    DISCWINDOW = MdlDiscWindow(MDLDISC);
    javaMethodEDT('show', DISCWINDOW);
  end
  mlock;
end

if n > 1
    param1 = varargin{1};
    param2 = varargin{2};   
    switch lower(param2)
    case 'create'
        if isempty(DISCWINDOW)
            if n == 3
                sys = varargin{3};
                if (isstr(sys) && ~isvarname(sys))
                    error(DAStudio.message('Simulink:ModelDiscretizer:InvalidModelNameMsg', sys ));
                end
                MDLDISC = MdlDisc(getdiscdata(sys));
                DISCWINDOW = MdlDiscWindow(MDLDISC);
            else
                MDLDISC = MdlDisc;
                DISCWINDOW = MdlDiscWindow(MDLDISC);
            end
            mlock;
        end        
    case 'show'
        javaMethodEDT('show', DISCWINDOW);
    case 'close'
        DISCWINDOW.dispose;
        DISCWINDOW= [];
        munlock;
        clear slmdldiscui;
      case 'close_temp'
        foundMDL = find_system('type','block_diagram','name', param2);
        if(~isempty(foundMDL))
            bdclose(param2,0)
        end         
     otherwise      
    end
end

if nargout > 0 
    disctoolhandle = DISCWINDOW;
end

%end function slmdldiscui

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% show error or warning message
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  callDiagnosticViewer(msgText)
warndlg(msgText, 'Simulink Model Discretizer');

%end function callDiagnosticViewer

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Check if there are any variable transport delay blocks in the model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ret = hasVariableTransportDelay(themdl)

if isempty(find_system('SearchDepth', 0, 'Name', themdl)) ...
    &  isempty(find_system('SearchDepth', 0, 'handle', themdl))
   if exist(themdl) == 4
      load_system(themdl);
   end
end

if isempty(find_system(themdl, 'BlockType', 'VariableTransportDelay'))
    ret = 0;
else
    ret = 1;
end

% end function hasVariableTransportDelay

%[EOF] slmdldiscui.m
