function update(h,varargin)

%% Find the time series in the view and retain their selected status so
%% that it can be reproduced when the table is rebuilt

if ~isempty(h.ViewNode) && ishandle(h.ViewNode)
    timePlot = h.ViewNode.Plot;
else
    return % All views deleted
end

%% If the viewNode has just been created there will not yet be a timePlot.
%% In this case open an empty table
if ~isempty(timePlot) && ishandle(timePlot)
    tsList = timePlot.getTimeSeries;
    isSelected = true(size(tsList));
    tsNames = cell(size(tsList));
    tableData = cell(size(h.Handles.tsTable.Data));
    
    %% Preserve de-selected time series in the table    
    for k=1:length(tsList)
        tsNames{k} = tsList{k}.Name;
        if size(tableData,2)==3 && size(tableData,1)>0 && ...
                (nargin==1 || isempty(varargin{1}))
            I = find(strcmp(tsNames{k},tableData(:,2)));
            if ~isempty(I)
                isSelected(k) = tableData{I(1),1};
            end
        elseif nargin>=2 && ~isempty(varargin{1}) % Single specified timeseries selects by a @tsnode
            isSelected(k) = (varargin{1}==tsList{k});
        end
    end
    
    %% Create column ranges
    colRanges = cell(length(tsList),1);
    for k=1:length(tsList)
        colRanges{k} = sprintf('1:%d',size(tsList{k}.Data,2));
    end
         
    tableData = [num2cell(isSelected(:)) tsNames(:) colRanges(:)];
else
    tableData = cell(0,3);
end

%% Rebuild the table and refresh the dialog so that the state of the
%% components reflects the selected time series
h.Handles.tsTable.setData(tableData);
localTsChanged([],[],h)

%% Make the time series column take up all the extra space
colswidth0 = h.Handles.tsTable.getColumnWidth(0);
h.Handles.tsTable.setColumnWidth(1,(h.Handles.tsTable.getTableScrollPane.getWidth-...
    colswidth0)/2-5);
h.Handles.tsTable.setColumnWidth(2,(h.Handles.tsTable.getTableScrollPane.getWidth-...
    colswidth0)/2-5);
drawnow

function localTsChanged(eventSrc, eventData, h)

%% Table data callback which disbales cross-time series interpolation if
%% more than one time series is selected
tsData = cell(h.Handles.tsTable.Data);
if size(tsData,1)>1 && sum(double([tsData{:,1}]))>1  
    set(h.Handles.RADIOthists,'Value',1)
    set(h.Handles.RADIOotherts,'Enable','off')
    set(h.Handles.COMBotherts,'Enable','off')
elseif ~isempty(tsData)
    set(h.Handles.RADIOotherts,'Enable','on')
    set(h.Handles.COMBotherts,'Enable','on')
    set(h.Handles.COMBotherts,'String',tsData(:,2),'Value',1)
else % No time series in the view
    set(h.Handles.RADIOotherts,'Enable','off')
    set(h.Handles.COMBotherts,'Enable','off')
    set(h.Handles.COMBotherts,'String',{''})
end
