Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CDropTargetContextPeer.m
41152 views
/*1* Copyright (c) 2011, 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*/2425#import "sun_lwawt_macosx_CDropTargetContextPeer.h"2627#import "CDataTransferer.h"28#import "CDropTarget.h"29#import "DnDUtilities.h"30#import "ThreadUtilities.h"31#import "JNIUtilities.h"3233jclass jc_CDropTargetContextPeer = NULL;34#define GET_DTCP_CLASS() \35GET_CLASS(jc_CDropTargetContextPeer, "sun/lwawt/macosx/CDropTargetContextPeer");3637#define GET_DTCP_CLASS_RETURN(ret) \38GET_CLASS_RETURN(jc_CDropTargetContextPeer, "sun/lwawt/macosx/CDropTargetContextPeer", ret);394041static void TransferFailed(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat) {42AWT_ASSERT_NOT_APPKIT_THREAD;43GET_DTCP_CLASS();44DECLARE_METHOD(transferFailedMethod, jc_CDropTargetContextPeer, "transferFailed", "(J)V");45(*env)->CallVoidMethod(env, jthis, transferFailedMethod, jformat); // AWT_THREADING Safe (!appKit)46CHECK_EXCEPTION();47}4849static CDropTarget* GetCDropTarget(jlong jdroptarget) {50CDropTarget* dropTarget = (CDropTarget*) jlong_to_ptr(jdroptarget);5152// Make sure the drop target is of the right kind:53if ([dropTarget isKindOfClass:[CDropTarget class]]) {54return dropTarget;55}5657return nil;58}596061/*62* Class: sun_lwawt_macosx_CDropTargetContextPeer63* Method: startTransfer64* Signature: (JJ)J65*/66JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer67(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jformat)68{6970jlong result = (jlong) 0L;7172// Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]73// works off a data copy and doesn't have to go to the native event thread to get the data.74// We can have endTransfer just call startTransfer.7576JNI_COCOA_ENTER(env);77// Get the drop target native object:78CDropTarget* dropTarget = GetCDropTarget(jdroptarget);79if (dropTarget == nil) {80DLog2(@"[CDropTargetContextPeer startTransfer]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);81TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);82return result;83}8485GET_DTCP_CLASS_RETURN(result);86DECLARE_METHOD_RETURN(newDataMethod, jc_CDropTargetContextPeer, "newData", "(J[B)V", result);87if ((*env)->ExceptionOccurred(env) || !newDataMethod) {88DLog2(@"[CDropTargetContextPeer startTransfer]: couldn't get newData method for %d.\n", (NSInteger) jdroptarget);89TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);90return result;91}9293// Get data from drop target:94jobject jdropdata = [dropTarget copyDraggingDataForFormat:jformat];95if (!jdropdata) {96DLog2(@"[CDropTargetContextPeer startTransfer]: copyDraggingDataForFormat failed for %d.\n", (NSInteger) jdroptarget);97TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);98return result;99}100101// Pass the data to drop target:102@try {103(*env)->CallVoidMethod(env, jthis, newDataMethod, jformat, jdropdata); // AWT_THREADING Safe (!appKit)104} @catch (NSException *ex) {105DLog2(@"[CDropTargetContextPeer startTransfer]: exception in newData() for %d.\n", (NSInteger) jdroptarget);106(*env)->DeleteGlobalRef(env, jdropdata);107TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);108return result;109}110111// if no error return dropTarget's draggingSequence112result = [dropTarget getDraggingSequenceNumber];113JNI_COCOA_EXIT(env);114115return result;116}117118/*119* Class: sun_lwawt_macosx_CDropTargetContextPeer120* Method: addTransfer121* Signature: (JJJ)V122*/123JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_addTransfer124(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat)125{126// Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]127// works off a data copy and doesn't have to go to the native event thread to get the data.128// We can have endTransfer just call startTransfer.129130Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer(env, jthis, jdroptarget, jformat);131132return;133}134135/*136* Class: sun_lwawt_macosx_CDropTargetContextPeer137* Method: dropDone138* Signature: (JJZZI)V139*/140JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_dropDone141(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jboolean jislocal, jboolean jsuccess, jint jdropaction)142{143// Get the drop target native object:144JNI_COCOA_ENTER(env);145CDropTarget* dropTarget = GetCDropTarget(jdroptarget);146if (dropTarget == nil) {147DLog2(@"[CDropTargetContextPeer dropDone]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);148return;149}150151// Notify drop target Java is all done with this dragging sequence:152[dropTarget javaDraggingEnded:(jlong)jdroptransfer success:jsuccess action:jdropaction];153JNI_COCOA_EXIT(env);154155return;156}157158159