function initialize(h)

%% Builds the Data Selection GUI

import javax.swing.*; 
offon = {'off';'on'};

%% Main figure
h.Figure = figure('Units','Characters','Position',[50 20 91 44],'Toolbar',...
    'None','Numbertitle','off','Menubar','None','Name','Preprocess Data',...
    'Visible','off','closeRequestFcn',@(es,ed) set(h,'Visible','off'),...
    'HandleVisibility','callback');

%% Top Time series selection panel
PNLselect = uipanel('Parent',h.Figure,'Units','Characters','Position',...
    [2.4 32.07 86 10.769],'Title','Select the View and Time Series');
h.Handles.LBLselts = uicontrol('Style','Text','Parent',PNLselect,'Units','Characters','Position',...
    [3 7.154 20 1.154],'String','Select view','HorizontalAlignment',...
    'Left');
h.Handles.COMBOselectView = uicontrol('Style','Popupmenu','Parent', ...
    PNLselect,'Units','Characters','Position',[20.6 6.769 60 1.692],...
    'String',{''},'Callback',{@localSwitchView h},'BackgroundColor',[1 1 1]);
[h.Handles.tsTable, tsTablePanel] = tsuitable(h.Figure,repmat({' '},[1 3]), ...
    {'Modify (y/n)?','Timeseries','Selected column(s)'});
tsCustomizeUitable(h.Handles.tsTable,2)
set(tsTablePanel,'Parent',PNLselect,'Units','Characters','Position',...
    [2.6 1 79 5.154])
set(h.Handles.tsTable,'DataChangedCallback',{@localTsChanged h});
h.Handles.tsTable.setCheckBoxEditor(1);

