%% check_grid
%    check_grid will recursively loop through all the cells of a
%    grid and call the check_matlab_syntax function for each of the
%    strings in the cells. if there is an error the cell will
%    change colours to alert the user where the problem is.
% inputs:
%   object:GUI - current GUI object
%   grid:Grid - Grid object being checked
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function msg = check_grid_condition(object,grid)
msg = [];
for i = 1:size(grid.cells,2)
    
    error = '';
    string = get(grid.cells(i).cond,'String');

    if size(string) > 0
        s = [string(1,:)];
        for j = 2:size(string,1)
           s = [s ' ' string(j,:)];
        end
        string = s;
    end
    
    if ( strcmp(string,'') || isempty(string)) && i == 1 && isempty(grid.parent_grid) && size(grid.cells,2) == 1
        break;
    end
    
    % if the string is empty indicating that the table is 1
    % dimensional or the string is "otherwise" skip the syntax
    % checking.
    if(~isempty(string) && ~strcmp(string,'otherwise'))
        error = object.check_matlab_syntax_condition(char(string),0);
    end
    if isempty(error)
        if (strcmp(string,'') || isempty(string))
            error = 'Cell is empty';
        end
    end
    if ~isempty(error)
        % generate the message string
        msg = [msg sprintf('\n')];
        
        msg = [msg 'Condtion -> ' char(string) sprintf('\n')];
        msg = [msg error sprintf('\n')];
        
        % set tooltip string of cell to error msg
        set(grid.cells(i).cond,'TooltipString',error);
        % change background colour
        grid.cells(i).flag_cell(1);
    else
        % reset tooltip and colour if no error found
        set(grid.cells(i).cond,'TooltipString','')
        grid.cells(i).flag_cell(0);
        
        
    end
    % recurse through subgrid
    if(~isempty(grid.cells(i).subgrid))
        msg = [msg object.check_grid_condition(grid.cells(i).subgrid)];
    end
    
    
end
end
