function importdlg(sisodb,AvailConfig)
%IMPORTDLG  Opens and manages the SISO Tool Import dialog.
%
%   ImportFig = IMPORTDLG(SISODB,AvailConfig) opens an Import dialog 
%   for the SISO Tool with database SISODB.  The vector AVAILCONFIG 
%   contains the indices of available feedback structures for display 
%   in the Import dialog.
%
%   See also IMPORTFCN.

%   Author(s): Karen D. Gondoly
%   Revised: P. Gahinet
%   Copyright 1986-2002 The MathWorks, Inc.
%   $Revision: 1.33 $  $Date: 2002/04/10 04:59:15 $

% RE Do not install CS help on modal windows (can't get out)

LoopData = sisodb.LoopData;

% Read current loop data
ModelData = LocalReadData(LoopData);

% Widget database
ud = struct('Parent',sisodb.Figure,...
    'Figure',[],...
    'Filter',@LocalBrowseFilter,...
    'ListData',struct('Names',[],'Models',[]),...
    'ModelData',ModelData,...
    'ImportedModel',{cell(4,1)},...
    'ImportedModelNames',{cell(4,1)},...
    'ImportExpression',{cell(4,1)},...
    'Handles',[]);

% Edit box labeling
Flabel = LoopData.describe('F','compact');
Clabel = LoopData.describe('C','compact');
ud.Labels = {'G = ','(Plant)';'H = ','(Sensor)';...
		'F = ',sprintf('(%s)',Flabel);'C = ',sprintf('(%s)',Clabel)};

% Create dialog
ud = LocalOpenDlg(ud);

% Display system name
set(ud.Handles.SystemNameEdit,...
    'String',ModelData.SystemName,...
    'UserData',ModelData.SystemName)

% Read and store model names
Names = {LocalGetName(ModelData.Plant,0) ; ...
        LocalGetName(ModelData.Sensor,0) ; ...
        LocalGetName(ModelData.Filter,1) ; ...
        LocalGetName(ModelData.Compensator,1)};
for ct=1:4,
    set(ud.Handles.ModelEdit(ct),'String',Names{ct});
end

% Draw feedback structure and hook up callback
ud.Handles.Configuration = loopstruct(LoopData.Configuration,ud.Handles.ConfigurationAxes);
set(ud.Handles.NextButton,'UserData',struct('Available',AvailConfig,'Current',LoopData.Configuration));

% Set apply callback
set(ud.Handles.OKButton,'Callback',{@LocalApply sisodb ud.Figure})

% Make figure visible and store database
set(ud.Figure,'UserData',ud,'Visible','on')

% Read systems in workspace
importfcn('workspace',ud.Figure);

% Disable OK if there is no data in UI yet
if isempty(LoopData.Plant.Model)
    set(ud.Handles.OKButton,'Enable','off')
end



%--------------------------Local Functions------------------------

%%%%%%%%%%%%%%%%%%

%%% LocalApply %%%
%%%%%%%%%%%%%%%%%%
function LocalApply(hSrc,event,sisodb,ImportFig)
% Commit import operation 
importapply(sisodb,ImportFig);


%%%%%%%%%%%%%%%%%%%%%
%%% LocalReadData %%%
%%%%%%%%%%%%%%%%%%%%%
function ModelData = LocalReadData(LoopData)
% Reads loop data into Import dialog database
if isempty(LoopData.Plant.Model)
   % No imported data yet. Use defaults
   DefaultModel = zpk(1);
   ModelData = struct(...
      'SystemName','untitled',...
      'Plant',struct('Name','untitledG','Model',DefaultModel),...
      'Sensor',struct('Name','untitledH','Model',DefaultModel),...
      'Filter',struct('Name','untitledF','Model',DefaultModel),...
      'Compensator',struct('Name','untitledC','Model',DefaultModel),...
      'FeedbackSign',-1,...
      'Configuration',1);       
else
   % Read loop data into MODELDATA structure
   ModelData = struct(...
      'SystemName',LoopData.SystemName,...
      'Plant',struct('Name',LoopData.Plant.Name,'Model',LoopData.Plant.Model),...
      'Sensor',struct('Name',LoopData.Sensor.Name,'Model',LoopData.Sensor.Model),...
      'Filter',struct('Name',LoopData.Filter.Name,'Model',LoopData.Filter.zpk),...
      'Compensator',struct('Name',LoopData.Compensator.Name,'Model',LoopData.Compensator.zpk),...
      'FeedbackSign',LoopData.FeedbackSign,...
      'Configuration',LoopData.Configuration);   
end


%%%%%%%%%%%%%%%%%%%%
%%% LocalGetName %%%
%%%%%%%%%%%%%%%%%%%%
function ModelName = LocalGetName(ModelData,ProtectFlag)
% Get model name to display in edit boxes
[z,p,k] = zpkdata(ModelData.Model,'v');

if isempty(z) & isempty(p) & abs(k)==1,
    % Model is 1 or -1
    ModelName = sprintf('%d',k);
else
    % Use model name
    ModelName = ModelData.Name;
    if ProtectFlag,
       % RE: Protect against overwriting changes to imported compensator
       ModelName = sprintf('(edited) %s',ModelName);
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalBrowseFilter %%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function tf = LocalBrowseFilter(var)
% Checks if given variable should be included in the import browser
% VAR is the structure produced by WHOS
if any(strcmp(var.class,{'tf','ss','zpk'}))
   tf = isequal(var.size,[1 1]);
elseif any(strcmp(var.class,{'idpoly','idss','idarx'}))
   tf = isequal(var.size([1 2]),[1 1]);
else 
   tf = 0;
end

%--------------------------Rendering Functions------------------------


%%%%%%%%%%%%%%%%%%%%
%%% LocalOpenFig %%%
%%%%%%%%%%%%%%%%%%%%
function ud = LocalOpenDlg(ud)

% Dimensioning Params (in char. units)
FigW = 108;
FigH = 22.5; 
Params = struct(...
   'hBorder',1.5,...
   'vBorder',0.5,...
   'ButtonH',1.5,...
   'EditH',1.5,...
   'TextH',1.1,...
   'Toffset',3,...
   'ArrowGap',7,...
   'ArrowW',5,...
   'StdUnit','character',...
   'StdColor',get(0,'DefaultUIControlBackground'));

%---Open the Import LTI Design Model Figure
ImportFig = figure(...
   'Units',Params.StdUnit, ...
   'Color',Params.StdColor, ...
   'MenuBar','none', ...
   'Visible','off',...
   'IntegerHandle','off',...
   'Name',xlate('Import System Data'), ...
   'NumberTitle','off', ...
   'Position',[3 7 FigW FigH], ...
   'Resize','off',...
   'WindowStyle','modal');
ud.Figure = ImportFig;

% Center wrt SISO Tool main window
centerfig(ImportFig,ud.Parent);

% Import source frame
Y0 = Params.ButtonH+2*Params.vBorder;
SrcFramePosition = [Params.hBorder,Y0,0.5*(FigW-2*Params.hBorder),13.5];
ud = LocalCreateSourceFrame(ud,SrcFramePosition,Params);

% System name
Y0 = Y0+SrcFramePosition(4)+2.5*Params.vBorder;
NameFramePosition = [Params.hBorder,Y0,SrcFramePosition(3),FigH-Y0-1.25];
ud = LocalCreateNameFrame(ud,NameFramePosition,Params);

% Window button fame
X0 = SrcFramePosition(1) + SrcFramePosition(3) + Params.ArrowGap;
ButtonGroupPos = [X0,Params.vBorder,FigW-Params.hBorder-X0,Params.ButtonH+2*Params.vBorder];
ud = LocalCreateButtons(ud,ButtonGroupPos,Params);

% System data frame
Y0 = Params.ButtonH+2*Params.vBorder;
DataFramePosition = [X0,Y0,ButtonGroupPos(3),FigH-Y0-1.25];
ud = LocalCreateDataFrame(ud,DataFramePosition,Params);
   
% Feedback structure 
AxisH = 7;
Y0 = FigH-AxisH-1.7;      
ud.Handles.ConfigurationAxes = axes('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'Color',Params.StdColor, ...
   'Xcolor',Params.StdColor, ...
   'Ycolor',Params.StdColor, ...
   'Position',[X0+1 Y0 DataFramePosition(3)-2 AxisH],... 
   'XTick',[], ...
   'YTick',[]);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalCreateNameFrame %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ud = LocalCreateNameFrame(ud,Position,Params)

ImportFig = ud.Figure;
TextH = Params.TextH;
EditH = Params.EditH;
FW = Position(3);
FH = Position(4);

% Main frame
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',Position, ...
   'Style','frame');
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[Position(1)+Params.Toffset Position(2)+FH-TextH/2 17 TextH], ...
	'String','System Name', ...
	'Style','text');

