function thisTab = lagpanel(this,h)

import com.mathworks.toolbox.timeseries.*;
import java.awt.*
import com.mathworks.mwswing.*;

%% Build freq panel
h.Handles.LagPanel = localBuildPanel(h,this);
thisTab = struct('Name','Domain','Handles',[]);
thisTab.Name = 'Domain';
h.Tabs = [h.Tabs; thisTab];
outerLagPanel = MJPanel(BorderLayout);
outerLagPanel.add(h.Handles.LagPanel,BorderLayout.WEST);
if h.Handles.TabPane.isValid
    awtinvoke(h.Handles.TabPane,'add(Ljava/lang/String;Ljava/awt/Component;)',...
        java.lang.String('Define Lags'),outerLagPanel);
else
    h.Handles.TabPane.add('Define Lags',outerLagPanel);
end

%% Listener to ViewChanged event which keeps the hist
%% panel updated
h.Listeners = [h.Listeners; handle.listener(this.AxesGrid,'ViewChange',...
    {@localupdatelags h this})];
localupdatelags([],[],h,this)


function LagPanel = localBuildPanel(h,this)

import com.mathworks.mwswing.*;
import javax.swing.*;
import java.awt.*;

%% Build panel
LagPanel = MJPanel;

%% Create components
LBLnumlagLeft = MJLabel('From :');
h.Handles.TXTnumlagLeft = MJTextField(12);
h.Handles.TXTnumlagLeft.setText('-10');
LBLnumlagRight = MJLabel('To :');
h.Handles.TXTnumlagRight = MJTextField(12);
h.Handles.TXTnumlagRight.setText('10');
%% Add callbacks
set(handle(h.Handles.TXTnumlagLeft,'callbackproperties'),'ActionPerformedCallback',...
    {@localupdatelags h this})
set(handle(h.Handles.TXTnumlagRight,'callbackproperties'),'ActionPerformedCallback',...
    {@localupdatelags h this})

%% Create inner panel and add components
PNLinner = MJPanel(GridLayout(2,2,5,5));
PNLinner.add(LBLnumlagLeft);
PNLinner.add(h.Handles.TXTnumlagLeft);
PNLinner.add(LBLnumlagRight);
PNLinner.add(h.Handles.TXTnumlagRight);

%% Finalize panel
LagPanel.add(PNLinner);

function localupdatelags(es,ed,h,this)

%% Update the lags property of the plot based on the definitions in the 
%% tsspecnode Dialog

left = str2double(char(h.Handles.TXTnumlagLeft.getText));
if isempty(left) || ~isscalar(left) || left-round(left)~=0 || isnan(left) || ~isfinite(left) 
    % Invalid number of lags - abort and revert to default
    h.Handles.TXTnumlagLeft.setText(mat2str(this.Lags(1)));
    return
end
right = str2double(char(h.Handles.TXTnumlagRight.getText));
if isempty(right) || ~isscalar(right) || right-round(right)~=0 || isnan(right) || ~isfinite(right) 
    % Invalid number of lags - abort and revert to default
    h.Handles.TXTnumlagRight.setText(mat2str(this.Lags(end)));
    return
end
if right>=left
   Lags = [left:right];    
else
   Lags = [right:left];    
   h.Handles.TXTnumlagLeft.setText(mat2str(right));
   h.Handles.TXTnumlagRight.setText(mat2str(left));
end
%% Apply the vector to the datafcn for each wave
if ~isequal(this.Lags,Lags);
    this.Lags = Lags;    
    for k=1:length(this.Responses)
        this.Responses(k).DataSrc.send('SourceChanged');
    end
end