Skip to content

DropTargetContext: get the target actions from the peer - #72

Merged
guillerodriguez merged 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-target-actions
Oct 28, 2026
Merged

guillerodriguez merged 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-dtc-target-actions

Conversation

@duncanmcqueen

@duncanmcqueen duncanmcqueen commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Split from #57.

Problem and fix

DropTargetContext.getTargetActions() returns a field that only setTargetActions() sets, and nothing calls setTargetActions() during a drag. Thus the field is 0. DropTargetDragEvent.getDropAction() masks the drop action with getTargetActions(), so it always returns ACTION_NONE. (DropTargetDropEvent.getDropAction() returns the drop action as given, so the problem does not occur there.)

The fix, as in the JDK:

  • Remove the targetActions field and keep the actions in the DropTarget.
  • getTargetActions() asks the peer, or returns dropTarget.getDefaultActions() when there is no peer.
  • setTargetActions() gives the actions to the peer, if there is one, and always calls dropTarget.setDefaultActions(). Thus setTargetActions(ACTION_NONE) works, and a later DropTarget.setDefaultActions() is not ignored.
  • DropTargetDragEvent.getDropAction() returns the drop action as given, as DropTargetDropEvent.getDropAction() does. The peer must limit the drop action to the target actions, as the JDK peers do.

I found this bug when I wrote an AWT toolkit for Haiku, which supplies drag and drop peers.

How to reproduce

The methods are not public, so the test calls them with reflection. It tests the context without a peer, and then with a peer that has the target action ACTION_COPY (StubDropPeer, below). The DropTargetDragEvent check also needs #70: without #70, the constructor rejects the source actions ACTION_COPY_OR_MOVE.

javac StubDropPeer.java DropContextActions.java
java DropContextActions
DropContextActions.java
import java.awt.*;
import java.awt.dnd.*;
import java.lang.reflect.Method;

/**
 * DropTargetContext.getTargetActions() must return the actions of its
 * peer, or the default actions of the drop target when it has no peer.
 * setTargetActions() must give the actions to the peer and to the drop
 * target. DropTargetDragEvent.getDropAction() must return the drop
 * action as given, as in the JDK. The methods are not public, so the
 * test calls them with reflection.
 */
public class DropContextActions
{
  static boolean failed;

  static void check(String what, int got, int expected)
  {
    System.out.println((got == expected ? "PASS " : "FAIL ") + what + " = "
                       + got + ", expected " + expected);
    failed |= got != expected;
  }

  static void addNotify(DropTargetContext dtc, Object peer) throws Exception
  {
    Method m = DropTargetContext.class.getDeclaredMethod("addNotify",
      Class.forName("java.awt.dnd.peer.DropTargetContextPeer"));
    m.setAccessible(true);
    m.invoke(dtc, peer);
  }

  public static void main(String[] args) throws Exception
  {
    DropTarget dt = new DropTarget(new Canvas(), DnDConstants.ACTION_COPY_OR_MOVE,
                                   new DropTargetAdapter()
      {
        public void drop(DropTargetDropEvent e) {}
      });
    DropTargetContext dtc = dt.getDropTargetContext();
    Method get = DropTargetContext.class.getDeclaredMethod("getTargetActions");
    Method set = DropTargetContext.class.getDeclaredMethod("setTargetActions",
                                                           int.class);
    get.setAccessible(true);
    set.setAccessible(true);

    check("getTargetActions() without a peer", (Integer) get.invoke(dtc),
          DnDConstants.ACTION_COPY_OR_MOVE);
    set.invoke(dtc, DnDConstants.ACTION_NONE);
    check("getTargetActions() after setTargetActions(ACTION_NONE)",
          (Integer) get.invoke(dtc), DnDConstants.ACTION_NONE);
    set.invoke(dtc, DnDConstants.ACTION_COPY);
    dt.setDefaultActions(DnDConstants.ACTION_LINK);
    check("getTargetActions() after setTargetActions(ACTION_COPY)"
          + " and setDefaultActions(ACTION_LINK)",
          (Integer) get.invoke(dtc), DnDConstants.ACTION_LINK);

    set.invoke(dtc, DnDConstants.ACTION_COPY);
    DropTargetDragEvent e =
      new DropTargetDragEvent(dtc, new Point(0, 0), DnDConstants.ACTION_MOVE,
                              DnDConstants.ACTION_COPY_OR_MOVE);
    check("DropTargetDragEvent.getDropAction()", e.getDropAction(),
          DnDConstants.ACTION_MOVE);

    StubDropPeer peer = new StubDropPeer(dt, DnDConstants.ACTION_COPY);
    addNotify(dtc, peer);
    check("getTargetActions() with a peer", (Integer) get.invoke(dtc),
          DnDConstants.ACTION_COPY);
    set.invoke(dtc, DnDConstants.ACTION_MOVE);
    check("peer actions after setTargetActions(ACTION_MOVE)",
          peer.getTargetActions(), DnDConstants.ACTION_MOVE);
    check("default actions after setTargetActions(ACTION_MOVE)",
          dt.getDefaultActions(), DnDConstants.ACTION_MOVE);
    System.exit(failed ? 1 : 0);
  }
}
StubDropPeer.java
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.dnd.peer.DropTargetContextPeer;

