function Panel = getDialogSchema(this, manager)

import javax.swing.*;

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

%% Populate the view combo
viewnodenames = get(this.up.getChildren,{'Label'});
set(this.Handles.COMBselectView,'String',viewnodenames,...
    'Value',find(strcmp(this.Label,viewnodenames)))

%% Listener to keep the time series table synched with the view
this.addListeners(handle.listener(this,'tschanged', @(es,ed) tstable(this)));

%% Show the panel
set(this.Handles.PNLTs,'Visible','on')
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}));

function f = localBuildPanel(thisfig,h,manager)

%% Create and position the components on the panel
 
%% Build upper combo and label
f = uipanel('Parent',thisfig,'Units','Normalized');
TXTselectView = uicontrol('Style','text','Parent',f,'Units',...
    'Pixels','String','Select view','HorizontalAlignment','Left');
h.Handles.COMBselectView = uicontrol('Style','popup','Parent',...
    f,'Units','Pixels','String',{'test'},'BackgroundColor',[1 1 1],'Callback',...
    {@localViewCombCallback h manager}); 

%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% Build axes panel
% h.Handles.PNLAxes = uipanel('Parent',f,'Units','Pixels','Title','Define Axes');
% h.axestable
% set(h.Handles.PNLaxesTable,'Units','Pixels','Parent',h.Handles.PNLAxes);
% BTNup = uicontrol('Style','Pushbutton','String','Add Axes','Parent',h.Handles.PNLAxes,...
%     'Units','Pixels','Callback',@(es,ed) addaxes(get(h,'Plot')));
% jaxestable = h.Handles.axesTable.getTable;
% BTNdwn = uicontrol('Style','Pushbutton','String','Remove Axes','Parent',h.Handles.PNLAxes,...
%     'Units','Pixels','Callback',@(es,ed) rmaxes(get(h,'Plot'),getSelectedRow(jaxestable)+1));
% set(h.Handles.PNLAxes,'ResizeFcn',{@PNLAxesResize h.Handles.PNLaxesTable BTNup BTNdwn})
% PNLAxesResize(h.Handles.PNLAxes,[],h.Handles.PNLaxesTable,BTNup,BTNdwn)
% %h.axespanel;
% 
% %% Build time panel
% domainpnl(h,f);
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Build time series panel
h.Handles.PNLTs = uipanel('Parent',f,'Units','Pixels','Title', ...
    'Define Displayed Time Series','Visible','off');
h.tstable;
h.Handles.BTNremoveTs = uicontrol('Style','pushbutton','Parent', ...
    h.Handles.PNLTs,'Units','Pixels','String','Remove Time Series From View',...
    'Callback',{@localRmTimeseries h});
h.Handles.BTNEditView = uicontrol('Style','pushbutton','Parent', ...
    h.Handles.PNLTs,'Units','Pixels','String','Edit View...',...
    'Callback',{@localEditView h});
set(h.Handles.PNLTs,'ResizeFcn',{@PNLTsResize ...
    h.Handles.BTNremoveTs h.Handles.BTNEditView h.Handles.PNLTsTable})
PNLTsResize(h.Handles.PNLTs,[],h.Handles.BTNremoveTs,h.Handles.BTNEditView,...
    h.Handles.PNLTsTable);

%% Build annoation panel
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%charpnl(h,f);
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Resize behavior
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set(f,'ResizeFcn',{@localFigResize h.Handles.PNLAxes h.Handles.PNLDomain h.Handles.PNLTs h.Handles.PNLAnnoation ...
%     h.Handles.COMBselectView TXTselectView});
set(f,'ResizeFcn',{@localFigResize h.Handles.PNLTs ...
    h.Handles.COMBselectView TXTselectView});
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function localSetVisible(es,ed,h)

children = h.find('-depth',inf);
for k=1:length(children)
    if isa(children(k),'uicontainer')
       set(children(k),'Visible',get(h,'Visible'));
    end
end
     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Resize functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
function PNLTsResize(es,ed,BTNremoveTs,BTNEditView,PNLTsTable)

%% Resize fcn for the time series pane;

%% Get sizes and margins
pos = get(es,'Position');
margin = 10;

%% Set positions
btnSize = get(BTNremoveTs,'Extent');
rmBtnSize = [max(1,pos(3)-btnSize(3)-margin) margin btnSize(3:4)];
set(BTNremoveTs,'Position',rmBtnSize)
btnSize = get(BTNEditView,'Extent');
set(BTNEditView,'Position',[max(1,rmBtnSize(1)-margin-btnSize(3)) margin btnSize(3:4)])
set(PNLTsTable,'Position', ...
    [margin btnSize(4)+2*margin max(1,pos(3)-2*margin) ...
         max(1,pos(4)-(btnSize(4)+2*margin)-2*margin)])


function PNLAxesResize(es,ed,PNLaxesTable,BTNup,BTNdwn)

%% Resize fcn for axes panel
pos = get(es,'Position');
margin = 10;

%% Set positions
btnAddSize = get(BTNup,'Extent');
btnRemoveSize = get(BTNdwn,'Extent');
set(PNLaxesTable,'Position',[margin,2*margin+btnAddSize(4),max(1,pos(3)-2*margin),...
    max(1,pos(4)-4*margin-btnAddSize(4))])
set(BTNup, 'Position', [max(1,pos(3)-2*margin-btnAddSize(3)-btnRemoveSize(3)) margin ...
    btnAddSize(3:4)]);
set(BTNdwn, 'Position', [max(1,pos(3)-margin-btnRemoveSize(3)) margin ...
    btnRemoveSize(3:4)]);


