function addCallbacks( this )
% ADDCALLBACKS Add Java related callbacks

% Add event listeners
set(this.Figure,'CloseRequestFcn',{@localClose this})
set(this.Tree,'NodeSelectedCallback',{ @LocalSelectionChanged, this});
set(handle(this.Tree.getTree,'callbackproperties'),'KeypressedCallback',...
    {@localKeyPressed this})

% PopupTriggered event is fired from java by the uitreePopupListener class

% ---------------------------------------------------------------------------- %
% function LocalWindowClosing(hSrc, hData, this)
% % Set the workspace node to be the one selected
% this.ExplorerPanel.setSelected( this.Root.getTreeNodeInterface );
% this.Explorer.setVisible(false);
% 
% % Get the children of the workspace
% children = this.Root.getChildren;
% for k = length(children):-1:1
%   children(k).delete;
% end
% 
% % Clear the status area
% this.Explorer.clearText;

% ---------------------------------------------------------------------------- %
% function LocalPopupTriggered(hSrc, hData, this)
% h = handle( hData.getNode.getObject );
% e = hData.getEvent;
% 
% popup = h.getPopupInterface( this );
% if ~isempty(popup) 
%   popup.show( hData.getSource, e.getX, e.getY );
%   popup.repaint;
% end

% ---------------------------------------------------------------------------- %
function LocalSelectionChanged(hSrc, hData, this)

% h = handle( hData.getNode.getObject );
% ExplorerPanel = this.ExplorerPanel;
% Explorer      = this.Explorer;

% Set explorer components
% if ~isempty( Explorer )
%   [menubar, toolbar] = getMenuToolBarInterface( h, this );
%   Explorer.setExplorerComponents(menubar, toolbar, h.Status, h.getGUIResources);
% end

h = get(get(hData,'CurrentNode'),'Value');
%figure(double(this.Figure))
if ~isempty(h)
   % Show panel
   set(this.Panel,'Visible','off');
   set(this.HelpPanel,'Visible','off');
   [this.Panel,this.HelpPanel] = getDialogInterface(handle(h), this);
   set(this.Panel,'Visible','on');
   set(this.HelpPanel,'Visible','on');
   
   % Update menu status
   setMenusEnabled(handle(h),this);
   
   % Margin bars must remain on top for the hit test to fire properly
   figchildren = get(this.Figure,'children');
   [junk,I] = ismember(this.Margin,figchildren);
   figchildren(I) = [];
   figchildren = [this.Margin(:);figchildren];
   set(this.Figure,'children',figchildren);
end 

function localClose(es,ed,this)

h = get(es,'Userdata');
if ~isempty(h) && ishandle(h)
    delete(h)
else
    closereq;
end

function localKeyPressed(es,ed,this)

%% Get selected node
selnodes = this.Tree.getSelectedNodes;
if length(selnodes)>0
    thisnode = handle(selnodes(1).getValue);
end

%% Node deletion
if ed.getKeyCode == ed.VK_DELETE
    remove(thisnode,this);
end

%% CTRL-C
if ed.isControlDown && ed.getKeyCode == ed.VK_C
    copynode(thisnode,this);
end

%% CTRL_V
if ed.isControlDown && ed.getKeyCode == ed.VK_V
    pastenode(thisnode,this);
end
