function updatetime(view,h)

%% Callback for ViewChanged listener which keeps time domain
%% panel updated

xlim = num2cell(view.AxesGrid.getxlim(1));
[isformated,absflag] = tsIsDateFormat(view.TimeFormat);
if strcmp(view.Absolutetime,'on') %Absolute time
    % Convert the xlims to datestrs and write the result to the start
    % end end time edit boxes on the TimePnl
    if ~isempty(view.StartDate)
       xlim{1} = datestr(xlim{1}*tsunitconv('days',view.TimeUnits)+...
           datenum(view.StartDate),view.TimeFormat);
       xlim{2} = datestr(xlim{2}*tsunitconv('days',view.TimeUnits)...
           +datenum(view.StartDate),view.TimeFormat);
    else
       xlim = {'',''}; 
    end
    h.Handles.TimePnl.TXTStartTime.setText(xlim{1});
    h.Handles.TimePnl.TXTEndTime.setText(xlim{2});

    % Sets the format in the @timeplot and the TimePnl to the current
    % timeseries format
    formats = {};
    for k=1:h.Handles.TimePnl.COMBformatunits.getItemCount
        formats = [formats; {h.Handles.TimePnl.COMBformatunits.getItemAt(k-1)}];
    end
    ind = find(strcmp(view.TimeFormat,formats));
    localSetMode(h,true,ind)
else  % Relative time
    units = h.Handles.TimePnl.COMBformatunits.getSelectedItem;
    if isformated && ~absflag % Time vector is formated
        % Parse xlim into a formatted time string a write the result
        % to the start and end time edit boxes if the time radio
        % button is checked
        if h.Handles.TimePnl.RADIOStartTime.isSelected
            xlow = datestr(xlim{1}*...
                tsunitconv('days',view.TimeUnits),view.Timeformat);
            h.Handles.TimePnl.TXTStartTime.setText(xlow);
        end
        if h.Handles.TimePnl.RADIOEndTime.isSelected
            xhigh = datestr(xlim{2}*...
                tsunitconv('days',view.TimeUnits),view.Timeformat);
            h.Handles.TimePnl.TXTEndTime.setText(xhigh);
        end
        ind = find(strcmp(view.TimeFormat,units));
    else % Numerical time vector
        if h.Handles.TimePnl.RADIOStartTime.isSelected
           h.Handles.TimePnl.TXTStartTime.setText(sprintf('%0.2g',xlim{1}));
        end
        if h.Handles.TimePnl.RADIOEndTime.isSelected
            h.Handles.TimePnl.TXTEndTime.setText(sprintf('%0.2g',xlim{2}));
        end
        % Sets the units in the @timeplot and the TimePnl to the current
        % timeseries units
        ind = find(strcmp(view.TimeUnits,units));
    end
    localSetMode(h,false,ind)
end


% Update the events combo with the events currently displayed in
% the view . TO DO: Remove parent dependency
if ~isempty(view.Parent)
    evlist = view.Parent.getevents;
    if ~isempty(evlist)
        h.Handles.TimePnl.setevents(get(view.Parent.getevents,{'Name'}));
        awtinvoke(h.Handles.TimePnl.RADIOStartEvent,'setEnabled(Z)',true);
        awtinvoke(h.Handles.TimePnl.RADIOEndEvent,'setEnabled(Z)',true);
    else
        awtinvoke(h.Handles.TimePnl.RADIOStartTime,'setSelected(Z)',true);
        awtinvoke(h.Handles.TimePnl.RADIOStartEvent,'setEnabled(Z)',false);
        awtinvoke(h.Handles.TimePnl.RADIOEndTime,'setSelected(Z)',true);
        awtinvoke(h.Handles.TimePnl.RADIOEndEvent,'setEnabled(Z)',false);
    end
end
 

function localSetMode(h,state,ind)

if ~isempty(ind)
    h.Handles.TimePnl.setabstimemode(state,ind(1)-1);
else
    h.Handles.TimePnl.setabstimemode(state,0);
end
