function StoreAsName = storedlg(sisodb,Prompt,Title)
%STOREDLG  Opens and manages the STORE AS dialog.
%
%   See also SISOTOOL.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc.
%   $Revision: 1.6 $  $Date: 2000/08/02 16:06:43 $

% Read current loop data
Dialog = LocalOpen(Title,Prompt,sisodb);

% Pause
uiwait(Dialog);

% Get output value
StoreAsName = get(Dialog,'UserData');

% Close dialog
delete(Dialog)

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

%%%%%%%%%%%%%%%%%%
%%% LocalAbort %%%
%%%%%%%%%%%%%%%%%%
function LocalAbort(hSrc,event,Dialog)
% Abort operation 
set(Dialog,'UserData','')
uiresume(Dialog)


%%%%%%%%%%%%%%%
%%% LocalOK %%%
%%%%%%%%%%%%%%%
function LocalOK(hSrc,event,Dialog,LoopData)
% OK operation 
StoreAsName = get(Dialog,'UserData');
% Check if already in use
Designs = LoopData.SavedDesigns;
if ~any(strcmp(StoreAsName,{Designs.Name})) | ...
        strcmpi(questdlg({'There is already a compensator with the same name.';...
            'Do you want to overwrite it?'}, ...
        'Store Warning', ...
        'Yes','No','Cancel','Cancel'),'Yes')
    % Exit dialog
    uiresume(Dialog)
end


%%%%%%%%%%%%%%%%%
%%% LocalEdit %%%
%%%%%%%%%%%%%%%%%
function LocalEdit(hSrc,event,Dialog)
% Edit name
NewName = get(hSrc,'String');
if isvarname(NewName)
    set(Dialog,'UserData',NewName)
else
    set(hSrc,'String',get(Dialog,'UserData'))
end


%%%%%%%%%%%%%%%%%%%%
%%% LocalDefault %%%
%%%%%%%%%%%%%%%%%%%%
function DefaultName = LocalDefault(LoopData)
% Gets default compensator name
CompName = LoopData.Compensator.Name;
Designs = LoopData.SavedDesigns;
UsedNames = {Designs.Name};
% Remove _xxx termination
UnderScores = findstr(CompName,'_');
if ~isempty(UnderScores) & ~isnan(str2double(CompName(UnderScores(end)+1:end)))
    CompName = CompName(1:UnderScores(end)-1);
end
% Find first available termination
term = 1;
nchar = length(CompName)+1;
for ct = find(strncmpi([CompName '_'],UsedNames,nchar)),
    tail = str2double(UsedNames{ct}(nchar+1:end));
    if ~isnan(tail)
        term = max(term,tail)+1;
    end
end
DefaultName = sprintf('%s_%d',CompName,term);


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


%%%%%%%%%%%%%%%%%%%%
%%% LocalOpenFig %%%
%%%%%%%%%%%%%%%%%%%%
function Dialog = LocalOpen(Title,Prompt,sisodb)

% Get default name
DefaultName = LocalDefault(sisodb.LoopData);

% Dimensioning Params (in char. units)
FigW = 40;
FigH = 6.8; 
hBorder = 1.5;
vBorder = 0.5;
ButtonH = 1.5;
ButtonW = 10;
EditH = 1.5;
TextH = 1.1;
StdUnit = 'character';
StdColor = get(0,'DefaultUIControlBackground');

%---Open the Import LTI Design Model Figure
Dialog = figure(...
   'Units',StdUnit, ...
   'Color',StdColor, ...
   'MenuBar','none', ...
   'Visible','off',...
   'IntegerHandle','off',...
   'Name',xlate(Title), ...
   'NumberTitle','off', ...
   'Position',[90 50 FigW FigH], ...
   'Resize','off',...
   'WindowStyle','modal');
centerfig(Dialog,sisodb.Figure);
set(Dialog,'CloseRequestFcn',{@LocalAbort Dialog},...
    'UserData',DefaultName)

% Add buttons
X0 = FigW - 2*(hBorder+ButtonW); Y0 = vBorder;
hOK = uicontrol('Parent',Dialog, ...
    'Unit',StdUnit,...
    'Callback',{@LocalOK Dialog sisodb.LoopData},...
    'Position',[X0 Y0 ButtonW ButtonH], ...
    'String','OK');
X0 = X0 + hBorder + ButtonW;
uicontrol('Parent',Dialog, ...
    'Unit',StdUnit,...
    'Callback',{@LocalAbort Dialog},...
    'Position',[X0 Y0 ButtonW ButtonH], ...
    'String','Cancel');

% Add edit box
Y0 = Y0+ButtonH+2*vBorder;
hEdit = uicontrol('Parent',Dialog, ...
    'Unit',StdUnit,...
    'BackgroundColor',[1 1 1], ...
    'Horiz','left',...
    'Callback',{@LocalEdit Dialog},...
    'Position',[hBorder Y0 FigW-2*hBorder EditH], ...
    'String',DefaultName,...
    'Style','edit');
Y0 = Y0 + EditH + 0.8 * vBorder;
uicontrol('Parent',Dialog, ...
        'Unit',StdUnit,...
        'BackgroundColor',StdColor, ...
        'Position',[hBorder Y0 FigW-2*hBorder TextH], ...
        'HorizontalAlignment','left',...
        'String',Prompt, ...
        'Style','text');
    
    
