classdef BuildInfoKeyValuePair < RTW.BuildInfoClass & matlab.mixin.SetGet
%RTW.BuildInfoKeyValuePair class
%   RTW.BuildInfoKeyValuePair extends RTW.BuildInfoClass.
%
%    RTW.BuildInfoKeyValuePair properties:
%       Settings - Property is of type 'handle'  
%       DisplayLabel - Property is of type 'ustring'  
%       Group - Property is of type 'ustring'  
%       Key - Property is of type 'ustring'  
%       Value - Property is of type 'ustring'  

%   Copyright 2017-2019 The MathWorks, Inc.

    properties
        Key = '';
        Value = '';
    end

    methods
        function h = BuildInfoKeyValuePair(varargin)
        %  when the object is read in from a mat file, this is called with no args to
        %  load the class
            if (nargin == 0)
                return;
            end   
            
            %check the args to make sure they all conform to the expected format
            [args_ok, args] = locCheckArgs(varargin{:});
            if ~(args_ok)
                DAStudio.error('RTW:buildInfo:badMethodArguments',...
                               'RTW:buildInfo:BuildInfoKeyValuePair');
            end
            
            % set the properties
            h.Key          = args.Key;
            h.Value        = args.Value;
            h.Group        = args.Group;
            h.DisplayLabel = args.DisplayLabel;
            h.Settings     = args.Settings;
        end
    end

    methods
        function val = getPreferredProperties(h)
        %GETPREFERREDPROPERTIES - Returns the preferred properties for the DA Studio
        %Explorer
            
        % the base class has properties which should be in the
        % preferred properties list.  Since there is no way to call a
        % base class method from a derived class, call a surrogate
        % getPreferredProperties from the base class.
            base_props = h.getBaseBuildInfoClassProperties();
            additional_props = {'Value'};
            val = [base_props additional_props];
        end
        
        function val = isHierarchical(~)
        %ISHIERARCHICAL - returns true if this class has hierarchical child properties.
            val = false;
        end
    end
end

function [args_ok, args] = locCheckArgs(varargin)

    args_ok      = true;
    args.Key          = '';
    args.Value        = '';
    args.DisplayLabel = 'BuildInfo Key-Value Pair';
    args.Group        = '';
    args.Settings     = [];

    % when the object is read in from a mat file, 
    if (nargin < 2)
        args_ok = false;
        return;
    end

    % First arg is the Key
    if ~ischar(varargin{1})
        args_ok = false;
        return;
    end
    args.Key = varargin{1};

    % second arg is the Value
    if ~ischar(varargin{2})
        args_ok = false;
        return;
    end
    args.Value = varargin{2};

    % third arg is the group
    if (nargin >= 3)
        if ~ischar(varargin{3})
            args_ok = false;
            return;
        end
        args.Group = varargin{3};
    end

    % Fourth arg is the display label for the viewer
    if (nargin >= 4)
        if ~ischar(varargin{4})
            args_ok = false;
            return;
        end
        args.DisplayLabel = varargin{4};
    end

    % final arg is a handle to a settings object, or [].
    if (nargin >= 5)
        if ~isempty(varargin{5}) && ~isa(varargin{5}, 'RTW.BuildInfoSettings')
            args_ok = false;
            return;
        end
        args.Settings = varargin{5};
    end
end

