Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e83f1049280c6f8f429e07c4dbb196c2873bb28 (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
/*
 * Copyright (c) 2004 - 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.net4j.util.ui.dnd;

import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.dnd.TransferData;

/**
 * @author Eike Stepper
 */
public abstract class DNDDropAdapter<TYPE> extends ViewerDropAdapter
{
  private Transfer[] transfers;

  /**
   * Specifies if dropping between two viewer elements is allowed.
   */
  private boolean dropBetweenEnabled;

  /**
   * @since 3.0
   */
  protected DNDDropAdapter(Transfer[] transfers, StructuredViewer viewer)
  {
    super(viewer);
    this.transfers = transfers;
  }

  /**
   * @since 3.0
   */
  public Transfer[] getTransfers()
  {
    return transfers;
  }

  @Override
  public StructuredViewer getViewer()
  {
    return (StructuredViewer)super.getViewer();
  }

  public boolean isDropBetweenEnabled()
  {
    return dropBetweenEnabled;
  }

  public void setDropBetweenEnabled(boolean dropBetweenEnabled)
  {
    this.dropBetweenEnabled = dropBetweenEnabled;
  }

  @Override
  protected int determineLocation(DropTargetEvent event)
  {
    int location = super.determineLocation(event);
    if (location == LOCATION_BEFORE || location == LOCATION_AFTER)
    {
      if (!dropBetweenEnabled)
      {
        location = LOCATION_ON;
      }
    }

    return location;
  }

  @SuppressWarnings("unchecked")
  @Override
  public boolean performDrop(Object data)
  {
    Object target = getCurrentTarget();
    if (target == null)
    {
      target = getViewer().getInput();
    }

    return performDrop((TYPE)data, target);
  }

  @Override
  public boolean validateDrop(Object target, int operation, TransferData type)
  {
    if (target != null && !validateTarget(target, operation))
    {
      return false;
    }

    for (Transfer transfer : transfers)
    {
      if (transfer.isSupportedType(type))
      {
        return true;
      }
    }

    return false;
  }

  protected abstract boolean validateTarget(Object target, int operation);

  protected abstract boolean performDrop(TYPE data, Object target);
}

Back to the top