%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function localFigResize(es,ed,PNLAxes,PNLDomain,PNLTs,PNLAnnoation, ...
%     COMBselectView,TXTselectView)
% 
% %% Resize callback for view panel
% 
% gap = 3; % Interpanel gap
% timePanelHeight = 141;
% fixedVsize = 10+22+timePanelHeight+10+3*gap;
% 
% %% Components and panels are repositioned relative to the main panel
% mainpnlpos = hgconvertunits(ancestor(es,'figure'),get(es,'Position'),get(es,'Units'),...
%     'Pixels',get(es,'Parent'));
% 
% %% Set the time series selector size
% set(TXTselectView,'Position',[10 mainpnlpos(4)-24 119 15]);
% set(COMBselectView,'Position',[100 mainpnlpos(4)-30 mainpnlpos(3)-100-24 22]);
% 
% %% Set the annotations panel to take up all the space horizonatally
% %% and 1/3 of the available vertical space
% PNLAnnoationVsize = (mainpnlpos(4)-fixedVsize)/3;
% pnlpos = hgconvertunits(ancestor(PNLAnnoation,'figure'),[10 11 mainpnlpos(3)-24 PNLAnnoationVsize],'Pixels',...
%     get(PNLAnnoation,'Units'),get(PNLAnnoation,'Parent'));
% set(PNLAnnoation,'Position',pnlpos);
% 
% 
% %% Set the time series panel to take up all the space horizonatally
% %% and 1/3 of the available vertical space
% PNLTsVsize = (mainpnlpos(4)-fixedVsize)/3;
% pnlpos = hgconvertunits(ancestor(PNLTs,'figure'),...
%     [10 PNLAnnoationVsize+11+gap mainpnlpos(3)-24 PNLTsVsize],'Pixels',...
%     get(PNLTs,'Units'),get(PNLTs,'Parent'));
% set(PNLTs,'Position',pnlpos);
% 
% 
% %% Set the time panel to take up all the space horizonatally
% %% and fixed amount of vertical space
% pnlpos = hgconvertunits(ancestor(PNLDomain,'figure'),...
%     [10 PNLAnnoationVsize+11+PNLTsVsize+2*gap ...
%     mainpnlpos(3)-24 timePanelHeight],'Pixels',...
%     get(PNLDomain,'Units'),get(PNLDomain,'Parent'));
% set(PNLDomain,'Position',pnlpos);
% 
% 
% 
% %% Set the axes panel to take up all the space horizonatally
% %% and 1/3 of the available vertical space
% PNLAxesVsize = (mainpnlpos(4)-fixedVsize)/3;
% pnlpos = hgconvertunits(ancestor(PNLAxes,'figure'),...
%     [10 PNLAnnoationVsize+11+PNLTsVsize+timePanelHeight+3*gap ...
%     mainpnlpos(3)-24 PNLAxesVsize],'Pixels',...
%     get(PNLAxes,'Units'),get(PNLAxes,'Parent'));
% set(PNLAxes,'Position',pnlpos);

function localFigResize(es,ed,PNLTs,COMBselectView,TXTselectView)

%% Resize callback for view 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

gap = 3; % Interpanel gap
timePanelHeight = 141;
fixedVsize = 10+22+timePanelHeight+10+3*gap;

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

%% Set the time series selector size
set(TXTselectView,'Position',[10 max(1,mainpnlpos(4)-24) 119 15]);
set(COMBselectView,'Position',[100 max(1,mainpnlpos(4)-30) max(1,mainpnlpos(3)-124) 22]);

%% Set the time series panel to take up all the space horizonatally
%% and 1/3 of the available vertical space
PNLTsVsize = (max(1,mainpnlpos(4)-fixedVsize))/3;
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pnlpos = hgconvertunits(ancestor(PNLTs,'figure'),...
%     [10 11+gap mainpnlpos(3)-24 PNLTsVsize],'Pixels',...
%     get(PNLTs,'Units'),get(PNLTs,'Parent'));
pnlpos = hgconvertunits(ancestor(PNLTs,'figure'),...
    [10 10 max(1,mainpnlpos(3)-20) max(1,mainpnlpos(4)-50)],'Pixels',...
    get(PNLTs,'Units'),get(PNLTs,'Parent'));
set(PNLTs,'Position',pnlpos);
%%%%%%%%%%%%%%%%%%%%% PLOTTOOL INTEGRATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function localViewCombCallback(eventSrc,eventData,h,manager)

%% Find the view node corresponding to the selected combo item
combpos = get(eventSrc,'Value');
combvals = get(eventSrc,'String');
Iold = find(strcmp(combvals,h.Label));
Inew = find(strcmp(get(h.up.getviews,{'Label'}),combvals(combpos)));

%% Programatically hit the corresponding node
if ~isempty(Inew)
    manager.Tree.setSelectedNode(h.up.getviews(Inew(1)).getTreeNodeInterface)
end

%% Set the combo back to its former value so that the Dialog panel remains
%% in sync with its corresponding node. This creates the illusion that the
%% view selection panel is independent of the selected node
set(eventSrc,'value',Iold(1))

function localRmTimeseries(eventSrc,eventData,h)

%% Remove time series button callback
selectedRow = h.Handle.tsTable.getTable.getSelectedRow+1;
if selectedRow>0
    h.Plot.rmwave(h.Plot.waves(selectedRow));
end
h.send('tschanged',handle.EventData(h,'tschange'));


function localEditView(eventSrc,eventData,this)

%% Edit view button callback which detaches Opens the PlotTool 
%% Property Editor
if ~isempty(this.Plot)
    propedit(this.Plot.AxesGrid.Parent);
else
    errordlg('No plot has been created to edit','Time Series Tools','modal')
end


