classdef Configuration < matlab.mixin.Copyable
    %Configuration   Define the Configuration class.

    %   Copyright 2009 The MathWorks, Inc.

    properties (SetAccess = private)

        Type;
        Name;
    end
    
    properties (SetObservable)
        Enable = false;
    end
    
    properties
        PropertySet;
    end
    
    properties (Hidden, Transient) %Access = private)
        pPropertySet;
    end
    
    % Add backwards compatible properties.
    properties (Dependent, Hidden)
        PropertyDb;
    end
    
    properties (Hidden, Transient)
        Registration;
    end
        
    methods

        function this = Configuration(type, name, enable, varargin)
            %Configuration   Construct the Configuration class.
            
            mlock;
            narginchk(2,inf);
            this.Type = type;
            this.Name = name;
            
            if nargin>2
                if islogical(enable)
                    this.Enable = enable;
                    if nargin>4
                        this.PropertySet = extmgr.PropertySet('-notype', varargin{:});
                    elseif nargin > 3
                        this.PropertySet = varargin{1};
                    end
                else
                    if nargin>3
                        % Force a "too many input arguments" error.
                        narginchk(1,3);
                    end
                    this.PropertySet = enable; % theEnable is really hPropertyDb
                end
            end
        end
        
        function string = toString(this, showEllip, compress)
            if nargin < 2
                showEllip = false;
            end
            if nargin < 3
                compress = false;
            end
            if showEllip
                ellipsis = sprintf('...\n  ');
            else
                ellipsis = '';
            end
            string = sprintf('extmgr.Configuration(''%s'',''%s'',%s', ...
                this.Type, this.Name, mat2str(this.Enable));
            pSet = this.PropertySet;
            if isempty(pSet) || pSet.numProps == 0
                string = sprintf('%s)', string);
            else
                pString = toString(pSet, showEllip, compress);
                if showEllip
                    pString = strrep(pString, sprintf('\n'), sprintf('\n  '));
                end
                string = sprintf('%s,%s%s)', string, ellipsis, pString);
            end
        end
        
        function set.PropertySet(this, newPropertySet)
            if isa(newPropertySet, 'extmgr.PropertyDb')
                % Support for older files.
                newPropertySet = convertToNewFormat(newPropertySet);
            end
            this.pPropertySet = newPropertySet; %#ok<MCSUP>
        end
        
        function propSet = get.PropertySet(this)
            propSet = this.pPropertySet;
        end
        
        % Set/Get for backwards compatible properties.
        function set.PropertyDb(this, newPropertySet)
            this.PropertySet = newPropertySet;
        end
        
        function propertySet = get.PropertyDb(this)
            propertySet = this.pPropertySet;
        end
    end
    
    methods (Hidden)
        function p = findProperty(this, propName)
            p = findProperty(this.PropertySet, propName);
        end
        
        function convertPropertySetToOldFormat(this)
            hPropSet = this.pPropertySet;
            if ~isempty(hPropSet)
                % Set into the private property that has no checks.
                this.pPropertySet = convertToOldFormat(hPropSet);
            else
                % Old releases expect an object and not empty
                this.pPropertySet = extmgr.PropertyDb;
            end
        end
 
%       DDG  protocol method. Return a valid DDG data type 
%       Fall out of g894809 (change on DDG side), please see the spec for details
%       Override the "getPropDataType" protocol method to mimic the behavior before the change.      
        function dataType = getPropDataType(~, propName)
            switch propName
                case {'Enable'}
                    dataType = 'bool';
                otherwise
                    dataType = 'invalid';
            end
        end
        
    end
    
    methods (Access = protected)
        function hCopy = copyElement(this)
            %copyElement Copy the single elemtn
            
            hCopy = copyElement@matlab.mixin.Copyable(this);
            
            if ~isempty(this.pPropertySet)
                hCopy.pPropertySet = copy(this.pPropertySet);
            end
            hCopy.Registration = this.Registration;
        end
    end
end

% [EOF]
