function thisTab = freqpnl(view,h)

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

%% Build freq panel
h.Handles.FreqPnl = localBuildPanel(h,view);
thisTab = struct('Name','Domain','Handles',[]);
thisTab.Name = 'Domain';
h.Tabs = [h.Tabs; thisTab];
outerFreqPanel = MJPanel(BorderLayout);
outerFreqPanel.add(h.Handles.FreqPnl,BorderLayout.WEST);
h.Handles.TabPane.add('Define Frequency Vector',outerFreqPanel);     

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

function localUnitChange(es,ed,h,view)

%% Units combo callback. Note that the view change listener will
%% call the localRefresh fnc which will update the start and end edit
%% boxes

%% Get the selected units and assign them to the axesgrid
view.AxesGrid.Xunits = h.Handles.FreqPnl_COMBOunits.getSelectedItem;
for k=1:length(view.Waves)
    view.Waves(k).DataSrc.send('sourcechange');
end


function FreqPnl = localBuildPanel(h,view)

import com.mathworks.mwswing.*;


%% Build panel
FreqPnl = MJPanel;

%% Create components
LBLstart = MJLabel('Start frequency');
h.Handles.FreqPnl_EDITstart = MJTextField(7);
set(handle(h.Handles.FreqPnl_EDITstart,'callbackproperties'),'ActionPerformedCallback',...
   {@localFreqUpdate h view});
set(handle(h.Handles.FreqPnl_EDITstart,'callbackproperties'),'FocusLostCallback',...
   {@localFreqUpdate h view});
LBLend = MJLabel('End frequency');
h.Handles.FreqPnl_EDITend = MJTextField(7);
set(handle(h.Handles.FreqPnl_EDITend,'callbackproperties'),'ActionPerformedCallback',...
   {@localFreqUpdate h view});
set(handle(h.Handles.FreqPnl_EDITend,'callbackproperties'),'FocusLostCallback',...
   {@localFreqUpdate h view});
LBLunits = MJLabel('Units');
h.Handles.FreqPnl_COMBOunits = MJComboBox;
set(handle(h.Handles.FreqPnl_COMBOunits,'callbackproperties'),...
    'ActionPerformedCallback',{@localUnitChange h view})
funits = strcat('cyc/',get(findtype('TimeUnits'),'Strings'));
for k=1:length(funits)
   thisunit = funits{k};
   thisunit = thisunit(1:end-1); % Strip the last s
   h.Handles.FreqPnl_COMBOunits.addItem(thisunit);
end

%% Set the current units
funits = get(findtype('TimeUnits'),'Strings');
for k=1:length(funits)
    thisfunit = funits{k};
    funits{k} = sprintf('cyc/%s',thisfunit(1:end-1));
end
unitind = find(strcmp(view.Axesgrid.Xunits,funits));
if isempty(unitind)
    unitind = find(strcmp('cyc/sec',funits));
end
h.Handles.FreqPnl_COMBOunits.setSelectedIndex(unitind-1);

%% Add components
FreqPnl.add(LBLstart);
FreqPnl.add(h.Handles.FreqPnl_EDITstart);
FreqPnl.add(LBLend);
FreqPnl.add(h.Handles.FreqPnl_EDITend);
FreqPnl.add(LBLunits);
FreqPnl.add(h.Handles.FreqPnl_COMBOunits);

function localFreqUpdate(eventSrc,eventData,h,view)

%% Callback for start and end time edit boxes which updates the AxesGrid

%% Get the start and end times from the edit boxes and apply them to the
%% axesgrid
startfreq = str2num(h.Handles.FreqPnl_EDITstart.getText);
if isempty(startfreq)
    startfreq = 0;
end
endfreq = str2num(h.Handles.FreqPnl_EDITend.getText);
if isempty(endfreq)
    endfreq = inf;
end
view.AxesGrid.setxlim([startfreq endfreq]);

function localRefresh(es,ed,h,view)
%% Callback for ViewChanged listener which keeps the freq domain
%% panel updated

xlim = num2cell(view.AxesGrid.getxlim(1));

units = h.Handles.FreqPnl_COMBOunits.getSelectedItem;
h.Handles.FreqPnl_EDITstart.setText(sprintf('%0.2g',xlim{1}));
h.Handles.FreqPnl_EDITend.setText(sprintf('%0.2g',xlim{2}));