%% Dialog buttons
BTNok = uicontrol('style','Pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[42 1 13.8 1.769],'String','OK','Callback',{@localOK h});
BTNapply = uicontrol('style','Pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[58 1 13.8 1.769],'String','Apply','Callback',@(es,ed) eval(h));
BTNcancel = uicontrol('style','Pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[74 1 13.8 1.769],'String','Cancel','Callback', ...
    @(es,ed) set(h,'Visible','off'));

%% Build detrending button group
BTNgrpdetrend = uibuttongroup('Parent',h.Figure,'Units','Characters','Position',...
    [2.4 25.154 86 6.154],'Title','Specify detrending action','SelectionChangeFcn',...
    {@localDetrend h});
h.Handles.CHKdetrend = uicontrol('Style','checkbox','Parent',BTNgrpdetrend,'Units','Characters',...
    'Position',[2.4 3.08 14.6 1.154],'String','Detrending','Callback',...
    @(es,ed) set(h,'Detrendactive',offon{get(es,'Value')+1}));
h.Handles.RADIOconst = uicontrol('Style','radiobutton','Parent',BTNgrpdetrend,'Units','Characters',...
    'Position',[25.8 3.077 16.4 1.154],'Value',1,'String','Constant');
h.Handles.RADIOlinear = uicontrol('Style','radiobutton','Parent',BTNgrpdetrend,'Units','Characters',...
    'Position',[25.8 1.077 16.4 1.154],'Value',0,'String','Linear');


%% Build filter panel
PNLfilter = uipanel('Parent',h.Figure,'Units','Characters','Position',...
    [2.4 15.308 86 9.231],'Title','Specify filtering action');
h.Handles.CHKfilter = uicontrol('Style','checkbox','Parent',PNLfilter,'Units','Characters',...
    'Position',[2 6.077 14.6 1.154],'String','Filtering','Callback',...
    @(es,ed) set(h,'Filteractive',offon{get(es,'Value')+1}));
TXTfiltertype = uicontrol('Style','text','Parent',PNLfilter,'Units','Characters',...
    'Position',[16.8 5.923 10.6 1.154],'String','Filter type');
filttypes = {'First order (1/(1+ time constant * s))';...
             'Discrete transfer fcn (B(z^-1)/A(z^-1))';...
             'Ideal pass or stop'};
COMBOfiltertype = uicontrol('Style','popupmenu','Parent',PNLfilter,...
    'Units','Characters',...
    'Position',[32.6 5.692 48 1.692],'String',...
    filttypes,'Callback',...
    {@localSetFlterType h},'Backgroundcolor',[1 1 1]);

%% Build time constant panel
h.Handles.PNLtimeconst = uipanel('Parent',PNLfilter,'Units','Characters','Position',...
    [15 0.558 57.4 4.692],'Bordertype','None');
TXTtimeconst = uicontrol('Style','text','Parent',h.Handles.PNLtimeconst, ...
    'Units','Characters','Position',[2 2.692 14.6 1.154],'String', ...
    'Time constant','HorizontalAlignment','Left');
EDITtimeconst = uicontrol('Style','edit','Parent',h.Handles.PNLtimeconst, ...
    'Units','Characters','Position',[17.8 2.5 48 1.615],'String','10',...
    'HorizontalAlignment','Left','Callback',...
    {@localParseFilterSpec h 'Timeconst' '10'},'Backgroundcolor',[1 1 1]);
%set(EDITtimeconst,'Callback',@(es,ed) set(h.Handles.CHKfilter,'Value',~isempty(get(EDITtimeconst,'String'))))

%% Build transfer fcn panel
h.Handles.PNLtransfcn = uipanel('Parent',PNLfilter,'Units','Characters','Position',...
    [15 0.558 57.4 4.692],'Bordertype','None','Visible','off');
TXTAcoeff = uicontrol('Style','text','Parent',h.Handles.PNLtransfcn,'Units','Characters',...
    'Position',[2 2.692 14.6 1.154],'String','A coefficients','HorizontalAlignment','Left');
EDITAcoeff = uicontrol('Style','edit','Parent',h.Handles.PNLtransfcn,'Units','Characters',...
    'Position',[17.8 2.385 48 1.615],'HorizontalAlignment','Left','String',...
    '[1 -0.5]','Callback',{@localParseFilterSpec h 'Acoeffs' '[1 -0.5]'},...
    'Backgroundcolor',[1 1 1]);
TXTBcoeff = uicontrol('Style','text','Parent',h.Handles.PNLtransfcn,'Units', ...
    'Characters','Position',[2 0.538 14.6 1.154],'String','B coefficients',...
    'HorizontalAlignment','Left');
EDITBcoeff = uicontrol('Style','edit','Parent',h.Handles.PNLtransfcn,'Units',...
    'Characters','Position',[17.8 0.231 48 1.615],'HorizontalAlignment',...
    'Left','String','1','Callback', ...
    {@localParseFilterSpec h 'Bcoeffs' '1'},'Backgroundcolor',[1 1 1]);

%% Build ideal filter panel
passstopstr = {'pass','stop'};
h.Handles.PNLideal = uipanel('Parent',PNLfilter,'Units','Characters','Position',...
    [15 0.558 57.4 4.692],'Bordertype','None','Visible','off');
TXTpassstop = uicontrol('Style','text','Parent',h.Handles.PNLideal,'Units','Characters',...
    'Position',[2 2.692 23.6 1.154],'String','Specify pass/stop band','HorizontalAlignment','Left');
COMBpassstop = uicontrol('Style','popupmenu','Parent',h.Handles.PNLideal,'Units','Characters',...
    'Position',[26.8 2.385 39 1.615],'String',passstopstr,'Callback',...
    @(es,ed) set(h,'Band',passstopstr{get(es,'Value')}),...
    'Backgroundcolor',[1 1 1]); 
TXTidealRange = uicontrol('Style','text','Parent',h.Handles.PNLideal,'Units','Characters',...
    'Position',[2 0.538 23.6 1.154],'String','Range','HorizontalAlignment','Left');
EDITidealRange = uicontrol('Style','edit','Parent',h.Handles.PNLideal,'Units','Characters',...
    'Position',[26.8 0.231 39 1.615],'HorizontalAlignment','Left','String',...
    '[0 0.1]','Callback',{@localParseFilterSpec h 'Range' '[0 0.1]'},...
    'Backgroundcolor',[1 1 1]);

%% Build missing button group. TO DO Implement radio button callbacks
BTNgrpmissing = uibuttongroup('Parent',h.Figure,'Units','Characters','Position',...
    [2.4 3.769 86 11.154],'Title','Specify Action on Missing Data');
h.Handles.CHKremovetimes = uicontrol('Style','checkbox','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[2 7.538 25.8 1.308],'String','Remove times where','Callback',...
    @(es,ed) set(h,'Rowremove',offon{get(es,'Value')+1}));
h.Handles.COMBremovetimes = uicontrol('Style','popupmenu','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[29.8 7.231 10.1 1.692],'String',{'All','Any'},'callback',...
    {@localAnyAllChange h},...
    'Backgroundcolor',[1 1 1]);
TXTremovetimes = uicontrol('Style','text','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[41.8 7.538 20.8 1.154],'String','data is missing, then');
h.Handles.CHKinterp = uicontrol('Style','checkbox','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[2 5 43.8 1.538],'String', ...
    'Interpolate remaining missing data using','Callback',...
    @(es,ed) set(h,'Interpolate',offon{get(es,'Value')+1}));
set(BTNgrpmissing,'SelectionChangeFcn',@(es,ed) set(h.Handles.CHKinterp,'Value',true))
h.Handles.RADIOthists = uicontrol('Style','radiobutton','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[9.8 3.769-.462 28.4 1.154],'String','This time series','Value',1);
h.Handles.RADIOotherts = uicontrol('Style','radiobutton','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[9.8 1.846-.462 45.2 1.154],'String','Time series with equal # of columns ');
h.Handles.COMBotherts = uicontrol('Style','popupmenu','Parent',BTNgrpmissing,'Units','Characters',...
    'Position',[49.2 1 31 1.692],'String',{''},'BackgroundColor',[1 1 1]);

%% Set the figure background to match the panels
set(h.Figure,'color',get(PNLfilter,'backgroundcolor'))

%% Install general listeners
h.generic_listeners

%% Put the dialog in refreshed state
h.reset

%% Install filtering object listeners
h.addlisteners(handle.listener(h,h.findprop('Filter'), ...
      'PropertyPostSet',{@localFilterTypeChange h h.Handles}));
  
localTsChanged([],[],h)

function localOK(eventSrc, eventData, h)

%% OK button callback 
h.eval
h.Visible = 'off';

function localSwitchView(eventSrc, eventData, h)

%% View combo callback which changes the viewNode
ind = get(eventSrc,'Value');
views = get(eventSrc,'Userdata');
h.ViewNode = views(ind);

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


function localDetrend(eventSrc,eventData,h)

%% Callback for detrend radio buttons which sets the Detrend type prop
if get(h.Handles.RADIOconst,'Value')==1
    h.Detrendtype = 'constant';
else
    h.Detrendtype = 'linear';
end
set(h.Handles.CHKdetrend,'Value',true)
chkcallback = get(h.Handles.CHKdetrend,'Callback'); % Fire the callback
feval(chkcallback,h.Handles.CHKdetrend,[]);
    
function localSetFlterType(eventSrc,eventData,h)

%% Filter type combo callback which sets the filter rule
filter_types = {'firstord','transfer','ideal'};
h.Filter = filter_types{get(eventSrc,'Value')};
set(h.Handles.CHKfilter,'Value',true)
chkcallback = get(h.Handles.CHKfilter,'Callback'); % Fire the callback
feval(chkcallback,h.Handles.CHKfilter,[]);
    
function localFilterTypeChange(eventSrc,eventData,h,Handles)

%% Filterrule Filter property callback which changes the filter definition
%% panel visibility
switch h.Filter
    case 'firstord'
        set(Handles.PNLtransfcn,'Visible','off')
        set(Handles.PNLideal,'Visible','off')
        set(Handles.PNLtimeconst,'Visible','on')       
    case 'transfer'
        set(Handles.PNLtimeconst,'Visible','off')
        set(Handles.PNLideal,'Visible','off')
        set(Handles.PNLtransfcn,'Visible','on')       
    case 'ideal'
        set(Handles.PNLtransfcn,'Visible','off')
        set(Handles.PNLtimeconst,'Visible','off')
        set(Handles.PNLideal,'Visible','on')
end

function localParseFilterSpec(eventSrc,eventData,h,prop,defaultVal)

%% Parses filter specifications - reseting invalid entries
numval = eval(get(eventSrc,'String'),'[]');
if isempty(numval)
    set(eventSrc,'String',defaultVal) % Reset to default
else
    set(h,prop,numval) % Set the object property to the evaluated value
    set(h.Handles.CHKfilter,'Value',true)
    chkcallback = get(h.Handles.CHKfilter,'Callback'); % Fire the callback
    feval(chkcallback,h.Handles.CHKfilter,[]);
end
 
function localAnyAllChange(es,ed,h)

%% Callback for any/all combo
offon = {'off';'on'};
set(h,'Rowor',offon{get(es,'Value')})
set(h.Handles.CHKremovetimes,'Value',true)
set(h.Handles.RADIOthists,'Enable',offon{3-get(es,'Value')})  
set(h.Handles.RADIOotherts,'Enable',offon{3-get(es,'Value')})  
set(h.Handles.COMBotherts,'Enable',offon{3-get(es,'Value')})  
set(h.Handles.CHKinterp,'Enable',offon{3-get(es,'Value')}) 