function Panel = getDialogSchema(this, manager)

import javax.swing.*;

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

%% Install listeners 
this.state_listeners;

%% Show the panel
set(this.Handles.PNLViews,'Visible','on')
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
 
import javax.swing.*;

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

%% Build view panel
h.Handles.PNLViews = uipanel('Parent',f,'Units','Normalized','Title',...
    'Current Plots','Position',[0.02 0.52 0.96 0.46]);
h.viewstable
set(h.Handles.PNLviewsTable,'Units','Pixels','Parent',h.Handles.PNLViews);
BTNadd = uicontrol('Style','Pushbutton','String','Add Plot','Parent',h.Handles.PNLViews,...
    'Units','Pixels','Callback',@(es,ed) addplot(h,manager));
BTNremove = uicontrol('Style','Pushbutton','String','Remove Plot','Parent',h.Handles.PNLViews,...
    'Units','Pixels','Callback',{@localRemoveView h});
BTNedit = uicontrol('Style','Pushbutton','String','Edit Plot...','Parent',h.Handles.PNLViews,...
    'Units','Pixels','Callback',{@localEditView h});

set(h.Handles.PNLViews,'ResizeFcn',{@PNLViewsResize h.Handles.PNLviewsTable BTNadd BTNremove BTNedit})
PNLViewsResize(h.Handles.PNLViews,[],h.Handles.PNLviewsTable,BTNadd,BTNremove,BTNedit)

%% List selection listener must update time series table changes status
listSelectionListener = ...
    handle(h.Handles.viewsTable.getTable.getSelectionModel,'callbackproperties');
listSelectionListener.ValueChangedCallback = {@localViewSelection h};

%% Build time series panel
h.Handles.PNLTs = uipanel('Parent',f,'Units','Normalized','Title', ...
    'Time Series in Selected View','Position',[0.02 0.02 0.96 0.46]);

%% React to current selection
localViewSelection([],[],h)

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 PNLViewsResize(es,ed,PNLviewsTable,BTNadd,BTNremove,BTNedit)

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

%% Resize fcn for the views pane;

%% Get sizes and margins
% Hack to avoid recursion when resetting units to get the panel size
if strcmpi(get(es,'Units'),'Pixels')  
    return
end
set(es,'Units','Pixels')
pos = get(es,'Position');
set(es,'Units','Normalized')
margin = 10;

%% Set positions
btnSize = get(BTNremove,'Extent');
set(BTNadd,'Position',[max(1,pos(3)-btnSize(3)-margin) margin btnSize(3:4)])
set(BTNremove,'Position',[max(1,pos(3)-2*btnSize(3)-2*margin) margin btnSize(3:4)])
set(BTNedit,'Position',[max(1,pos(3)-3*btnSize(3)-3*margin) margin btnSize(3:4)])
set(PNLviewsTable,'Position', ...
    [margin btnSize(4)+2*margin max(1,pos(3)-2*margin) ...
         max(1,pos(4)-(btnSize(4)+2*margin)-2*margin)])

     
function localViewSelection(eventSrc,eventData,h)

%% Find the selected view and create a listener to track changes in its
%% member time series
selRow = h.Handles.viewsTable.getTable.getSelectedRow+1;
if selRow>0
    viewTableData = cell(h.Handles.viewsTable.Data);
    selectedView = h.getChildren('Label',viewTableData{selRow,1});
else
    selectedView = [];
end

%% Refresh the time series table
tstable(h,selectedView)

function localRemoveView(eventSrc,eventData,h)

%% Remove view button callback which detaches the view node (whose
%% listeners then close the view etc.)
selRow = h.Handles.viewsTable.getTable.getSelectedRow+1;
if selRow>0
    thisview = h.getChildren('Label',h.Handles.viewsTable.Data(selRow,1));
    h.removeNode(thisview);
end

function localEditView(eventSrc,eventData,h)

%% Edit view button callback which detaches Opens the PlotTool 
%% Property Editor
selRow = h.Handles.viewsTable.getTable.getSelectedRow+1;
if selRow>0
    thisview = h.getChildren('Label',h.Handles.viewsTable.Data(selRow,1));
    if ~isempty(thisview.Plot)
        propedit(thisview.Plot.AxesGrid.Parent);
    end
end


