function finalizeDialogRegistration(dp)
% Done registering dialog panels
%  - determine dialog ordering from initial order specifications,
%    store in .DockedDialogs
%  - determine DialogBorder services to apply, and cache
%  - update dialog content

%   Copyright 2010-2014 The MathWorks, Inc.

% Lazy Loading plan:
%
%  - all DialogContent objects are on DCQueue list until DP panel becomes
%    visible
%  - let the "make DP panel visible" method perform the step of initial
%    conversion from DCQueue to Dialogs, possibly including use of
%    DialogLazy objects that masquerade as Dialogs until that particular
%    Dialog is made visible (docked or undocked)
%  - that process will call this method again, in which case DCQueue will
%    be empty, signaling us that we can proceed with registration below
%
if ~isempty(dp.DCQueue)
    % Do NOT reset the .Initializing flag
    checkRequestForDockedDialogs(dp);
    return
end

cacheTraversalInfo(dp.AllDialogs);
installDialogActions(dp); % installs actions on dialogs
%cacheTraversalInfo(dp.AllDialogs,false); % improves clear/close speed

addInitialDialogsToDockedList(dp);

cacheDialogBorderServices(dp); % e.g., panel lock
enableDialogBorderServices(dp); % synchronize properties that have state

% We are now done with initializing.
% Enable full layout with change/update methods.
dp.Initializing = false;

% Perform changeDialogContentHeight on all dialogs, now that ALL of the
% visibility changes have completed.
%
% NOTE: Do this only AFTER all dialogs have been opened for initialization.
% Otherwise we get a lot of time wasted on a docked-message handler, and a
% lot of re-do on the dialog positioning.
changeDialogContentHeight(dp.AllDialogs);

% Perform client updates to dialog text/values
updateDialogContent(dp);



%Resolve manual apply
for i=1:length(dp.Dialogs)
    if isa(dp.Dialogs(i).DialogContent,'dialogmgr.DCTableForm')
        setSubDialogsManual(dp.Dialogs(i).DialogContent,false,[]);
    end
end

end

function installDialogActions(dp)
% Installs actions on dialogs, such as visibility and enable actions.
%
% All dialogs (top-level and child) need initializeFinal() called. That
% method installs actions such as row and element visibility and enables,
% including actions that cross the dialog hierarchy (i.e., one dialog
% affecting visibility of entries in another dialog).
%
% So row visibilities may be changed, affecting dialog heights.  Since
% hierarchical references may occur, the height of one dialog may be
% affected by another dialog.  So we can't predict when all dialogs stop
% changing their heights, until the last dialog rowvis actions complete.
dlgs = dp.AllDialogs; % all dialogs, including child and top-level
Ndlgs = numel(dlgs);
for i = 1:Ndlgs
    initializeFinal(dlgs(i).DialogContent);
end

end

function addInitialDialogsToDockedList(dp)
% Add a subset of Dialog handles to the Dockeddialogs list.
%
% Does NOT consider the DCQueue list, only the Dialogs list.
%
% - Dialogs on the dp.Dialogs master list are implicitly closed.
% - Adds a subset to the .DockedDialogs list, as specified by the
%     DockedDialogNamesInit property
% - Does NOT add any Dialogs to the .UndockedDialogs list

% DockedDialogNamesInit
%   A cell-array of strings indicating the names of dialogs to make docked
%   and the order in which they are to appear.  This cell-array is used
%   ONLY AT INITIALIZATION to pre-load the index array, and at the time
%   that serialization of property values is performed.
%
% DockedDialogs
% A vector containing a subset of Dialog objects in .Dialogs that are
% currently docked in the multi-dialog panel.  This vector is used during
% program operation to determine docked display order, and is updated at
% runtime to reflect changes to docking order and dialog visibility.

% Get dialogs names that are to be opened at initialization
initNames = dp.DockedDialogNamesInit;
Ninit = numel(initNames);

% Get names of all registered dialogs
%  - registered means NOT on the lazy-loading queue
%  - this list contains only top-level dialogs, not child dialogs
rDlgs = dp.Dialogs; 
rNames = {rDlgs.Name};

% Allocate vector
idxVisOrder = zeros(1,Ninit); % maximum # of names we can match
NumToDock = 0; % number of names identified in registered Dialog list

% Determine dialog visibility order
for i = 1:Ninit
    idx = find(strcmpi(initNames{i},rNames));
    if isempty(idx)
        % Internal message to help debugging. Not intended to be user-visible.
        warning(message('Spcuilib:dialogmgr:WarnUnrecognizedDialogName', ...
            initNames{i} ));
    else
        % Record dialog in order in which it is to appear
        NumToDock = NumToDock + 1;
        idxVisOrder(NumToDock) = idx;
    end
end
idxVisOrder = idxVisOrder(1:NumToDock);
dp.DockedDialogs = rDlgs(idxVisOrder);

end

function checkRequestForDockedDialogs(dp)
% Check list of dialog names specified for initial docking by client.
%
% We must check against Dialogs and DCQueue to confirm existence of each
% name.  Issues warnings if any names are not found.

% Get dialogs names that are requested to be docked (visible)
initNames = dp.DockedDialogNamesInit;
Ninit = numel(initNames);

% Get names of all defined top-level Dialogs
% Add names of all DCQueued DialogContent objects
rDlgs = dp.Dialogs; 
rNames = {rDlgs.Name};
qNames = getNamesOfQueuedDialogs(dp);
allNames = [rNames qNames];

% Check requested names:
for i = 1:Ninit
    if ~any(strcmpi(initNames{i},allNames))
        warning(message('Spcuilib:dialogmgr:WarnUnrecognizedDialogName', ...
            initNames{i} ));
    end
end

end

% [EOF]
