Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11/XDropTargetContextPeer.java
41159 views
/*1* Copyright (c) 2003, 2015, 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.awt.AWTAccessor;35import sun.util.logging.PlatformLogger;3637import sun.awt.AppContext;38import sun.awt.SunToolkit;3940import sun.awt.dnd.SunDropTargetContextPeer;41import sun.awt.dnd.SunDropTargetEvent;4243import jdk.internal.misc.Unsafe;4445/**46* The XDropTargetContextPeer is the class responsible for handling47* the interaction between the XDnD/Motif DnD subsystem and Java drop targets.48*49* @since 1.550*/51final class XDropTargetContextPeer extends SunDropTargetContextPeer {52private static final PlatformLogger logger =53PlatformLogger.getLogger("sun.awt.X11.xembed.xdnd.XDropTargetContextPeer");5455private static final Unsafe unsafe = XlibWrapper.unsafe;5657/*58* A key to store a peer instance for an AppContext.59*/60private static final Object DTCP_KEY = "DropTargetContextPeer";6162private XDropTargetContextPeer() {}6364static XDropTargetContextPeer getPeer(AppContext appContext) {65synchronized (_globalLock) {66XDropTargetContextPeer peer =67(XDropTargetContextPeer)appContext.get(DTCP_KEY);68if (peer == null) {69peer = new XDropTargetContextPeer();70appContext.put(DTCP_KEY, peer);71}7273return peer;74}75}7677static XDropTargetProtocolListener getXDropTargetProtocolListener() {78return XDropTargetProtocolListenerImpl.getInstance();79}8081/*82* @param returnValue the drop action selected by the Java drop target.83*/84protected void eventProcessed(SunDropTargetEvent e, int returnValue,85boolean dispatcherDone) {86/* The native context is the pointer to the XClientMessageEvent87structure. */88long ctxt = getNativeDragContext();89/* If the event was not consumed, send a response to the source. */90try {91if (ctxt != 0 && !e.isConsumed()) {92Iterator<XDropTargetProtocol> dropTargetProtocols =93XDragAndDropProtocols.getDropTargetProtocols();9495while (dropTargetProtocols.hasNext()) {96XDropTargetProtocol dropTargetProtocol =97dropTargetProtocols.next();98if (dropTargetProtocol.sendResponse(ctxt, e.getID(),99returnValue)) {100break;101}102}103}104} finally {105if (dispatcherDone && ctxt != 0) {106unsafe.freeMemory(ctxt);107}108}109}110111protected void doDropDone(boolean success, int dropAction,112boolean isLocal) {113/* The native context is the pointer to the XClientMessageEvent114structure. */115long ctxt = getNativeDragContext();116117if (ctxt != 0) {118try {119Iterator<XDropTargetProtocol> dropTargetProtocols =120XDragAndDropProtocols.getDropTargetProtocols();121122while (dropTargetProtocols.hasNext()) {123XDropTargetProtocol dropTargetProtocol =124dropTargetProtocols.next();125if (dropTargetProtocol.sendDropDone(ctxt, success,126dropAction)) {127break;128}129}130} finally {131unsafe.freeMemory(ctxt);132}133}134}135136protected Object getNativeData(long format)137throws IOException {138/* The native context is the pointer to the XClientMessageEvent139structure. */140long ctxt = getNativeDragContext();141142if (ctxt != 0) {143Iterator<XDropTargetProtocol> dropTargetProtocols =144XDragAndDropProtocols.getDropTargetProtocols();145146while (dropTargetProtocols.hasNext()) {147XDropTargetProtocol dropTargetProtocol =148dropTargetProtocols.next();149// getData throws IAE if ctxt is not for this protocol.150try {151return dropTargetProtocol.getData(ctxt, format);152} catch (IllegalArgumentException iae) {153}154}155}156157return null;158}159160private void cleanup() {161}162163protected void processEnterMessage(SunDropTargetEvent event) {164if (!processSunDropTargetEvent(event)) {165super.processEnterMessage(event);166}167}168169protected void processExitMessage(SunDropTargetEvent event) {170if (!processSunDropTargetEvent(event)) {171super.processExitMessage(event);172}173}174175protected void processMotionMessage(SunDropTargetEvent event,176boolean operationChanged) {177if (!processSunDropTargetEvent(event)) {178super.processMotionMessage(event, operationChanged);179}180}181182protected void processDropMessage(SunDropTargetEvent event) {183if (!processSunDropTargetEvent(event)) {184super.processDropMessage(event);185}186}187188// If source is an XEmbedCanvasPeer, passes the event to it for processing and189// return true if the event is forwarded to the XEmbed child.190// Otherwise, does nothing and return false.191private boolean processSunDropTargetEvent(SunDropTargetEvent event) {192Object source = event.getSource();193194if (source instanceof Component) {195Object peer = AWTAccessor.getComponentAccessor()196.getPeer((Component) source);197if (peer instanceof XEmbedCanvasPeer) {198XEmbedCanvasPeer xEmbedCanvasPeer = (XEmbedCanvasPeer)peer;199/* The native context is the pointer to the XClientMessageEvent200structure. */201long ctxt = getNativeDragContext();202203if (logger.isLoggable(PlatformLogger.Level.FINER)) {204logger.finer(" processing " + event + " ctxt=" + ctxt +205" consumed=" + event.isConsumed());206}207/* If the event is not consumed, pass it to the208XEmbedCanvasPeer for processing. */209if (!event.isConsumed()) {210// NOTE: ctxt can be zero at this point.211if (xEmbedCanvasPeer.processXEmbedDnDEvent(ctxt,212event.getID())) {213event.consume();214return true;215}216}217}218}219220return false;221}222223public void forwardEventToEmbedded(long embedded, long ctxt,224int eventID) {225Iterator<XDropTargetProtocol> dropTargetProtocols =226XDragAndDropProtocols.getDropTargetProtocols();227228while (dropTargetProtocols.hasNext()) {229XDropTargetProtocol dropTargetProtocol = dropTargetProtocols.next();230if (dropTargetProtocol.forwardEventToEmbedded(embedded, ctxt,231eventID)) {232break;233}234}235}236237static final class XDropTargetProtocolListenerImpl238implements XDropTargetProtocolListener {239240private static final XDropTargetProtocolListener theInstance =241new XDropTargetProtocolListenerImpl();242243private XDropTargetProtocolListenerImpl() {}244245static XDropTargetProtocolListener getInstance() {246return theInstance;247}248249public void handleDropTargetNotification(XWindow xwindow, int x, int y,250int dropAction, int actions,251long[] formats, long nativeCtxt,252int eventID) {253Object target = xwindow.getTarget();254255// The Every component is associated with some AppContext.256assert target instanceof Component;257258Component component = (Component)target;259260AppContext appContext = SunToolkit.targetToAppContext(target);261262// Every component is associated with some AppContext.263assert appContext != null;264265XDropTargetContextPeer peer = XDropTargetContextPeer.getPeer(appContext);266267peer.postDropTargetEvent(component, x, y, dropAction, actions, formats,268nativeCtxt, eventID,269!SunDropTargetContextPeer.DISPATCH_SYNC);270}271}272}273274275