% Name edit
TW = 10;   vGap = (FH-EditH);
X0 = Position(1)+1.5*Params.hBorder;
Y0 = Position(2)+0.4*vGap;
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'BackgroundColor',Params.StdColor, ...
   'Position',[X0 Y0+(Params.EditH-Params.TextH)/2 TW Params.TextH], ...
   'HorizontalAlignment','left',...
   'String','Name:', ...
   'Style','text');
X0 = Position(1) + TW;
EBW = 28; 
ud.Handles.SystemNameEdit = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'BackgroundColor',[1 1 1], ...
   'Callback','importfcn(''namecallback'',gcbf)',...
   'Position',[X0 Y0 EBW Params.EditH], ...
   'Style','edit', ...
   'HorizontalAlignment','left');




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalCreateSourceFrame %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ud = LocalCreateSourceFrame(ud,Position,Params)

ImportFig = ud.Figure;
TextH = Params.TextH;
EditH = Params.EditH;
hBorder = Params.hBorder;
FW = Position(3);
FH = Position(4);

% Main frame
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',Position, ...
   'Style','frame');
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[Position(1)+Params.Toffset Position(2)+FH-TextH/2 15 TextH], ...
	'String','Import from', ...
	'Style','text');

% Source selector
X0 = Position(1)+1.5*hBorder;
Y0 = Position(2)+FH-3*TextH;
RBW = 15;
ud.Handles.Wbutton = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'BackgroundColor',Params.StdColor, ...
   'Callback','importfcn(''radiocallback'',gcbf);importfcn(''workspace'',gcbf);',...
	'Position',[X0 Y0 RBW TextH], ...
	'String','Workspace', ...
	'Style','radiobutton', ...
   'Value',1);