/** A DropTargetContextPeer that offers text and the given target actions. */
public class StubDropPeer implements DropTargetContextPeer
{
  int actions;
  DropTarget target;

  StubDropPeer(DropTarget target, int actions)
  {
    this.target = target;
    this.actions = actions;
  }

  public void setTargetActions(int a) { actions = a; }
  public int getTargetActions() { return actions; }
  public DropTarget getDropTarget() { return target; }
  public DataFlavor[] getTransferDataFlavors()
  {
    return new DataFlavor[] { DataFlavor.stringFlavor };
  }
  public Transferable getTransferable() { return new StringSelection("text"); }
  public boolean isTransferableJVMLocal() { return true; }
  public void acceptDrag(int a) {}
  public void rejectDrag() {}
  public void acceptDrop(int a) {}
  public void rejectDrop() {}
  public void dropComplete(boolean ok) {}
}

Results

Haiku R1/beta6 x86_64 (hrev60228), JamVM with the Haiku port (#25 in ingelabs/jamvm), and ingelabs/classpath built on Haiku. I compiled each version of the changed classes with javac from OpenJDK 8 and put them first on the boot class path (-Xbootclasspath/p). All three versions also have the constructor fix of #70. "Before" is master. "Previous" is the first version of this pull request. "After" is this version.

Before:
FAIL getTargetActions() without a peer = 0, expected 3
PASS getTargetActions() after setTargetActions(ACTION_NONE) = 0, expected 0
FAIL getTargetActions() after setTargetActions(ACTION_COPY) and setDefaultActions(ACTION_LINK) = 1, expected 1073741824
FAIL DropTargetDragEvent.getDropAction() = 0, expected 2
PASS getTargetActions() with a peer = 1, expected 1
FAIL peer actions after setTargetActions(ACTION_MOVE) = 1, expected 2
FAIL default actions after setTargetActions(ACTION_MOVE) = 1073741824, expected 2

Previous:
PASS getTargetActions() without a peer = 3, expected 3
FAIL getTargetActions() after setTargetActions(ACTION_NONE) = 3, expected 0
FAIL getTargetActions() after setTargetActions(ACTION_COPY) and setDefaultActions(ACTION_LINK) = 1, expected 1073741824
FAIL DropTargetDragEvent.getDropAction() = 0, expected 2
PASS getTargetActions() with a peer = 1, expected 1
PASS peer actions after setTargetActions(ACTION_MOVE) = 2, expected 2
FAIL default actions after setTargetActions(ACTION_MOVE) = 1073741824, expected 2

After:
PASS getTargetActions() without a peer = 3, expected 3
PASS getTargetActions() after setTargetActions(ACTION_NONE) = 0, expected 0
PASS getTargetActions() after setTargetActions(ACTION_COPY) and setDefaultActions(ACTION_LINK) = 1073741824, expected 1073741824
PASS DropTargetDragEvent.getDropAction() = 2, expected 2
PASS getTargetActions() with a peer = 1, expected 1
PASS peer actions after setTargetActions(ACTION_MOVE) = 2, expected 2
PASS default actions after setTargetActions(ACTION_MOVE) = 2, expected 2

OpenJDK 27 on Linux gives the "After" result for the four checks without a peer. (Its addNotify() takes an internal peer type, so the checks with a peer cannot run there. JDK 8 gave the "After" result for them with the first version of the test.)

Platforms

Not specific to Haiku. The problem is in java.awt.dnd and occurs with all toolkits. I reproduced it on Linux.

AI assistance

An AI model (Claude Code, by Anthropic) wrote this change, the test and this text. The repository owner directed the work and checked the results. Please review it as you review other contributions.

🤖 Generated with Claude Code

@duncanmcqueen
duncanmcqueen marked this pull request as ready for review October 14, 2026 23:52
@duncanmcqueen
duncanmcqueen force-pushed the fix-dtc-target-actions branch from 7748bbf to 8e33200 Compare October 15, 2026 13:51
@guillerodriguez

Copy link
Copy Markdown
Contributor

Thanks, this looks good.

One issue: the targetActions field with the != 0 fallback has two edge cases when there is no peer:

  • setTargetActions(ACTION_NONE) is ignored: getTargetActions() then returns the drop target’s default actions instead of 0.
  • After any non-zero setTargetActions(), later calls to DropTarget.setDefaultActions() are ignored by getTargetActions().

I suggest removing the targetActions field and keeping the actions in the DropTarget. setTargetActions() would pass the actions to the peer, if there is one, and always call dropTarget.setDefaultActions(). getTargetActions() would ask the peer, or return dropTarget.getDefaultActions() when there is no peer.

What do you think?

@guillerodriguez

Copy link
Copy Markdown
Contributor

One more thing I noticed: DropTargetDragEvent.getDropAction() masks the drop action with getTargetActions(), but DropTargetDropEvent.getDropAction() returns the drop action as given (so it was not affected by this problem, even though the PR description says otherwise). The JDK doesn’t mask in either event: both return the drop action they were constructed with, and the peer is expected to account for the target actions itself.

Removing the masking from DropTargetDragEvent.getDropAction() would also fix the ACTION_NONE problem from the description. (The getTargetActions() fix is still worth doing either way.)

getTargetActions() returned a field that only setTargetActions() set,
and nothing calls setTargetActions() during a drag. Thus the field was
0, and DropTargetDragEvent.getDropAction(), which masks the drop
action with the target actions, always returned ACTION_NONE.

As in the JDK, remove the field and keep the actions in the
DropTarget. getTargetActions() asks the peer, or returns the default
actions of the drop target when there is no peer. setTargetActions()
gives the actions to the peer, if there is one, and to the drop
target.

Also, as in the JDK, DropTargetDragEvent.getDropAction() now returns
the drop action as given. The peer must limit the drop action to the
target actions. DropTargetDropEvent.getDropAction() already did this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
@duncanmcqueen
duncanmcqueen force-pushed the fix-dtc-target-actions branch from 8e33200 to 67e1ed4 Compare October 22, 2026 13:25
@duncanmcqueen

Copy link
Copy Markdown
Contributor Author

Thank you. You are correct on all points. I changed the commit as you suggest:

  • The targetActions field is removed. The actions are kept in the DropTarget.
  • setTargetActions() gives the actions to the peer, if there is one, and always calls dropTarget.setDefaultActions().
  • getTargetActions() asks the peer, or returns dropTarget.getDefaultActions().
  • DropTargetDragEvent.getDropAction() does not mask the drop action now, as in the JDK.

You are also correct about DropTargetDropEvent.getDropAction(). My description was wrong. I corrected it.

I added your two edge cases to the test (setTargetActions(ACTION_NONE), and setDefaultActions() after setTargetActions()), and also checks for DropTargetDragEvent.getDropAction() and for setTargetActions() with a peer. On Haiku with JamVM, all checks pass with this version. The first version failed your two edge cases. OpenJDK 27 gives the same results for the checks without a peer. The DropTargetDragEvent check also needs #70, because the constructor rejects ACTION_COPY_OR_MOVE without #70.

Because the event does not mask the drop action now, the Haiku peer in #61 must limit the drop action to the target actions. I changed the peer to do this, as the JDK peers do.

@guillerodriguez
guillerodriguez merged commit e40d5ec into ingelabs:master Oct 28, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants