view m-toolbox/classes/@param/setDefaultOption.m @ 40:977eb37f31cb database-connection-manager

User friendlier errors from utils.mysql.connect
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 18:04:03 +0100
parents f0afece42f48
children
line wrap: on
line source

% SETDEFAULTOPTION Sets the default option of the a param object.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: SETDEFAULTOPTION Sets the default option of the a param
%              object. If the option doesn't exist in the options of the
%              param object then this method throws an error.
%
% CALL:        obj = obj.setDefaultOption(option);
%
% INPUTS:      obj    - A single param object
%              option - An option which should to be set.
%
% VERSION:     $Id: setDefaultOption.m,v 1.7 2011/04/22 21:14:57 mauro Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function p = setDefaultOption(p, option)
  
  % Check input
  if numel(p) ~= 1
    error('### This method works only with one input parameter object.');
  end
  if ~ischar(option) && isa(option, 'paramValue') && numel(option) ~= 1
    error('### This method works only with one input option.');
  end
  
  if ~isa(p.val, 'paramValue')
    p.val = option;
    return;
  end
  
  % Make a copy if the user doesn't use the modifier command
  p = copy(p, nargout);
  
  found = false;
  for ii = 1:numel(p.val.options)
    
    % Use different compare methods for the different types of 'option'
    switch class(option)
      case 'char'

        % CHAR
        if strcmpi(p.val.options{ii}, option)
          p.val.setValIndex(ii);
          found = true;
          break;
        end
      
      case 'specwin'
        
        % SPECWIN
        if strcmpi(p.val.options{ii}, option.type)
          p.val.setValIndex(ii);
          found = true;
          break;
        end
        
      case 'paramValue'
        if isequal(p.val.options{ii}, option.options{option.valIndex})
          p.val.setValIndex(ii);
          found = true;
          break;
        end
        
      otherwise
        
        % ALL OTHER TYPES
        if isequal(p.val.options{ii}, option)
          p.val.setValIndex(ii);
          found = true;
          break;
        end
        
    end
  end
  
  if ~found
    % IF the selection mode of the param-object is OPTIONAL then append
    % this new option to the param.options
    if (p.val.selection == paramValue.OPTIONAL)
      newOptions = [reshape(p.val.options, [], 1); {option}];
      p.val.setValIndexAndOptions(numel(newOptions), newOptions);
    else
      % throw an error if the option is not found in the param-options
      error('### I couldn''t find the option [%s] in the param-options.', utils.helper.val2str(option));
    end
  end
  
end