function Panel = getDialogSchema(this, manager)

import javax.swing.*;

%% Create the node panel and components
Panel = localBuildPanel(manager.Figure,this,manager);

%% Install listeners which update the timeseries table
this.Listeners = [this.Listeners; ...
               handle.listener(this,'ObjectChildAdded',@(es,ed) tstable(this)); ...
               handle.listener(this,'ObjectChildRemoved',{@localRemoveTsNode this})
               handle.listener(this,'timeserieschange',@(es,ed) tstable(this))];



set(this.Handles.PNLTs,'Visible','on')
set(this.Handles.tsTable,'Visible',true)

%% Show the panel
figure(double(manager.Figure))

% Temporary listeners to update table visibility when the enclosing panel
% visibility is modified
p = handle(Panel);
this.addListeners(handle.listener(p,p.findprop('Visible'),'PropertyPostSet',...
    {@localSetVisible p}));

localSetVisible([],[],p)

function f = localBuildPanel(thisfig,h,manager)

%% Create and position the components on the panel
 
import javax.swing.*;

%% Build upper combo and label
f = uipanel('Parent',thisfig,'Units','Normalized'); 

%% Build time series panel
h.Handles.PNLTs = uipanel('Parent',f,'Units','Characters','Title',...
    'Time Series','Visible','off');
h.tstable
set(h.Handles.PNLtsTable,'Units','Pixels','Parent',h.Handles.PNLTs);
BTNexport = uicontrol('style','Pushbutton','Parent',...
    h.Handles.PNLTs,'String',' Export... ','Units','Characters',...
    'callback',{@localExport h manager});
BTNimport = uicontrol('style','Pushbutton','Parent',...
    h.Handles.PNLTs,'String',' Import ... ','Units','Characters',...
    'Callback',{@localImport manager});
BTNadd = uicontrol('Style','Pushbutton','String',' Insert ... ','Parent',h.Handles.PNLTs,...
    'Units','Characters','Callback',@(es,ed) addNode(h));
BTNremove = uicontrol('Style','Pushbutton','String',' Remove ','Parent',h.Handles.PNLTs,...
    'Units','Characters','Callback',{@localRemoveTs h});
set(h.Handles.PNLTs,'ResizeFcn',...
    {@PNLTsResize h.Handles.PNLtsTable BTNadd BTNremove BTNimport BTNexport})
PNLTsResize(h.Handles.PNLTs,[],h.Handles.PNLtsTable,BTNadd,BTNremove,BTNimport,BTNexport)

%% Assign and call the panel resize fcn
set(f,'ResizeFcn',...
    {@localFigResize h.Handles.PNLTs})
localFigResize(f,[],h.Handles.PNLTs)


function localSetVisible(es,ed,h)

children = h.find('-depth',inf,'-isa','uicontainer');
if ~isempty(children)
    set(children,'Visible',get(h,'Visible'));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Resize functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
function PNLTsResize(es,ed,PNLtsTable,BTNadd,BTNremove,BTNimport,BTNexport)

%% Time series panel resize calback. Puts the add and remove time series
%% buttons on the lower right and the table takes up the remaining space
charpixconvertY = 420/32.3077;
charpixconvertX = 560/112;
charpixconvert = [charpixconvertX charpixconvertY charpixconvertX charpixconvertY];

%% Get component sizes
pos = get(es,'Position');
btnSize = 1.2*get(BTNexport,'Extent');
margin = 1.5;

%% Set the sizes
set(BTNexport,'Position',[max(1,pos(3)-4*(btnSize(3)+margin)) margin btnSize(3:4)])
set(BTNimport,'Position',[max(1,pos(3)-3*(btnSize(3)+margin)) margin btnSize(3:4)])
set(BTNremove,'Position',[max(1,pos(3)-btnSize(3)-margin) margin btnSize(3:4)])
set(BTNadd,'Position',[max(1,pos(3)-2*(btnSize(3)+margin)) margin btnSize(3:4)])
set(PNLtsTable,'Position', ...
    charpixconvert.*([margin 2*margin+btnSize(4) max(1,pos(3)-2*margin) max(1,pos(4)-3*margin-btnSize(4))]))

function localRemoveTsNode(es,ed,h)

if strcmp(ed.Type,'ObjectChildRemoved')
    h.tstable(ed.Child);
end

function localRemoveTs(es,ed,this)

%% Remove ts callback

selectedRow = this.Handles.tsTable.getTable.getSelectedRow+1;
if selectedRow>0
    tsName = this.Handles.tsTable.Data(selectedRow,1);
    tsNode = this.getChildren('Label',tsName);
    if ~isempty(tsNode)
        this.removeNode(tsNode); % Listeners will update the table
    end
end

function localFigResize(es,ed,PNLts)

%% Resize callback for tsparentnode panel

%% No-op if the panel is inivible or if there is no eventData passed with
%% the firing resize event
if strcmp(get(es,'Visible'),'off') && isempty(ed)
    return
end

%% Components and panels are repositioned relative to the main panel
mainpnlpos = hgconvertunits(ancestor(es,'figure'),get(es,'Position'),get(es,'Units'),...
    'Characters',get(es,'Parent'));

%% Set the tstable panel to take up all the space above the import panel
set(PNLts,'Position',[2 1.4 max(1,mainpnlpos(3)-4) max(1,mainpnlpos(4)-2.4)]);

function localImport(es,ed,manager)

%% Import button callback
tsguis.ImportWizard(manager.Root.Tsviewer);

function localExport(es,ed,this,manager)

%% Fing the time series selected in the table
selectedRow = this.Handles.tsTable.getTable.getSelectedRow+1;
if selectedRow>0
    tsName = this.Handles.tsTable.Data(selectedRow,1);
    node = this.getChildren('Label',tsName);
else
    errordlg('Select the row corresponding to the time series you intent to export',...
        'Time Series Tools','modal')
end

%% Open export for selected time series node
dlg = tsguis.allExportdlg;
if isa(node,'tsguis.tsnode')
    dlg.initialize('file',manager.Figure,node.Timeseries);
else
    errordlg('Select the time series tree node which represents the time series you intent to export',...
        'Time Series Tools','modal')
end