function open(h)

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

%% Build the tree
rootNode = tsexplorer.Workspace('Time Series Session');
rootNode.TsViewer = h;
timeseriesNode = tsguis.tsparentnode('Time Series');
rootNode.addNode(timeseriesNode);
viewsNode = tsexplorer.node('Views');
rootNode.addNode(viewsNode);
timeViewsNode = tsguis.viewcontainer('Time Plots','tsguis.tsseriesview');
viewsNode.addNode(timeViewsNode);
specViewsNode = tsguis.viewcontainer('Spectral Plots','tsguis.tsspecnode');
viewsNode.addNode(specViewsNode);
xyViewsNode = tsguis.viewcontainer('XY Plots','tsguis.tsxynode');
viewsNode.addNode(xyViewsNode);
corrViewsNode = tsguis.viewcontainer('Correlations','tsguis.tscorrnode');
viewsNode.addNode(corrViewsNode);
histViewsNode = tsguis.viewcontainer('Histograms','tsguis.tshistnode');
viewsNode.addNode(histViewsNode);
macrosNode = tsexplorer.node('Macros');

%% Create a figure with an inspector panel and tree widget.
thisFigure = figure('NumberTitle', 'off', 'Name', 'Time Series Tools',...
    'Units','Characters','Menubar','none','Position',[0 0 187 70*.8],...
    'HandleVisibility','callback','Visible','off','KeyPressFcn',...
    {@localRedoUndo h}); 
h.TreeManager = tsexplorer.TreeManager(rootNode,'Figure',thisFigure,'DialogPosition',...
    40,'HelpDialogPosition',40);
centerfig(thisFigure);
% add the motion detector to show the resize arrow for mouse
set(thisFigure,'WindowButtonMotionFcn',{@tshoverfig h.TreeManager});

%% Change the base figure icon to a MATLAB icon
drawnow
jf = get(thisFigure, 'JavaFrame');
jframe = javax.swing.SwingUtilities.getWindowAncestor(jf.getAxisComponent);
ic = fullfile(matlabroot,'toolbox/matlab/icons/matlabicon.gif');
jframe.setIcon(javax.swing.ImageIcon(java.lang.String(ic)))

%% Populate the viewer with the top level node handles
set(h,'TSNode', timeseriesNode, 'ViewsNode', viewsNode)

%% Install menus
figmenus(h)
set(thisFigure,'Userdata',h);

%% Set style manager
h.StyleManager = wavepack.WaveStyleManager;

%% Open the Views node
h.TreeManager.Tree.expand(viewsNode.getTreeNodeInterface);

%% Prevent selection of the root node
customTreeSelectionModel  = tsTreeSelectionModel([TreePath([rootNode.getTreeNodeInterface;viewsNode.getTreeNodeInterface]);...
    TreePath(rootNode.getTreeNodeInterface)]);
awtinvoke(h.TreeManager.Tree.getTree,'setSelectionModel(Ljavax/swing/tree/TreeSelectionModel;)',...
    customTreeSelectionModel);

%% Show
h.TreeManager.Visible = 'on';

h.Listeners = [h.Listeners;...
     handle.listener(h,'ObjectBeingDestroyed',{@localClose h})];

%% Select time series parent node
h.TreeManager.Tree.setSelectedNode(h.TreeManager.Root.down.getTreeNodeInterface);


function localClose(es,ed,h)

%% Close all views
viewnodes = h.ViewsNode.find('-isa','tsguis.viewnode');
for k=1:length(viewnodes)
    viewnodes(k).remove(h.TreeManager)
end

%% Hide main figure
h.TreeManager.visible = 'off';

function localRedoUndo(es,eventData,h)

%% KeyPress callback for redo/undo
if strcmp(eventData.Key,'z') && isequal(eventData.Modifier,{'control'})
    h.undo;
elseif strcmp(eventData.Key,'y') && isequal(eventData.Modifier,{'control'})
    h.redo;
end


