function initialize(h,manager,varargin)

import javax.swing.*

%% Builds the arithmatic dialog

%% Get list of time series
tslist = get(h.SrcNode.getChildren,{'Label'});
macrolist = {};

%% Main figure
h.Figure = figure('Units','Characters','Position',[103.8 26.3 72.2 35.154],'Toolbar',...
    'None','Numbertitle','off','Menubar','None','Name','Time Series Operation',...
    'Visible','on','closeRequestFcn',@(es,ed) set(h,'Visible','off'),...
    'HandleVisibility','callback');

%% Explanation label
LBLexplain = uicontrol('Style','Text','Parent',h.Figure,'Units','Characters',...
    'Position',[0.8 32.923 76.2 1.308],'String',...
    'Select time series and any arithmetic operations below',...
    'HorizontalAlignment','Left');

%% Time series panel
TSPanel = uipanel('Parent',h.Figure,'Units','Characters','Position',...
    [0.8 25 69.2 7.308],'Title','Click on Time Series to Add to Expression');
h.Handles.tsList = uicontrol('Style','Listbox','Parent',TSPanel,'Units','Characters',...
    'Position',[2 1.308 64 4.154],'String',tslist,...
    'HorizontalAlignment','Left','Backgroundcolor',[1 1 1]);

%% Operations panel
OpsPanel = uipanel('Parent',h.Figure,'Units','Characters','Position',...
    [0.8 13.077 69.2 11.231],'Title','Click to Add Pre-defined Operations');
LBLarith = uicontrol('Style','Text','Parent',OpsPanel,'Units','Characters',...
    'Position',[2.4 8 22 1.154],'String',...
    'Arithmetic:','HorizontalAlignment','Left');
BTNplus = uicontrol('Style','pushbutton','Parent',OpsPanel,'Units','Characters',...
    'Position',[14.4 7.615 5.2 1.769],'String','+',...
    'HorizontalAlignment','Center','Callback', ...
    @(es,ed) localArithOp(h,'+'));
BTNminus = uicontrol('Style','pushbutton','Parent',OpsPanel,'Units','Characters',...
    'Position',[21.4 7.615 5.2 1.769],'String','-',...
    'HorizontalAlignment','Center','Callback', ...
    @(es,ed) localArithOp(h,'-'));
BTNtimes = uicontrol('Style','pushbutton','Parent',OpsPanel,'Units','Characters',...
    'Position',[28.4 7.615 5.2 1.769],'String','*',...
    'HorizontalAlignment','Center','Callback', ...
    @(es,ed) localArithOp(h,'.*'));
BTNdivide = uicontrol('Style','pushbutton','Parent',OpsPanel,'Units','Characters',...
    'Position',[35.4 7.615 5.2 1.769],'String','/',...
    'HorizontalAlignment','Center','Callback', ...
    @(es,ed) localArithOp(h,'./'));

%% Expression components
LBLexpression = uicontrol('Style','Text','Parent',h.Figure,'Units','Characters',...
    'Position',[3.2 11 15.2 1.154],'String',...
    'Expression:','HorizontalAlignment','Left');
h.Handles.TXTexp = uicontrol('Style','Edit','Parent',h.Figure,'Units','Characters',...
    'Position',[3.2 8.846 63.4 1.615],'HorizontalAlignment','Left',...
    'Backgroundcolor',[1 1 1],'TooltipString','Enter MATLAB expression');

%% Output components
LBLoutname = uicontrol('Style','Text','Parent',h.Figure,'Units','Characters',...
    'Position',[3.2 6.385 40 1.154],'String',...
    'Name of output time series (if any):','HorizontalAlignment','Left');
h.Handles.TXToutname = uicontrol('Style','Edit','Parent',h.Figure,'Units','Characters',...
    'Position',[3.2 3.923 63.4 1.615],'HorizontalAlignment','Left',...
    'Backgroundcolor',[1 1 1]);

%% Callbacks
set(h.Handles.tsList,'callback',{@localPopList h});

%% Buttons
BTNok = uicontrol('Style','pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[21.2 0.846 13.8 1.769],'String','OK',...
    'HorizontalAlignment','Center','Callback',{@localOK h});
BTNapply = uicontrol('Style','pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[37.2 0.846 13.8 1.769],'String','Apply',...
    'HorizontalAlignment','Center','Callback',@(es,ed) eval(h));
BTNcancel = uicontrol('Style','pushbutton','Parent',h.Figure,'Units','Characters',...
    'Position',[37.2+16 0.846 13.8 1.769],'String','Cancel',...
    'HorizontalAlignment','Center','Callback',@(es,ed) set(h,'Visible','off'));

%% Set the figure background to match the labels
set(h.Figure,'color',get(LBLexplain,'backgroundcolor'))

%% Install general listeners
h.generic_listeners;
manager.Listeners = [manager.Listeners; handle.listener(manager,manager.findprop('Visible'),...
    'PropertyPostSet',{@localHide h manager})];

%% Cache the java handle to the expression edit box
jf = get(h.Figure,'javaframe');
drawnow
k = 0;
cc = jf.getAxisComponent.getParent.getParent.getComponent(0).getComponent(k);
while ~isa(cc, 'com.mathworks.hg.peer.FigureComponentContainer') && ...
        k<jf.getAxisComponent.getParent.getParent.getComponent(0).getComponentCount
    k = k+1;
    cc = jf.getAxisComponent.getParent.getParent.getComponent(0).getComponent(k);
end   
if ~isempty(cc)
    numcomps = cc.getComponentCount;
    for k=1:numcomps
         thiscomp = cc.getComponent(k).getComponent(0);
         if ~isempty(strfind(char(thiscomp.getToolTipText),'Enter MATLAB expression'))
             h.Handles.jExpression = thiscomp;
             break
         end
    end
end

function localOK(eventSrc,eventData,h)

%% OK button callback 
ok = h.eval;

%% Close on success
if ok
   h.Visible = 'off';
end

function localPopList(es,ed,h)

%% Add list item to current expression

%% Get selected list item
listPos = get(h.Handles.tsList,'value');
listTxt = get(h.Handles.tsList,'string');
listItem = listTxt{listPos};

%% Add text to expression box
localAddTxt(h,listItem)

function localArithOp(h,op)

%% Add text to expression box
localAddTxt(h,op)

function localHide(es,ed,h,manager)

%% Listener to tree manager hide will hide the stats dlg
if strcmp(manager.Visible,'off')
    set(h,'Visible','off')
end

function localAddTxt(h,addedItem)

%% Get caratpos
if ~isempty(h.Handles.jExpression)
    caratpos = h.Handles.jExpression.getCaretPosition;
else
    caratpos = length(get(h.Handles.TXTexp,'string'));
end

%% Add item to expression
if ~isempty(addedItem)
   exprText = get(h.Handles.TXTexp,'string');
   if strcmp(addedItem,'default')
       addedItem = [addedItem ' '];
   end
   set(h.Handles.TXTexp,'string',...
       sprintf('%s%s%s',exprText(1:caratpos),addedItem,exprText(caratpos+1:end)))
end

%% Reset caratpos
drawnow
if ~isempty(h.Handles.jExpression)
    h.Handles.jExpression.setCaretPosition(caratpos+length(addedItem));
end

%% Set the focus to the Expression box
uicontrol(h.Handles.TXTexp)