function dropCallback(h,x,y)

% tsnode -> viewnode
% tsnode -> viewcontainer
% viewnode -> viewcontainer
% viewnode -> viewnode


%% Find currently selected node 
n = h.Tree.getSelectedNodes;
if isempty(n)
   return
end
droppednode = handle(n(1).getUserObject);

%% Get the target node and make sure that it fits one of the above cases
targetviewnode = handle(h.Tree.getTree.getPathForLocation(x,y).getLastPathComponent.getUserObject);
strvec = 'ft';

switch strvec([isa(droppednode,'tsguis.viewnode'),isa(droppednode,'tsguis.tsnode'),...
        isa(targetviewnode,'tsguis.viewnode'),isa(targetviewnode,'tsguis.viewcontainer')]+1)
    case 'fttf' % Case 1
         ts = {droppednode.Timeseries};   
         newview = targetviewnode;
    case 'ftft' % Case 2
         ts = {droppednode.Timeseries};
         newview = targetviewnode.addplot(h);
    case 'tftf' % Case 4
         targetviewnode = targetviewnode.up;
         % If the selected and drop nodes are views of the same type then there is
         % nothing to do
         if targetviewnode==droppednode.up
            return
         end
         ts = droppednode.Plot.getTimeSeries;
         newview = targetviewnode.addplot(h);
    case 'tfft' % Case 3
         % If the selected and drop nodes are views of the same type then there is
         % nothing to do
         if targetviewnode==droppednode.up
            return
         end
         ts = droppednode.Plot.getTimeSeries;
         if length(ts)>0
             newview = targetviewnode.addplot(h);
         end
end  



%% Add the time series from the dropped view to the taarget view
for k=1:length(ts)
    % xynode and corrnode doesn't take more than 2 ts objects, therefore,
    % only the first 2 ts objects are used in both cases
    if k>2 && (isa(newview,'tsguis.tscorrnode') || isa(newview,'tsguis.tsxynode'))
        continue
    end
    newview.addTs(ts{k});
end
h.Tree.setSelectedNode(targetviewnode.getTreeNodeInterface)    

