classdef Library < extmgr.AbstractSet
    %Library   Define the Library class.
    
    %   Copyright 2012 The MathWorks, Inc.
    
    properties (Constant, Hidden)
        Instance = extmgr.Library;
    end
    
    properties (SetAccess = private)
        MessageLog;
    end
    
    methods
        function hSet = getRegistrationSet(this, varargin)
            %getRegistrationSet Return or create RegisterDb object from library.
            %  getRegistrationSet returns the RegistrationSet object corresponding to the specified
            %  extension file name.  If the extension file name is not found,
            %  a corresponding registration database is created and cached.
            if nargin == 2
                hSet = findobj(this.Children, 'FileName', varargin{1});
            else
                hSet = findobj(this.Children, 'NameSpace', varargin{1});
            end
            
            if isempty(hSet)
                % Create new registration database for this extension file name,
                % and pass a reference to RegisterLib's message log for reporting
                if nargin == 2
                    hSet = extmgr.PathRegistrationSet(varargin{:}, this.MessageLog);
                else
                    hSet = extmgr.NameSpaceRegistrationSet(varargin{:}, this.MessageLog);
                end
                % Add named database to RegisterLib library
                this.add(hSet);
            end
        end
    end
    
    methods (Access = private)
        function this = Library
            %Library   Construct the Library class.
            mlock;
            
            hMessageLog = uiservices.MessageLog('Extension Registration Library');
            hMessageLog.AutoOpenMode = 'manually';
            
            this.MessageLog = hMessageLog;
        end
    end
    
    methods (Static)
        function resetLibrary
            %resetLibrary Reset all of the extension registrations.
            
            l = extmgr.Library.Instance;
            l.Children = [];
        end
        
        function varargout = explore
            %EXPLORE  Explore the library database.
            
            % Create the default explorer which starts at the Library level
            % by default.
            h = extmgr.Explorer;
            h.show;
            
            if nargout
                varargout = {h};
            end
        end
    end
    
    methods (Hidden)
        function node = getTreeNode(this)
            %GETTREENODE Get the treeNode.
            
            hChildren = this.Children;
            node = [{'Library'} cell(1, numel(hChildren))];
            for indx = 1:numel(hChildren)
                node{indx+1} = hChildren(indx).getTreeNode;
            end
        end
        
        function nodeInfo = getNodeInfo(this)
            %GETNODEINFO Get the nodeInfo.
            
            nodeInfo.Title = uiscopes.message('TextLirarybDetailsNodeInfo');
            nodeInfo.Widgets = {uiscopes.message('TextLibraryDBNodeInfo') sprintf('%d', numChild(this))};
        end
        
        function hObject = getObjectFromPath(this, path)
            %GETOBJECTFROMPATH Get the objectFromPath.
            
            % A valid path at the Library level should start with
            % 'Library'.  If it does not, we assume that it starts with the
            % identification of a registration database.
            
            % The 'path' should be a valid DDG Tree path, using the '/'
            % character to delineate the objects.
            
            [registerName, path] = strtok(path, '/');
            
            % If we have only been provided with an empty string, return early.
            if isempty(registerName)
                hObject = this;
                return;
            end
            
            hObject = this.getRegistrationSet(registerName);
            
            if isempty(hObject) || isempty(path)
                return;
            end
            
            % Remove the unneeded '/'.
            path(1) = [];
            
            hObject = getObjectFromPath(hObject, path);
        end
    end
    
    methods (Access = protected, Static)
        function className = getChildClass
            className = 'extmgr.RegistrationSet';
        end
    end
end

% [EOF]
