function [ handles ] = genWeightBoxes( handles )
% Takes care of the filter weight boxes depending on selected parameters
%

if handles.numFilterBoxesGenerated == true
    for k = 1:handles.numFiltersBoxes
        delete(handles.uiT{k});
    end
end

colEditable = true(1,handles.xSizeD);

for k = 1:handles.numFiltersD
    d = handles.filtVal{k};
    t = uitable(handles.FiltArrayDesigner,'Data',d,'ColumnWidth',{50});
    handles.uiT{k} = t;
    t.CellEditCallback = {@myCellEditCallback,k};
    t.Units = 'Normalized';
    t.Position(3) = t.Extent(3);
    t.Position(4) = t.Extent(4);
    t.ColumnEditable = colEditable;
end

yLayoutSteps = floor(sqrt(handles.numFiltersD/2.15));
if yLayoutSteps ~= 0
    xLayoutSteps = ceil(handles.numFiltersD/yLayoutSteps);
    yExtent = 0.4/yLayoutSteps - 0.01;
else
    xLayoutSteps = handles.numFiltersD;
    yExtent = 0.4 - 0.01;
    yLayoutSteps = 1;
end

xExtent =1/xLayoutSteps - 0.01;
% xExtent = t.Extent(3);
% yExtent = t.Extent(4);

% xLayoutSteps = floor((1-0.014)/(xExtent+0.025));
% if xLayoutSteps < 3
%     xExtent = (1-0.014)/3;
%     for k = 1:handles.numFiltersD
%         t = handles.uiT{k};
%         t.Position(3) = xExtent;
%         t.Position(4) = yExtent + 0.02;
%         xLayoutSteps = 3;
%     end
% end
% yLayoutSteps = floor(0.45/(yExtent + 0.025));

% numRows = ceil(handles.numFiltersD/xLayoutSteps);



% if numRows > yLayoutSteps
%     error('too many filters, cannot display on page');
% end
for j = 1:yLayoutSteps
    for k = 1:xLayoutSteps
        if ((j-1)*xLayoutSteps + k) > handles.numFiltersD
            break;
        end
        handles.uiT{k + (j-1)*xLayoutSteps}.Position(1) = 0.005+(k-1)*(xExtent+0.01);        
        handles.uiT{k + (j-1)*xLayoutSteps}.Position(3) = xExtent;
        handles.uiT{k + (j-1)*xLayoutSteps}.Position(2) = (j-1)*(yExtent+0.01)+0.005;    
        handles.uiT{k + (j-1)*xLayoutSteps}.Position(4) = yExtent;
    end
end

handles.numFiltersBoxes = handles.numFiltersD;
handles.numFilterBoxesGenerated = true;

end