Y0 = Y0 - 1.5*TextH;
ud.Handles.Mbutton = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
   'Callback','importfcn(''radiocallback'',gcbf);importfcn(''matfile'',gcbf);',...
	'Position',[X0 Y0 RBW TextH], ...
	'String','MAT-file', ...
	'Style','radiobutton');
Y0 = Y0 - 1.5*TextH;
ud.Handles.Sbutton = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
   'Callback','importfcn(''radiocallback'',gcbf);importfcn(''simulink'',gcbf);',...
	'Position',[X0 Y0 RBW TextH], ...
	'String','Simulink', ...
	'Style','radiobutton');
set(ud.Handles.Wbutton,'UserData',[ud.Handles.Mbutton,ud.Handles.Sbutton]);
set(ud.Handles.Mbutton,'UserData',[ud.Handles.Wbutton,ud.Handles.Sbutton]);
set(ud.Handles.Sbutton,'UserData',[ud.Handles.Mbutton,ud.Handles.Wbutton]);

Y0 = Position(2)+1.25*Params.vBorder;
EBW = 0.42*FW;
ud.Handles.BrowseButton= uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'Callback','importfcn(''browsemat'',gcbf);',...
	'Enable','off', ...
	'Position',[X0 Y0 EBW Params.ButtonH], ...
	'String','Browse');
Y0 = Y0 + 1.05*Params.ButtonH;
ud.Handles.FileNameEdit = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'Enable','off', ...
   'Horiz','left',...
   'BackgroundColor',[1 1 1], ...
   'Callback','importfcn(''clearpath'',gcbf)',...
	'Position',[X0 Y0 EBW EditH], ...
   'Style','edit', ...
   'UserData',struct('FileName',[],'PathName',[]));
Y0 = Y0 + 1.2*EditH;
ud.Handles.FileNameText = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Enable','off', ...
	'Position',[X0 Y0 EBW TextH], ...
   'String','MAT-File Name:', ...
	'Style','text');

% Contents list box
LBW = 0.48*FW;   X0 = Position(1)+FW-LBW-hBorder;  Y0 = Position(2);
ud.Handles.ModelList = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',[1 1 1], ...
	'Position',[X0 Y0+Params.vBorder LBW FH-2.2], ...
	'Style','listbox', ...
	'Value',1);
