function [panel,helppanel] = getDialogInterface(this, manager)
% GETDIALOGINTERFACE Returns the handle of the Java dialog for left click on
% the tree nodes.

import java.io.*;
import javax.swing.*;

%% Target uitreePopupListener with the context menus for this node
%% This must be done before the panel is rendered to prevent the 
%% context menus and selected nodes getting out of synch
manager.Cmenulistener.rmenu = this.getPopupInterface(manager);



if isempty( this.Dialog )
  this.Dialog = getDialogSchema(this,manager);
end

%% Set the size
fpos = get(manager.Figure,'Position');
pnlpos = hgconvertunits(ancestor(this.Dialog,'figure'),...
    [manager.DialogPosition 0 fpos(3)-manager.DialogPosition-manager.HelpDialogPosition-1 ...
    fpos(4)],'Characters',get(this.Dialog,'Units'),get(this.Dialog,'Parent'));
set(this.Dialog,'Position',pnlpos)

%% Help panel
if isempty(this.HelpDialog)
    this.HelpDialog = uipanel('Parent',manager.Figure,'Units','Characters');
    % Build a JEditorPane using the html file stored in the HelpFile
    % property
    helpfile = File(this.HelpFile);
    helptext = JEditorPane(helpfile.toURL);
    helpscroll = JScrollPane(helptext);
    [jhelptext,helpcontainer] = javacomponent(helpscroll,[],manager.Figure);
    % Remove component resizing listener since we are not using normalized
    % units (performance reasons)
    L = get(helpcontainer,'Listeners__');
    for k=1:length(L)
        if strcmp(L(k).SourceObject.Name,'PixelBounds')
            L(k).Enable = 'off';
            break
        end
    end
    set(helpcontainer,'parent',this.HelpDialog,'units','normalized',...
        'position',[0 0 1 1])
     
    % Temporary listener to update the visibility of a uipanel's children
    helpdlghandle = handle(this.HelpDialog);
    setappdata(this.HelpDialog,'listener',handle.listener(helpdlghandle,...
        helpdlghandle.findprop('Visible'),'PropertyPostSet',...
        {@localVis helpdlghandle}));
end


%% Position the help panel on the right of the frame 
helppnlpos = hgconvertunits(ancestor(this.HelpDialog,'figure'),...
    [fpos(3)-manager.HelpDialogPosition 0 manager.HelpDialogPosition fpos(4)],...
    'Characters',get(this.HelpDialog,'Units'),get(this.HelpDialog,'Parent'));
set(this.HelpDialog,'Position',helppnlpos)

%% Return the main panel
panel = this.Dialog;
helppanel = this.HelpDialog;

%% Send a resize event (since invisible panels have been prevented from 
%% responsing to resize events)
resizefcn = get(this.Dialog,'ResizeFcn');
if ~isempty(resizefcn)
   feval(resizefcn{1},handle(this.Dialog),1,resizefcn{2:end});
end

function localVis(es,ed,h)

%% Temporary callback function to toggle the visibility of uipanel children
c = get(h,'Children');
for k=1:length(c(:))
    set(c(k),'Visible',get(h,'Visible'))
end

