Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fc8921c56c72e51366976af42c3bc6154333ad42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.internal.ui.transfer;

import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewRegistry;

import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.io.IORuntimeException;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.part.PluginTransfer;
import org.eclipse.ui.part.PluginTransferData;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author Eike Stepper
 */
public class RepositoryTransferDragListener extends DragSourceAdapter
{
  private static final Transfer[] TRANSFERS = { PluginTransfer.getInstance(), FileTransfer.getInstance() };

  private StructuredViewer viewer;

  protected RepositoryTransferDragListener(StructuredViewer viewer)
  {
    this.viewer = viewer;
  }

  @Override
  public void dragStart(DragSourceEvent event)
  {
    IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
    event.doit = !selection.isEmpty(); // TODO Check that only resource nodes are selected?
  }

  @Override
  public void dragSetData(DragSourceEvent event)
  {
    IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
    for (Transfer transfer : TRANSFERS)
    {
      if (transfer.isSupportedType(event.dataType))
      {
        event.data = getObject(selection, transfer);
        break;
      }
    }
  }

  protected Object getObject(IStructuredSelection selection, Transfer transfer)
  {
    try
    {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      @SuppressWarnings("resource")
      ExtendedDataOutputStream out = new ExtendedDataOutputStream(baos);

      CDOView view = null;
      for (Iterator<?> it = selection.iterator(); it.hasNext();)
      {
        Object object = it.next();
        if (object instanceof CDOResourceNode)
        {
          CDOResourceNode node = (CDOResourceNode)object;
          CDOView nodeView = node.cdoView();
          if (view == null)
          {
            view = nodeView;

            int viewID = CDOViewRegistry.INSTANCE.getID(view);
            out.writeInt(viewID);
          }
          else if (view != nodeView)
          {
            continue;
          }

          out.writeString(node.getPath());
        }
      }

      out.writeString(null);
      byte[] data = baos.toByteArray();
      return new PluginTransferData(RepositoryPluginDropAdapter.DROP_ACTION_ID, data);
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  public static RepositoryTransferDragListener support(StructuredViewer viewer)
  {
    RepositoryTransferDragListener dragListener = new RepositoryTransferDragListener(viewer);
    viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_DEFAULT, TRANSFERS, dragListener);
    return dragListener;
  }
}

Back to the top