function initialize(h,manager,f,varargin)

%% Common initialization functions for all view nodes

import java.awt.dnd.*
import com.mathworks.toolbox.timeseries.*;
import javax.swing.tree.*;

%% Assign the Figure and Tree properties
set(f,'Units','Characters','closeRequestFcn',{@localClose h},'Toolbar', ...
    'figure')
h.Handles.InitTXT = uicontrol('style','text','string', ...
    'Drag and drop a time series node to view','Parent',f);
set(f,'ResizeFcn',{@localTxtResize h.Handles.InitTXT})
localTxtResize(f,[],h.Handles.InitTXT);
set(h,'Tree',manager.tree,'Figure',f);
% h.HelpFile = fullfile(matlabroot, 'toolbox', 'matlab', 'timeseries', ...
%                          '@tsguis', '@viewnode','tshelp.htm');
                     
%% Add tools menus
mtools = findobj(allchild(f),'type','uimenu','Tag','figMenuTools');
uimenu('Parent',mtools,'Label','Select data...','Callback',...
    @(es,ed) openselectdlg(h),'Separator','on');
uimenu('Parent',mtools,'Label','Merge/resample...','Callback',...
    @(es,ed) openmergedlg(h));
mpreproc = uimenu('Parent',mtools,'Label','Preprocess data');
uimenu('Parent',mpreproc,'Label','Remove missing data...','Callback',...
    @(es,ed) openpreprocdlg(h));
uimenu('Parent',mpreproc,'Label','Filter data...','Callback',...
    @(es,ed) openpreprocdlg(h));
uimenu('Parent',mpreproc,'Label','Detrend data....','Callback',...
    @(es,ed) openpreprocdlg(h));
uimenu('Parent',mtools,'Label','Time shift...','Callback',...
    @(es,ed) openshiftdlg(h));

%% Create drop target
jf = get(h.Figure, 'JavaFrame');
drawnow
h.AxisCanvas = getAxisComponent(jf);
h.DropAdaptor = AxisDropAdaptor(h,'dropCallback',manager.tree.getTree,...
    TreePath([manager.root.getTreeNodeInterface; manager.root.down.getTreeNodeInterface]));
h.AxisCanvas.setDropTarget(DropTarget(h.AxisCanvas,h.DropAdaptor));

%% Node detachment listener
h.addListeners(handle.listener(h,'ObjectParentChanged', ...
    {@localHideView h}));

%% Add node name
if nargin == 3 
  h.Label = 'View Node';
elseif nargin == 4
  h.Label = varargin{1};
elseif nargin >= 5 
  h.Label = h.createDefaultName( varargin{1}, varargin{2} );
else
  error('Node name and an optional parent node handle should be provided')
end

%% Set node properties
set(h,'AllowsChildren',true,'Editable',true,'Icon', ...
    fullfile(matlabroot, 'toolbox', 'matlab', 'timeseries', ...
                          'plot_op_conditions.gif'))

% Build tree node. Note in the CETM there is no need to do this becuase the
% Explorer calls it when building the tree
h.getTreeNodeInterface;

function localResizeFig(eventSrc, eventData)

%% Temporary figure resize callback which calls the children resize until
%% the HG resize geck 206119 is resolved
c = handle(get(eventSrc,'Children'));
for k=1:length(c)
    if ~isempty(c(k).findprop('ResizeFcn'))
        resizefcn = get(c(k),'ResizeFcn');
        if ~isempty(resizefcn) 
            if ischar(resizefcn) 
                eval(resizefcn);
            elseif iscell(resizefcn)
                feval(resizefcn{1},c(k),[],resizefcn{2:end});
            else
                feval(resizefcn,c(k),[]);
            end
        end
    end
end

function localTxtResize(eventSrc,eventData,txt)

%% Resize fcn to ensure that the text is always in the center of the figure

%% Get the figure and find its size in characters
oldunits = get(eventSrc,'Units');
set(eventSrc,'Units','Characters');
pos = get(eventSrc,'Position');
set(eventSrc,'Units',oldunits);

%% Set the text to the middle of the figure
set(txt,'Units','Characters','Position', ...
    [1 pos(4)/2 max(1,pos(3)-2) 1.5])


function localClose(eventSrc, eventData, h)

%% Figure closing listener detaches view node. The node detachement
%% listener then disposes of the view
h.up.removeNode(h);

function localHideView(eventSrc,eventData,h)

%% Node detachement listener. Sets plot and view invisible and destroys the
%% figure
if ~isempty(eventData.NewParent)
    return
end
if ishandle(h.Plot)
    delete(h.Plot)
    %h.Plot.Visible = 'off';
end
if ishandle(h.Dialog)
    set(h.Dialog,'Visible','off')
end
delete(h.Figure)