ud.Handles.ModelText = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[X0 Y0+FH-1.7 LBW TextH], ...
   'HorizontalAlignment','center',...
   'String','SISO Models', ...
   'Style','text');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalCreateDataFrame %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ud = LocalCreateDataFrame(ud,Position,Params)

ImportFig = ud.Figure;
TextH = Params.TextH;
EditH = Params.EditH;
FW = Position(3);
FH = Position(4);

% Main frame
axes('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'Color',Params.StdColor, ...
   'Position',Position,... 
   'Box','on',...
   'Xcolor',[0 0 0],...
   'Ycolor',[0 0 0],...
   'XTick',[], ...
   'YTick',[]);
uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[Position(1)+Params.Toffset Position(2)+FH-TextH/2 16 TextH], ...
	'String','System Data', ...
	'Style','text');

% Edit boxes
vGap = 1.65*Params.vBorder;
X0 = Position(1)+1.5*Params.hBorder; 
Y0 = Position(2)+1.5*vGap;
vOff = (EditH-TextH)/2;
LabelW = 5;   EditW = 0.47*FW;  
X0a = Position(1)-(Params.ArrowGap+Params.ArrowW)/2;
X0t = X0+LabelW+EditW+1;  TextW = Position(1)+Position(3)-X0t-0.2;

ud.Handles.ArrowButton = zeros(4,1);
ud.Handles.ModelEdit = zeros(4,1);
for ct=4:-1:1,
   uicontrol('Parent',ImportFig, ...
      'Unit',Params.StdUnit,...
      'BackgroundColor',Params.StdColor, ...
      'Position',[X0 Y0+vOff LabelW TextH], ...
      'HorizontalAlignment','left',...
      'String',ud.Labels{ct,1}, ...
      'Style','text');
   ud.Handles.ModelEdit(ct) = uicontrol('Parent',ImportFig, ...
      'Unit',Params.StdUnit,...
      'BackgroundColor',[1 1 1], ...
      'Horiz','left',...
      'Callback','importfcn(''editcallback'',gcbf)',...
      'Position',[X0+LabelW Y0 EditW EditH], ...
      'Style','edit');
   ud.Handles.ArrowButton(ct) = uicontrol('Parent',ImportFig, ...
      'Unit',Params.StdUnit,...
      'Callback','importfcn(''arrowcallback'',gcbf)',...
      'Position',[X0a Y0 Params.ArrowW EditH], ...
      'String','-->');
   uicontrol('Parent',ImportFig, ...
      'Unit',Params.StdUnit,...
      'BackgroundColor',Params.StdColor, ...
      'Position',[X0t Y0+vOff TextW TextH], ...
      'HorizontalAlignment','left',...
      'String',ud.Labels{ct,2}, ...
      'Style','text')   
%      'ForegroundColor',ud.Labels{ct,3},...
   Y0 = Y0 + EditH + vGap;
end   

ud.Handles.NextButton = uicontrol('Parent',ImportFig, ...
   'Unit',Params.StdUnit,...
   'Callback','importfcn(''changeconfig'',gcbf)',...
   'Position',[Position(1)+FW-12 Y0 10 Params.ButtonH], ...
   'String','Other...');


%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalCreateButtons %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function ud = LocalCreateButtons(ud,Position,Params)

ImportFig = ud.Figure;
BW = 12; % button width
ButtonH = Params.ButtonH;
Gap = (Position(3)-3*BW)/2;
Y0 = Position(2);

% Add window buttons
X0 = Position(1); 
ud.Handles.OKButton = uicontrol('Parent',ImportFig, ...
    'Unit',Params.StdUnit,...
    'Position',[X0 Y0 BW ButtonH], ...
    'String','OK');
X0 = X0 + BW + Gap;
ud.Handles.CancelButton = uicontrol('Parent',ImportFig, ...
    'Callback','close(gcbf)',...
    'Unit',Params.StdUnit,...
    'Position',[X0 Y0 BW ButtonH], ...
    'String','Cancel');
X0 = X0 + BW + Gap;
ud.Handles.HelpButton = uicontrol('Parent',ImportFig, ...
    'Unit',Params.StdUnit,...
    'Callback','ctrlguihelp(''sisoimportdialog'');',...
    'Position',[X0 Y0 BW ButtonH], ...
    'String','Help');
