classdef AbstractEnableConstraint < dynamicprops
    %ABSTRACTENABLECONSTRAINT Defines the AbstractConstraint
    %   interface which is used by the extension manager to customize
    %   behavior of extension types.

    %   Author(s): J. Schickler
    %   Copyright 2007-2011 The MathWorks, Inc.

    properties (SetAccess = 'protected')
        Type = '';
    end

    methods
        function this = AbstractEnableConstraint(type)
            this.Type = type;
        end

        function vdb = findViolations(~, ~)
            %FINDVIOLATIONS Find constraint violations in a configuration.
            
            vdb = [];
        end
        
        function impose(~, ~, ~)
            %IMPOSE   Impose this constraint on the configuration.
            
            % NO OP
        end
        
        function b = isEnableAll(~, ~)
            %ISENABLEALL True if the object is EnableAll

            b = false;
        end

        function tableValueChanged(~, ~, ~, ~, ~, ~)
            %TABLEVALUECHANGED React to value changes in a table.

            % NO OP
        end
        
        function varargout = validate(~, varargin)
            %VALIDATE Returns true if valid to apply dialog.

            b         = true;
            exception = MException.empty;

            if nargout
                varargout = {b, exception};
            end
        end
        
        function b = willViolateIfDisabled(this, hSystem, ~)
            %WILLVIOLATEIFDISABLED  Returns true if disabling this
            %extension would violate the constraint.
            
            b = this.isEnableAll(hSystem);
        end
    end
    
    methods (Access = 'protected')
        function enaCnt = getEnableCount(this, hSystem)
            
            hConfigDb = hSystem.ConfigurationSet;

            hHidden = findHiddenRegistrations(hSystem, this.Type);
            
            enaCnt = 0;
            for indx = 1:length(hHidden)
                hConfig = findConfig(hConfigDb, hHidden(indx));
                if ~isempty(hConfig) && hConfig.Enable
                    enaCnt = enaCnt + 1;
                end
            end
            hVis = findVisibleRegistrations(hSystem, this.Type);
            
            for indx = 1:length(hVis)
                hConfig = findConfig(hConfigDb, hVis(indx));
                if ~isempty(hConfig) && hConfig.Enable
                    enaCnt = enaCnt + 1;
                end
            end
        end
    end

    methods
        function set.Type(this, type)
            if isempty(type) || ~ischar(type)
                error(message('Spcuilib:scopes:AbstractEnableConstraintEmptyType'));
            end
            this.Type = type;
        end
    end
end

% [EOF]
