Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/awt/X11/XDropTargetContextPeer.java
32288 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.awt.X11;2627import java.awt.Component;28import java.awt.peer.ComponentPeer;2930import java.io.IOException;3132import java.util.Iterator;3334import sun.util.logging.PlatformLogger;3536import sun.awt.AppContext;37import sun.awt.SunToolkit;3839import sun.awt.dnd.SunDropTargetContextPeer;40import sun.awt.dnd.SunDropTargetEvent;4142import sun.misc.Unsafe;4344/**45* The XDropTargetContextPeer is the class responsible for handling46* the interaction between the XDnD/Motif DnD subsystem and Java drop targets.47*48* @since 1.549*/50final class XDropTargetContextPeer extends SunDropTargetContextPeer {51private static final PlatformLogger logger =52PlatformLogger.getLogger("sun.awt.X11.xembed.xdnd.XDropTargetContextPeer");5354private static final Unsafe unsafe = XlibWrapper.unsafe;5556/*57* A key to store a peer instance for an AppContext.58*/59private static final Object DTCP_KEY = "DropTargetContextPeer";6061private XDropTargetContextPeer() {}6263static XDropTargetContextPeer getPeer(AppContext appContext) {64synchronized (_globalLock) {65XDropTargetContextPeer peer =66(XDropTargetContextPeer)appContext.get(DTCP_KEY);67if (peer == null) {68peer = new XDropTargetContextPeer();69appContext.put(DTCP_KEY, peer);70}7172return peer;73}74}7576static XDropTargetProtocolListener getXDropTargetProtocolListener() {77return XDropTargetProtocolListenerImpl.getInstance();78}7980/*81* @param returnValue the drop action selected by the Java drop target.82*/83protected void eventProcessed(SunDropTargetEvent e, int returnValue,84boolean dispatcherDone) {85/* The native context is the pointer to the XClientMessageEvent86structure. */87long ctxt = getNativeDragContext();88/* If the event was not consumed, send a response to the source. */89try {90if (ctxt != 0 && !e.isConsumed()) {91Iterator dropTargetProtocols =92XDragAndDropProtocols.getDropTargetProtocols();9394while (dropTargetProtocols.hasNext()) {95XDropTargetProtocol dropTargetProtocol =96(XDropTargetProtocol)dropTargetProtocols.next();97if (dropTargetProtocol.sendResponse(ctxt, e.getID(),98returnValue)) {99break;100}101}102}103} finally {104if (dispatcherDone && ctxt != 0) {105unsafe.freeMemory(ctxt);106}107}108}109110protected void doDropDone(boolean success, int dropAction,111boolean isLocal) {112/* The native context is the pointer to the XClientMessageEvent113structure. */114long ctxt = getNativeDragContext();115116if (ctxt != 0) {117try {118Iterator dropTargetProtocols =119XDragAndDropProtocols.getDropTargetProtocols();120121while (dropTargetProtocols.hasNext()) {122XDropTargetProtocol dropTargetProtocol =123(XDropTargetProtocol)dropTargetProtocols.next();124if (dropTargetProtocol.sendDropDone(ctxt, success,125dropAction)) {126break;127}128}129} finally {130unsafe.freeMemory(ctxt);131}132}133}134135protected Object getNativeData(long format)136throws IOException {137/* The native context is the pointer to the XClientMessageEvent138structure. */139long ctxt = getNativeDragContext();140141if (ctxt != 0) {142Iterator dropTargetProtocols =143XDragAndDropProtocols.getDropTargetProtocols();144145while (dropTargetProtocols.hasNext()) {146XDropTargetProtocol dropTargetProtocol =147(XDropTargetProtocol)dropTargetProtocols.next();148// getData throws IAE if ctxt is not for this protocol.149try {150return dropTargetProtocol.getData(ctxt, format);151} catch (IllegalArgumentException iae) {152}153}154}155156return null;157}158159private void cleanup() {160}161162protected void processEnterMessage(SunDropTargetEvent event) {163if (!processSunDropTargetEvent(event)) {164super.processEnterMessage(event);165}166}167168protected void processExitMessage(SunDropTargetEvent event) {169if (!processSunDropTargetEvent(event)) {170super.processExitMessage(event);171}172}173174protected void processMotionMessage(SunDropTargetEvent event,175boolean operationChanged) {176if (!processSunDropTargetEvent(event)) {177super.processMotionMessage(event, operationChanged);178}179}180181protected void processDropMessage(SunDropTargetEvent event) {182if (!processSunDropTargetEvent(event)) {183super.processDropMessage(event);184}185}186187// If source is an XEmbedCanvasPeer, passes the event to it for processing and188// return true if the event is forwarded to the XEmbed child.189// Otherwise, does nothing and return false.190private boolean processSunDropTargetEvent(SunDropTargetEvent event) {191Object source = event.getSource();192193if (source instanceof Component) {194ComponentPeer peer = ((Component)source).getPeer();195if (peer instanceof XEmbedCanvasPeer) {196XEmbedCanvasPeer xEmbedCanvasPeer = (XEmbedCanvasPeer)peer;197/* The native context is the pointer to the XClientMessageEvent198structure. */199long ctxt = getNativeDragContext();200201if (logger.isLoggable(PlatformLogger.Level.FINER)) {202logger.finer(" processing " + event + " ctxt=" + ctxt +203" consumed=" + event.isConsumed());204}205/* If the event is not consumed, pass it to the206XEmbedCanvasPeer for processing. */207if (!event.isConsumed()) {208// NOTE: ctxt can be zero at this point.209if (xEmbedCanvasPeer.processXEmbedDnDEvent(ctxt,210event.getID())) {211event.consume();212return true;213}214}215}216}217218return false;219}220221public void forwardEventToEmbedded(long embedded, long ctxt,222int eventID) {223Iterator dropTargetProtocols =224XDragAndDropProtocols.getDropTargetProtocols();225226while (dropTargetProtocols.hasNext()) {227XDropTargetProtocol dropTargetProtocol =228(XDropTargetProtocol)dropTargetProtocols.next();229if (dropTargetProtocol.forwardEventToEmbedded(embedded, ctxt,230eventID)) {231break;232}233}234}235236static final class XDropTargetProtocolListenerImpl237implements XDropTargetProtocolListener {238239private final static XDropTargetProtocolListener theInstance =240new XDropTargetProtocolListenerImpl();241242private XDropTargetProtocolListenerImpl() {}243244static XDropTargetProtocolListener getInstance() {245return theInstance;246}247248public void handleDropTargetNotification(XWindow xwindow, int x, int y,249int dropAction, int actions,250long[] formats, long nativeCtxt,251int eventID) {252Object target = xwindow.getTarget();253254// The Every component is associated with some AppContext.255assert target instanceof Component;256257Component component = (Component)target;258259AppContext appContext = SunToolkit.targetToAppContext(target);260261// Every component is associated with some AppContext.262assert appContext != null;263264XDropTargetContextPeer peer = XDropTargetContextPeer.getPeer(appContext);265266peer.postDropTargetEvent(component, x, y, dropAction, actions, formats,267nativeCtxt, eventID,268!SunDropTargetContextPeer.DISPATCH_SYNC);269}270}271}272273274