function refresh(h)

%% Updates the components on the dialog to reflect the selected time series
%% Specifically, enables and disables redio buttons and displays the
%% "common" time vector

import javax.swing.*; 

%% List of enable-able components 
components = {h.Handles.BTNok,h.Handles.BTNcancel,h.Handles.RADIOinplace,...
               h.Handles.RADIOnewmerged,h.Handles.RADIOunion, ...
               h.Handles.RADIOintersect,h.Handles.RADIOuniform,...
               h.Handles.RADIOtimeseries,h.Handles.EDITinterval,...
               h.Handles.COMBunits,h.Handles.COMBts};
           
%% If the timePlot has gone disable components and do nothing
if isempty(h.ViewNode.Plot) || ~ishandle(h.ViewNode.Plot)
    mask = {'off','on','off','off','off','off','off','off','off','off','off'};
else
    %% Find the selected time series
    drawnow % Ensure the table is ready
    tableData = cell(h.Handles.tsTable.Data);
    if isempty(tableData) % No time series in the view
        return
    end
    I = ~cellfun('isempty',tableData(:,2)) & cell2mat(tableData(:,1));
    tsSelected = tableData(I,2);

    %% Refresh the timeseries combo
    tsList = h.ViewNode.Plot.getTimeSeries;
    tsNames = cell(size(tsList));
    for k=1:length(tsList)
        tsNames{k} = tsList{k}.Name;
    end
    tscombval = get(h.Handles.COMBts,'Value');
    previoustsnames = get(h.Handles.COMBts,'String');
    ind = [];
    if length(previoustsnames)>=tscombval
        ind = find(strcmp(previoustsnames{tscombval},tsNames));
    end
    if ~isempty(ind) % Keep last selection if it is still valid
        set(h.Handles.COMBts,'String',tsNames,'Value',ind(1));
    else
        set(h.Handles.COMBts,'String',tsNames,'Value',1)
    end

    %% Enable/disable components based on the number of time series selected               
    if isempty(tsSelected) % Disable all components
        mask = repmat({'off'},[1 11]);
        start = 'empty';
        finish = 'empty';
        % Select the default time vector
        set(h.Handles.RADIOtimeseries,'Value',1) 
    elseif length(tsSelected)==1
        mask = {'on','on','on','on','off','off','on','on','on','on','on'};
        % If either of the disabled radio buttons are selected then select the
        % default radio and and select this time series in the corresponding
        % time series combo
        if get(h.Handles.RADIOunion,'Value') || get(h.Handles.RADIOintersect,'Value')
            set(h.Handles.RADIOtimeseries,'Value',1)    
            %set(h.Handles.COMBts,'Value',find(strcmp(tsSelected{1},tsNames)))
        end

        %% If uniform time vector is selected set the start and end time
        %% accordingly
        targetsts = h.ViewNode.Plot.getTimeSeries(tsSelected{1});
        if get(h.Handles.RADIOuniform,'Value')
            thists = targetsts;
        else % Resample using another series time vector
            tsComboList = get(h.Handles.COMBts,'String');
            selectedTimeseriesName = tsComboList{get(h.Handles.COMBts,'Value')};
            thists = h.ViewNode.Plot.getTimeSeries(selectedTimeseriesName);
        end  
        if strcmp(h.ViewNode.Plot.AbsoluteTime,'on') % Convert start-end to abs time
            unitconv = tsunitconv('days',thists.TimeInfo.Units);
            if ~isempty(targetsts.TimeInfo.Format)
                start = datestr(unitconv*targetsts.TimeInfo.Start+...
                    datenum(targetsts.TimeInfo.StartDate,targetsts.TimeInfo.Format),...
                    targetsts.TimeInfo.Format);
                finish = datestr(unitconv*targetsts.TimeInfo.End+...
                    datenum(targetsts.TimeInfo.StartDate,targetsts.TimeInfo.Format),...
                    targetsts.TimeInfo.Format);
            else
                start = datestr(unitconv*targetsts.TimeInfo.Start+...
                    datenum(targetsts.TimeInfo.StartDate,targetsts.TimeInfo.Format));
                finish = datestr(unitconv*targetsts.TimeInfo.End+...
                    datenum(targetsts.TimeInfo.StartDate,targetsts.TimeInfo.Format));
            end
        else  % Convert start-end to timeplot units
            unitconv = tsunitconv(h.ViewNode.Plot.TimeUnits,targetsts.TimeInfo.Units);
            start  = sprintf('%0.4g %s',targetsts.TimeInfo.Start*unitconv,...
                h.ViewNode.Plot.TimeUnits);
            finish = sprintf('%0.4g %s',targetsts.TimeInfo.End*unitconv,...
                h.ViewNode.Plot.TimeUnits);
        end
    else
        mask = {'on','on','on','on','on','on','off','off','off','off','off'};

        %% Disabled radios must not be selected
        if get(h.Handles.RADIOtimeseries,'Value') || ...
                get(h.Handles.RADIOuniform,'Value')
            set(h.Handles.RADIOunion,'Value',true)
        end

        %% Find the start and end of the union on the overlapping interval
        %% TO DO: Deal with abs time
        if get(h.Handles.RADIOunion,'Value')
            start = inf;
            finish = -inf;
            for k=1:length(tsSelected)
                thists = h.ViewNode.Plot.getTimeSeries(tsSelected{k});
                unitconv = tsunitconv(h.ViewNode.Plot.TimeUnits,thists.TimeInfo.Units);
                if start>thists.timeInfo.Start*unitconv
                    start = thists.timeInfo.Start*unitconv;
                end
                if finish<thists.timeInfo.End*unitconv
                    finish = thists.timeInfo.End*unitconv;
                end            
            end
            if start>finish
                start = 'empty';
                finish = 'empty';
            else 
                start = sprintf('%0.4g %s',start,h.ViewNode.Plot.TimeUnits);
                finish = sprintf('%0.4g %s',finish,h.ViewNode.Plot.TimeUnits);
            end
        %% Find the intersection time vector
        elseif get(h.Handles.RADIOintersect,'Value')
            thists = h.ViewNode.Plot.getTimeSeries(tsSelected{1});
            time = thists.Time*tsunitconv(h.ViewNode.Plot.TimeUnits,thists.TimeInfo.Units);
            for k=1:length(tsSelected)
                thists = h.ViewNode.Plot.getTimeSeries(tsSelected{k});
                time = intersect(time,thists.Time*...
                    tsunitconv(h.ViewNode.Plot.TimeUnits,thists.TimeInfo.Units));
                if isempty(time)
                    break
                end
            end    
            if ~isempty(time)
                start = sprintf('%0.4g %s',min(time),h.ViewNode.Plot.TimeUnits);
                finish = sprintf('%0.4g %s',max(time),h.ViewNode.Plot.TimeUnits);
            else
                start = 'Empty';
                finish = 'Empty';
            end
        end       
    end    

    %% Update the time vector panel
    set(h.Handles.TXTstarttime,'String',sprintf('Start time: %s',start));
    set(h.Handles.TXTendtime,'String',sprintf('End time:  %s',finish));
end

%% Enable/disable the components using the mask 
for k=1:length(components)
    set(components{k},'Enable',mask{k})
end
