function initialize(h)

%% Builds the Data Selection GUI

import javax.swing.*; 

%% Main figure
h.Figure = figure('Units','Characters','Position',[104 21.7627 85.4 39.7573],'Toolbar','None',...
    'Menubar','None','NumberTitle','off','Name','Merge/Resample Time Series',...
    'Visible','off','closeRequestFcn',@(es,ed) set(h,'Visible','off'),'HandleVisibility',...
    'callback');

%% View selector
h.Handles.COMBOselectView = uicontrol('style','popupmenu','String',{'  '},'Units',...
    'Characters','Parent',h.Figure,'Position',[29 37.0658 53 1.6918], ...
    'BackgroundColor',[1 1 1],'Callback',{@localSwitchView h});

%% Build buttons
h.Handles.BTNok = uicontrol('String','OK','Parent',h.Figure,'Units','Characters','Position',...
    [53.6 1.1535 13.8 1.7687],'Callback',{@localOK h});
h.Handles.BTNcancel = uicontrol('String','Cancel','Parent',h.Figure,'Units','Characters','Position',...
    [70 1.1535 13.8 1.7687],'Callback',@(es,ed) set(h,'Visible','off'));

%% Build time series panel
PNLselectTs = uibuttongroup('Parent',h.Figure,'Units','Characters','Position', ...
    [2.2 21.7627 80.8 15],'Title','Define Time Series');
[h.Handles.tsTable, tsTablePanel] = tsuitable(h.Figure,repmat({false ''},[3 1]), ...
    {' ','Time series'});
tsCustomizeUitable(h.Handles.tsTable,2);
set(h.Handles.tsTable,'DataChangedCallback',{@localTsChanged h});
h.Handles.tsTable.setCheckBoxEditor(1);
set(tsTablePanel,'Units','Pixels','Parent',PNLselectTs,'Position', ...
    hgconvertunits(ancestor(PNLselectTs,'figure'),[3.2 5.5368 74.4 7.6900],...
    'Characters','Pixels',PNLselectTs))
h.Handles.RADIOinplace = uicontrol('style','radiobutton','String',...
    'Merge time series in place','Units',...
    'Characters','Parent',PNLselectTs,'Position',[3.8 3.2298 40.8 1.1535],'Value',1);
h.Handles.RADIOnewmerged = uicontrol('style','radiobutton','String',...
    'Create new time series to represent merged data','Units',...
    'Characters','Parent',PNLselectTs,'Position',[3.8 1.4611 52.8 1.1535]);

%% Build time interval panel
PNLInterval = uibuttongroup('Parent',h.Figure,'Units','Characters','Position', ...
    [2.2 9.6894 81.6 11.5350],'Title','Specify New Time Vector','SelectionChangeFcn',...
    @(es,ed) refresh(h));
h.Handles.RADIOunion = uicontrol('style','radiobutton','String',...
    'Union of time vectors on the interval where they overlap','Units',...
    'Characters','Parent',PNLInterval,'Position',[3.2 8.459 70 1.1535],'Value',1);
h.Handles.RADIOintersect = uicontrol('style','radiobutton','String',...
    'Intersection of time vectors on the interval where they overlap','Units',...
    'Characters','Parent',PNLInterval,'Position',[3.2 6.0751 70 1.1535]);
h.Handles.RADIOuniform = uicontrol('style','radiobutton','String',...
    'Uniform time vector with time interval','Units',...
    'Characters','Parent',PNLInterval,'Position',[3.2 3.6912 40.8 1.1535]);
h.Handles.RADIOtimeseries = uicontrol('style','radiobutton','String',...
    'Use time vector from time series','Units',...
    'Characters','Parent',PNLInterval,'Position',[3.2 1.3842 40.8 1.1535]);
h.Handles.EDITinterval = uicontrol('style','edit','Units',...
    'Characters','Parent',PNLInterval,'Position',[44.2 3.3836  16.4 1.6149],'String','1',...
    'HorizontalAlignment','Left','BackgroundColor',[1 1 1]);
h.Handles.COMBunits = uicontrol('style','popupmenu','Units',...
    'Characters','Parent',PNLInterval,'Position',[62.6 3.3067 15 1.6918],'String',...
    get(findtype('TimeUnits'),'String'),'Value',6,'BackgroundColor',[1 1 1]);
h.Handles.COMBts = uicontrol('style','popupmenu','Units',...
    'Characters','Parent',PNLInterval,'Position',[44 0.9228 33.8 1.6918],...
    'BackgroundColor',[1 1 1],'String',{' '},'Value',1);

%% Build time panel
PNLnewTime = uipanel('Parent',h.Figure,'Units','Characters','Position', ...
    [2.2 3.6143 81.6 5.5368],'Title','New Time Vector');
h.Handles.TXTstarttime = uicontrol('style','text','Units',...
    'Characters','Parent',PNLnewTime,'Position',[3 2.353 74.8 1.3073],...
    'String','Start - end','HorizontalAlignment','Left');
h.Handles.TXTendtime = uicontrol('style','text','Units',...
    'Characters','Parent',PNLnewTime,'Position',[3 0.3536 74.8 1.3073],...
    'String','Number of samples','HorizontalAlignment','Left');

%% Make the figure background color match the uipanel color
set(h.Figure,'Color',get(PNLnewTime,'BackGroundColor'))

%% Install listeners
h.generic_listeners


function localTsChanged(eventSrc,eventData,h)

h.refresh

function localOK(eventSrc, eventData, h)

%% OK button callback to merge and resample the data
if h.eval;
   h.Visible = 'off';
end 

function localSwitchView(eventSrc, eventData, h)

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